Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Help with textbox control

Help with textbox control

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestioncsharpc++game-dev
12 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Nelek

    Mmmm, are you asking to port from c# to c++? or c++ to c#? Because... there are elements in your code that are unknown for me :S

    Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

    S Offline
    S Offline
    Stick
    wrote on last edited by
    #3

    No, the above code has nothing to do with C#, it is straight C++.

    N 1 Reply Last reply
    0
    • S Stick

      Ok, I'm new to doing this in C++, although I have done it in C#. As this code has to work inside another larger C++ project I have (game program) I can't just do it in C#. In my click handler for a button I want to put text into a textbox control on my form. Unfortunately, the "text" is in a c-string (ie null terminated string), and so I'm not sure how to get it to work: System::Void btnGen_Click(System::Object^ sender, System::EventArgs^ e) { BYTE bMacaddress[MAC_DIM]; CSoftwareKey::RetrieveMACAddress(bMacaddress); char* pMacString = NULL; CSoftwareKey::Buffer2HexString( bMacaddress, MAC_DIM, &pMacString ); tbMacAddress->Text = *pMacString; // <---- Problem line } I tried first making a string: string strMacAddr(pMacString); and then: tbMacAddress->Text = pMacString; but this doesn't work either (and I'm sure you are laughing now) =) Anyway, how can I do this? Basically, pMacString points to something like this: "000C41805D2D\0" ( if I am making sense ) and so that's what I want in the text box. Thanks Patrick

      H Offline
      H Offline
      Hamid Taebi
      wrote on last edited by
      #4

      Your code is managed it was better you asked on the (Managed) C++/CLI[^]


      WhiteSky


      S 1 Reply Last reply
      0
      • S Stick

        No, the above code has nothing to do with C#, it is straight C++.

        N Offline
        N Offline
        Nelek
        wrote on last edited by
        #5

        :omg: then I'm more newbie than I thought :doh: :P hehehehe I've never seen "^" used as "System::Object^ sender" Nevermind, your tbMacAddress->Text is a string or a char[x]? I had some problems with CString and char* in VC++ because of the "\0" at the end of the CStrings. I solved it considering the CString as a vector and taking every letter in a loop and putting it in the place of the char[20] that was needed. Afterwards I passed the &char[0] where the LPCSTR was asked for and it worked. Finally I found another way to do it and changed it but... sometimes silly things give the way to the correct answer.

        Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

        1 Reply Last reply
        0
        • H Hamid Taebi

          Your code is managed it was better you asked on the (Managed) C++/CLI[^]


          WhiteSky


          S Offline
          S Offline
          Stick
          wrote on last edited by
          #6

          My question has nothing to do with managed code. Basically, forget the code which is confusing you, and just think of this as the question: If you have a c-string (ie a null terminated string) and you want to set the Text in a textbox to that value, how do you do it? For example, when a button on the form is pushed, set the textbox to the c-string's value. Patrick

          N H 2 Replies Last reply
          0
          • S Stick

            My question has nothing to do with managed code. Basically, forget the code which is confusing you, and just think of this as the question: If you have a c-string (ie a null terminated string) and you want to set the Text in a textbox to that value, how do you do it? For example, when a button on the form is pushed, set the textbox to the c-string's value. Patrick

            N Offline
            N Offline
            Nelek
            wrote on last edited by
            #7

            Hi, to do that I associate a member variable (CString) to the CEditBox and when I push a button or happens whatever it shall happen, call UpdateData (FALSE); On the other hand, as the CEdit is derived from CWnd. It should be possible to modify its text with SetWindowText( LPCTSTR lpszString ); but I'm not sure because I have never tried it, I do it as above (VC++ 6)

            Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

            S 1 Reply Last reply
            0
            • S Stick

              My question has nothing to do with managed code. Basically, forget the code which is confusing you, and just think of this as the question: If you have a c-string (ie a null terminated string) and you want to set the Text in a textbox to that value, how do you do it? For example, when a button on the form is pushed, set the textbox to the c-string's value. Patrick

              H Offline
              H Offline
              Hamid Taebi
              wrote on last edited by
              #8

              If you want to set text for a control Textbox(editbox) or button you can use of SetWindowText();


              WhiteSky


              1 Reply Last reply
              0
              • N Nelek

                Hi, to do that I associate a member variable (CString) to the CEditBox and when I push a button or happens whatever it shall happen, call UpdateData (FALSE); On the other hand, as the CEdit is derived from CWnd. It should be possible to modify its text with SetWindowText( LPCTSTR lpszString ); but I'm not sure because I have never tried it, I do it as above (VC++ 6)

                Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

                S Offline
                S Offline
                Stick
                wrote on last edited by
                #9

                Code? So if I tell you that you are given a variable: myString[] = "0C34AZ"; // which is null terminated what would be working code to put it in a textbox?

                N 1 Reply Last reply
                0
                • S Stick

                  Code? So if I tell you that you are given a variable: myString[] = "0C34AZ"; // which is null terminated what would be working code to put it in a textbox?

                  N Offline
                  N Offline
                  Nelek
                  wrote on last edited by
                  #10

                  Hi another time, ok, I have the DoDataExchange in my Form, and a member variable associated to my CEdit. DDX_Text(pDX, IDC_MYEDIT, m_szMemberString); I initialize it to “” (empty) in the constructor. And then, in some events/messages I put the name of another element I saved in the Document, in the example a click in a ListCtrl (report mode) where my elements are listed

                  void CMyFormView::OnClickCtrlList(NMHDR* pNMHDR, LRESULT* pResult)
                  { POSITION pos = m_clcMyList.GetFirstSelectedItemPosition();

                  //If the click was outside the range
                  

                  if (!pos)
                  { m_nRowNum = -1;
                  m_cbButton1.EnableWindow (FALSE);
                  m_ szMemberString = “”;
                  UpdateData (FALSE);
                  }

                  while (pos)
                  {	m\_nRowNum = m\_clcRuleList.GetNextSelectedItem(pos);
                  	M\_szMemberString = pDoc->GetElementNameOfColumn (m\_nRowNum);
                  	m\_cbButton1.EnableWindow (TRUE);
                  	UpdateData (FALSE);
                  }
                  
                  Invalidate ();			UpdateWindow ();
                  \*pResult = 0;
                  return;
                  

                  }

                  Another way (without member variables and the DoDataExchange) can be:

                  void CMyFormView::OnClickCtrlList(NMHDR* pNMHDR, LRESULT* pResult)
                  { POSITION pos = m_clcMyList.GetFirstSelectedItemPosition();
                  CEdit* pEdit = (CEdit*)GetDlgItem (IDC_MYEDIT);

                  //If the click was outside the range
                  if (!pos)
                  {	m\_nRowNum = -1;
                  	m\_cbButton1.EnableWindow (FALSE);
                  	pEdit->SetWindowText ("");
                  }
                  
                  while (pos)
                  {	m\_nRowNum = m\_clcRuleList.GetNextSelectedItem(pos);
                  	pEdit->SetWindowText (pDoc->GetElementNameOfColumn (m\_nRowNum));
                  	m\_cbButton1.EnableWindow (TRUE);
                  }
                  
                  Invalidate ();			UpdateWindow ();
                  \*pResult = 0;
                  return;
                  

                  }

                  The function I’m using is: CString CMyDoc::GetElementNameOfColumn (int nRow);

                  Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

                  S 1 Reply Last reply
                  0
                  • N Nelek

                    Hi another time, ok, I have the DoDataExchange in my Form, and a member variable associated to my CEdit. DDX_Text(pDX, IDC_MYEDIT, m_szMemberString); I initialize it to “” (empty) in the constructor. And then, in some events/messages I put the name of another element I saved in the Document, in the example a click in a ListCtrl (report mode) where my elements are listed

                    void CMyFormView::OnClickCtrlList(NMHDR* pNMHDR, LRESULT* pResult)
                    { POSITION pos = m_clcMyList.GetFirstSelectedItemPosition();

                    //If the click was outside the range
                    

                    if (!pos)
                    { m_nRowNum = -1;
                    m_cbButton1.EnableWindow (FALSE);
                    m_ szMemberString = “”;
                    UpdateData (FALSE);
                    }

                    while (pos)
                    {	m\_nRowNum = m\_clcRuleList.GetNextSelectedItem(pos);
                    	M\_szMemberString = pDoc->GetElementNameOfColumn (m\_nRowNum);
                    	m\_cbButton1.EnableWindow (TRUE);
                    	UpdateData (FALSE);
                    }
                    
                    Invalidate ();			UpdateWindow ();
                    \*pResult = 0;
                    return;
                    

                    }

                    Another way (without member variables and the DoDataExchange) can be:

                    void CMyFormView::OnClickCtrlList(NMHDR* pNMHDR, LRESULT* pResult)
                    { POSITION pos = m_clcMyList.GetFirstSelectedItemPosition();
                    CEdit* pEdit = (CEdit*)GetDlgItem (IDC_MYEDIT);

                    //If the click was outside the range
                    if (!pos)
                    {	m\_nRowNum = -1;
                    	m\_cbButton1.EnableWindow (FALSE);
                    	pEdit->SetWindowText ("");
                    }
                    
                    while (pos)
                    {	m\_nRowNum = m\_clcRuleList.GetNextSelectedItem(pos);
                    	pEdit->SetWindowText (pDoc->GetElementNameOfColumn (m\_nRowNum));
                    	m\_cbButton1.EnableWindow (TRUE);
                    }
                    
                    Invalidate ();			UpdateWindow ();
                    \*pResult = 0;
                    return;
                    

                    }

                    The function I’m using is: CString CMyDoc::GetElementNameOfColumn (int nRow);

                    Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

                    S Offline
                    S Offline
                    Stick
                    wrote on last edited by
                    #11

                    I just realised why I'm having the problem I'm having. I started a straight C++ project, and added a CLR form! I really didn't want a managed form, as I want to stick with unmanaged C++. duh So, basically, either I do this in C# ( or managed C++ ) or I have to learn how to do windows forms :( I think I'll stick to a console app to do this simple job and avoid the whole dang problem. Windows programming is just to much of a pain to learn.

                    N 1 Reply Last reply
                    0
                    • S Stick

                      I just realised why I'm having the problem I'm having. I started a straight C++ project, and added a CLR form! I really didn't want a managed form, as I want to stick with unmanaged C++. duh So, basically, either I do this in C# ( or managed C++ ) or I have to learn how to do windows forms :( I think I'll stick to a console app to do this simple job and avoid the whole dang problem. Windows programming is just to much of a pain to learn.

                      N Offline
                      N Offline
                      Nelek
                      wrote on last edited by
                      #12

                      Hehehehe, This is the same my partner said. He try to piss me off with "look, in VB I can print with just 2 lines", because I'm having problems to personalize a PrintDialog, margins, Bitmaps... Windows programming is not easy at all. But it is not imposible, just a question of time and being persistent.

                      Greetings. -------- M.D.V. If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups