CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Feb 2010
    Posts
    112

    Windows form app add variable to textbox

    I made GUI in VC++2008 Win form app template and I dont know how to assign string or int variable to a textbox that I made.There's no variable field in text field properties and no add variable on right click.I haven't found any.

    All that I found in Form.h is :

    private: System::Windows::Forms::TextBox^ textBox2;
    private: System::Windows::Forms::TextBox^ textBox3;
    private: System::Windows::Forms::TextBox^ textBox4;
    private: System::Windows::Forms::TextBox^ textBox5;
    private: System::Windows::Forms::TextBox^ textBox6;
    private: System::Windows::Forms::Button^ button1;
    private: System::Windows::Forms::TextBox^ textBox1;

    I guess those are just made for panel and are not supposed to be casted into strings or types I need.

    This should be an easy question.

  2. #2
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Windows form app add variable to textbox

    A TextBox has a property of type string called Text. Convert the value you want to display and assigned it to this property. For instance:
    Code:
    int i = 42;
    textBox1->Text = i.ToString();
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  3. #3
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Windows form app add variable to textbox

    [ redirected ]
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  4. #4
    Join Date
    Feb 2010
    Posts
    112

    Re: Windows form app add variable to textbox

    Thanx cilu!

    So if I have textbox and user types [email protected] in, I will read that and store it in char * variable like this:

    char* emailVar;
    emailVar=textBox1->Text.toChar();

    ??? I gues I missed types cast func???

    and second question:

    what lib do I need to include to use UpdateData(); ???
    Last edited by suncica2222; March 6th, 2010 at 04:49 PM.

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Windows form app add variable to textbox

    If you decided from some reason to write C++/CLI GUI application, don't mix managed and unmanaged types if this is not necessary. To get a textbox text, use String variable:

    String^ s = textBox1->Text;

    Windows form doesn't have UpdateData function. You need to get/set Text property for every textbox on the form to emulate MFC UpdateData behavior.

  6. #6
    Join Date
    Feb 2010
    Posts
    112

    Re: Windows form app add variable to textbox

    what confuses me is that as I see it Ill should write all code in button clicked function in Form.h file ???

    like this:

    private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
    emailTo=this->textBox1->Text;
    MessageBox::Show(emailTo,"caption");
    char a[]= "kljlkj";
    //UpdateData(true);
    }


    coz when I make new class or write it in main.cpp I can not reference Form or textboxes...???

  7. #7
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Windows form app add variable to textbox

    C++/CLI designer produces ugly inline code, because the same program works also for C# and VB .NET. There is also Form1.cpp file, which contains only:
    #include "Form1.h"

    You can move implementation to .cpp file according to general C++ rules.

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