Thread: weird errors

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    2

    weird errors

    Hi everyone, forgive me if my errors will seem trivial to you, I've only recently starting working with Windows programming and MFC (although I've had plenty of experience programming in console), and my program doesn't seem to work as expected.

    First of all, let me explain what it should do and how it's set up. Basically, I have a dialog box with 5 edit boxes and 1 "ok" button on it. I'm trying to get the dialog boxes to work like the ones for the registration key with one little extra feature. The caret should jump to the next box after 5 characters are put into one box, and also if the user uses copy/paste command, the pasted text gets split into the boxes. Well, I wrote the program that should do all this, but there are some things that happen that should not happen and I have no clue why. Basically I have the first 4 boxes carry identical code:

    Code:
    void Dlg2::OnChangeEdit1() 
    {
    	// TODO: If this is a RICHEDIT control, the control will not
    	// send this notification unless you override the CDialog::OnInitDialog()
    	// function and call CRichEditCtrl().SetEventMask()
    	// with the ENM_CHANGE flag ORed into the mask.
    	
    	// TODO: Add your control notification handler code here
    	CWnd* m_wEdit1=GetDlgItem(IDC_EDIT1);
    	m_wEdit1->GetWindowText(key1);
    	temp=key1.GetLength();
    	if (temp>4)
    	{
    		temp-=5;
    		CWnd* m_wEdit2=GetDlgItem(IDC_EDIT2);
    		m_wEdit2->SetWindowText ( _T(key1.Right(temp)));
    		m_wEdit1->GetWindowText(key1);
    		if(temp>0) m_wEdit1->SetWindowText ( _T(key1.Left(5)));
    		m_wEdit2->SetFocus();
    	}
    }
    and the fifth box just limits the maximum text to 5. The above code works fine for typing the key in manually, the cursor switches to next window after 5 characters are typed in. However, copy/pasting the code in doesn't work like I expected it to. Instead, the first box gets all of the pasted text, the 2nd gets all but the first 5 characters, and so on. So for example if I type in "abcdefghijklmno", the first box will have "abcdefghijklmno", 2nd will have "fghijklmno" and 3rd "klmno".

    Now here is what's even weirder. If I take the exact same code from above, change the "if (temp>4)" statement to "if (temp>5)" and remove the "if (temp>0)" part from the "if (temp>0) m_wEdit1->SetWindowText(_T(key1.Left(5)));" then the copy/pasting works like it should with each box only receiving 5 characters, but when the text is put in manually the cursor goes right before the last character when it is moved to the new box. This makes sense, since I never ordered the caret to move, but as I will explain in a moment even ordering it to move won't do anything. Another thing is, if I leave the "if (temp>4)" message alone, and just remove the "if (temp>0)" part, the program crashes when it processes the 5th character. This I don't udnerstand either, the if is in front of the statement that selects the 5 characters to be put into the first box. Therefore it's guaranteed that when the user puts in 5 characters there should be enough characters for the first box, yet it always crashes after 5th character is put in (or when the message is pasted into it that's longer than 5 characters). I'm very confused by this error as well.

    Last, but not least, the caret isn't moving. If I add the code below to the code above after the last line (SetFocus one) then it gives the same result as if I never used the code below:

    Code:
    		CPoint myPoint = m_wEdit2->GetCaretPos();
    		CDC* pDC = m_wEdit2->GetDC();
    		myPoint.x += (pDC->GetTextExtent (_T(key1.Right(temp)), temp)).cx;
    		SetCaretPos (myPoint);
    And I don't understand why this doesn't work either. Basically all I'm doing is taking the current caret position, figuring out the physical length of the text, and offsetting the caret. But it doesn't get offset. I even tried creating a new function "OnSetFocusEdit2" so that it gets called as soon as focus is set to second edit box, which box 1 does when it finishes and copying the caret code there instead, but still no effect. If anyone can explain to me these weird happenings, I would appreciate it.
    Last edited by R3N3G4D3; 06-14-2005 at 06:58 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP with DX9 Errors!!!
    By Tommaso in forum Game Programming
    Replies: 7
    Last Post: 06-28-2006, 02:51 PM
  2. Errors with header files in OpenGL using VisualC++
    By wile_spice in forum Game Programming
    Replies: 3
    Last Post: 06-22-2006, 08:56 AM
  3. Weird Errors in VS 2003
    By Devil Panther in forum Windows Programming
    Replies: 1
    Last Post: 10-01-2005, 06:16 AM
  4. executing errors
    By s0ul2squeeze in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2002, 01:43 PM