CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Oct 2003
    Location
    Durham nc
    Posts
    80

    Text from editbox

    Hi, i'm having problems getting the text from an edit box. I have a splitter window with two windows. My editbox is on one window and i want to get the text from it and send it to my second window. I have tried the option below but it gives me an error when the program starts.
    CWnd* pWnd = GetDlgItem(IDD_EDIT1);
    CString Tester;
    pWnd->GetWindowText(Tester);

    I'm using Doc/view arc , and the Editbox does not belong to the view that is calling for the text, is there another way to do this. I also tried the selection below and it gave me a blank string.

    GetDlgItemText(IDD_EDIT,Tester);

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635
    without more code it's hard to say, but if you get an error when the program starts, perhaps the window for the control doesn't exist yet. Test for pWnd != NULL before you use it.

  3. #3
    Join Date
    Oct 2003
    Location
    Durham nc
    Posts
    80
    I just tested for pWnd and it did return NULL, how do i go about fixing this.

  4. #4
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    The easiest way would probably be to simply define a DDX member variable for the control and use it...

  5. #5
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635
    Originally posted by pa2003
    I just tested for pWnd and it did return NULL, how do i go about fixing this.
    Again, without knowming more about your app, it's hard to say. What happens if you just say

    CWnd* pWnd = GetDlgItem(IDD_EDIT1);
    CString Tester;
    if(pWnd != NULL)
    pWnd->GetWindowText(Tester);

  6. #6
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by GCDEF
    Again, without knowming more about your app, it's hard to say. What happens if you just say
    Well, he already said that GetDlgItem() returned NULL... Most probably because he tried to access the control before it was created. A simple DDX CString member variable would do. But for some reason I never understood, a lot of people prefer using the complicated way with GetDlgItem() and GetWindowText()...

  7. #7
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656
    Well, if pWnd is NULL, your test would produce an empty string...
    To OP: did you create CEdit ontrol? In the window that you are trying to get to it? With ID of IDD_EDIT1? Before you access it?
    Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
    Convenience and productivity tools for Microsoft Visual Studio:
    FeinWindows - replacement windows manager for Visual Studio, and more...

  8. #8
    Join Date
    Oct 2003
    Location
    Durham nc
    Posts
    80
    how do i go about "DDX member variable for the control and use it"


    when i tried the code below it skipped the last line so the program came up with a blank screen where my text was suppose to be.

    CWnd* pWnd = GetDlgItem(IDD_EDIT1);
    CString Tester;
    if(pWnd != NULL)
    pWnd->GetWindowText(Tester);

    My problem is that its two different view and i can't seem to send the text to the second view.

  9. #9
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by pa2003
    how do i go about "DDX member variable for the control and use it"
    You create them using class wizard. Search MSDN for DDX, it's all explained there.

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635
    Originally posted by gstercken
    Well, he already said that GetDlgItem() returned NULL... Most probably because he tried to access the control before it was created. A simple DDX CString member variable would do. But for some reason I never understood, a lot of people prefer using the complicated way with GetDlgItem() and GetWindowText()...
    I know. I'm assuming, perhaps incorrectly, that since he said his app crashes at the beginning, subsequent calls to the same code would work once the windows were created. It's hard to tell without knowing more about what's going on.

  11. #11
    Join Date
    Oct 2003
    Location
    Durham nc
    Posts
    80
    I am thinking about doing this another way but i don't know if what am asking is possible. Is it possible to make a string work similar to some think like a static int; if i can have the string declared just once i can make a function that other functions can call, the problem am having when i try this is that each call resets the string. Is it posssible to do this, i just want to store the string some where then get it when its needed. I can get the text from inside its view, i just can get it to the second view, this might be able to do the trick if its possible.
    Last edited by pa2003; February 5th, 2004 at 12:05 PM.

  12. #12
    Join Date
    Sep 2002
    Location
    14° 39'19.65"N / 121° 1'44.34"E
    Posts
    9,815
    Originally posted by pa2003
    Is it possible to make a string work similar to some think like a static int; if i can have the string declared just once i can make a function that other functions can call, the problem am having when i try this is that each call resets the string. Is it posssible to do this, i just want to store the string some where then get it when its needed. I can get the text from inside its view, i just can get it to the second view, this might be able to do the trick if its possible.
    I don't see where static variables would come in... A CString member variable is all you need. More specifically, and as already said, a DDX member variable, to have the contents automatically transferred between your CString and the edit control. All you need to do is to access that member from outside the view, so I don't understand why you are still hesitating.

  13. #13
    Join Date
    Oct 2003
    Location
    Durham nc
    Posts
    80
    Can you give me an example using DDX, i read the msdn documents but i could not understand it.
    If i have a CString Example, how do i go about saving it with DDX and then getting it back.

  14. #14
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421
    Maybe I misunderstood, but from what I read, I believe you are trying to obtain the text from one of the splitter windows (which contains the edit box) from the other splitter window. Is this correct?

    In other words, is this code
    Code:
    CWnd* pWnd = GetDlgItem(IDD_EDIT1);
    CString Tester;
    pWnd->GetWindowText(Tester);
    located in the "second window" and the control exists in the first window? If so, that won't work since window "B" won't have access to window "A"s controls.

    If you're using doc/view architecture, you could obtain the text within Window A and store the text in the doc. Then call the document's UpdateAllViews and handle the OnUpdate in window B to read the new value.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  15. #15
    Join Date
    Oct 2003
    Location
    Durham nc
    Posts
    80
    Krmed you are right that is what i want to do, gstercken
    told me a way to do it is using DDX, thats what i'm trying now, i am having some problems but gstercken if you see any error with it let me know, the text is still null, here is my code.


    in DOExchange:
    CEdit* pEditBox = (CEdit*)GetDlgItem( IDD_EDIT1 );
    pEditBox->GetWindowText(m_edit1);
    CFormView:: DoDataExchange( pDX );
    //{{AFX_DATA_MAP(CSearchView)
    DDX_Text(pDX, IDD_EDIT1, m_edit1 );
    //}}AFX_DATA_MAP


    In myprogram:
    m_edit1=Text4;
    UpdateData(FALSE);

Page 1 of 2 12 LastLast

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