CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Guest

    Assertion error with CEdit

    Hi..
    I'm just new in mfc programming, and in my first program, I have a little pb with a CEdit control..Actually, I got a dialog-based frame with a CEdit control in it, and if I write this

    MyCEditCtrl.SetWindowText("");



    to empty the CEdit, I have an assertion error when I run the program...
    In some functions, this works well, and in some other, this gives me an assertion error..How come ??
    thank you for answers..


  2. #2
    Guest

    Re: Assertion error with CEdit

    your edit may not have been created yet.



  3. #3
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Assertion error with CEdit

    Assuming MyCEditCtrl is a member variable generated with ClassWizard, you may be trying to perform the operation you describe before it has been associated with the edit control itself. This is the same as suggested by Anonymous #2 in the other response. Here is an example of what might be wrong:

    ---BOOL CMyDlg::OnInitDialog()
    {
    // This will assert, because the object MyCEditCtrl has not been
    // associated with the Windows edit control until
    // CDialog::OnInitDialog() is called.
    MyCEditCtrl.SetWindowText("");

    CDialog::OnInitDialog();

    return TRUE ;
    }




    ---

    If you are not using member variables, then as Anonymous #2 suggested, you may have declared the C++ wrapper object but not called its Create() method before trying to use it.

    Does this help?



    --
    Jason Teagle
    [email protected]

  4. #4
    Guest

    Re: Assertion error with CEdit

    Well, for example, in a method, I have this :

    CString myString;
    myCEditCtrl.GetWindowText(myString);
    MessageBox(myString);
    myCEditCtrl.SetWindowText("");



    When I run it, then I have a MessageBox exactly showing what's in myCEditCtrl..So, that would mean myCEditCtrl has been created cause I can use it...Anyway..After I close the MessageBox, then I got the assertion error, exactly at the line : myCEditCtrl.SetWindowText("");
    So, if I can use GetWindowText, then I should be able to use SetWindowText too...but no, this gives me an assertion error...
    So, what about this ??..
    I've found an alternative by doing :

    myCEditCtrl.SetSel(0, -1);
    myCEditCtrl.ReplaceSel(0, -1,"");



    then, this works...(I'm not sure about the exact syntax of ReplaceSel() and SetSel(), coz I'm not using VisualC right now -> I'm in school, and there's no vc in school)...but well, this works..The problem is that it's not as 'friendly' as SetWindowText() method...
    So, any idea now about my problem, or do you think your code sample can resolve it ?


  5. #5
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Assertion error with CEdit

    Hmmm... You're right, if you can use GetWindowText() then you should be able to use SetWindowText() as well - can you be sure your edit box is not read-only (although you should still be able to call SetWindowText(), it is just that the USER can't change it)?

    Try doing a full recompile of your project. I have found that sometimes too many incremental recompiles and links screws VC++ up, so a full recompile should cure this. However, I don't expect this to be the problem.

    The next thing to do is, when the assertion occurs, make a note of the filename and line number it occurs in, and go find the routine it has failed in - then post the code. It is probably not a good idea to just post the name and line of the file, as we might well have different code libraries (we have VC++ V4.0 here). Post the whole of the routine it failed in, as well as any other information you can give about the edit control (properties at run time, etc.).

    Hopefully this will tell us why it is failing. If you want to bypass the BB route, e-mail me the details at [email protected] and I'll take a look.

    (An interesting side-note is that I have had a problem in the past with using GetWindowText() on a drop-down LIST combo box - it seems that that only works on a drop-down combo with an EDITABLE FIELD - you have to use GetCurSel() and GetLBText() instead for a static field combo; almost like your problem!)

    The only other thing you can do is e-mail me the whole project, so that I can see if anything else in the surrounding code is having a strange effect (it happens...).



    --
    Jason Teagle
    [email protected]

  6. #6
    Guest

    Re: Assertion error with CEdit

    Hey..thank you a lot for your interest about my problem..this is nice..
    But well..I don't think I need to send to you my whole project files since I can find an alternative to my problem using SetSel() and ReplaceSel()...
    Anyway, I've looked up to my program yesterday, and I've tried to figure out when the program makes me an assertion error, since it doesn't do assertion error in all methods...So, I've found (but I'm not sure of this) that if the CEdit is filled with "" or " " (blank text..), then I have an assertion error when I call the SetWindowText("") function...And if the CEdit is filled with "Something in it !", not a blank text, then SetWindowText("") works...quite interesting, huh...well, now, if you think you can know why it works like this, just tell me, ok ? Otherwise, don't get blured with it...I've got alternatives to this, and I don't need to use SetWindowText() method when I can use SetSel() and ReplaceSel()...
    thanks again for your help Jason..


  7. #7
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Assertion error with CEdit

    Well, if you're happy to use the alternative then I guess we'll leave it, but I'm curious to know why it is happening. It does seem more than a coincidence that it is white space or empty string that cause it to go wrong. Did you manage to find the routine within the (standard) MFC code where the assertion occurred? It might give us a clue...



    --
    Jason Teagle
    [email protected]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured