setting textbox text onto a child Form


Answer this question

setting textbox text onto a child Form

  • behnamkh

    Yes that helps. I've figured out that I need to pass the pointer to the textBox to function X.
    Here's what works :

    function X definition :
    X(System::Windows::Forms::TextBox* box, .....)
    {.....
         box->set_Text(S"hi");
    ....}

    inside Form2, the function X is called :

     X(textBox1, ....);

    The only thing left is to pass a group of textBox pointers to function X. Should I create a struct or enum of pointers How will I call function X inside Form2

    ak

  • Mark Dostie

    IMHO making the control public is really a hack, making a set function public and from within the function you can have it set the Text property of the textbox is a more OO compilant way. But it is your call :).

    The problem you metion above is really due to the fact that the pointer to a Form2 object is defined but no actual Form2 object instances has been assigned to it unless you give it a valid address the error will still pop-up.

    Say in one of the parameter you have BOOL X ( Form2* _theForm,z, //other params) then you do Form2* pForm2 = _theForm; then the pForm2 pointer is properly pointing to an object, of coz you have to have Form2 constructed before calling this function X.

    Again, I have a feeling you are breaking encapsulation everwhere, if you want to do what you want. However, I would not recomment as it one of the problem OO wants to fix. But come back to your question, you can make myFrom2 as a global and public variable, so that function X can access the form anywhere.

    Hope this helps.
    Stardust


  • Arturo_RX7

     I think I'm close :

    I have defined textbox1 in the Form2 class as a public, rather than private, member.

    The function X looks like this :

    BOOL X ( z, //other arguments//)
    {
        Form2* pForm2;
    //more code
        pForm2->textBox1->set_Text(z);
    //more code
    }

    Everything compiles :), but when it gets to the pForm2 statement I get an unhandled exception :
    object reference not set to an instance of an object.

    Way back inside Form1, Form2 was defined as :
    Form2 *myForm2 = new Form2;
    myForm2->Show();

    How can I resolve this
    Is there a way to call myForm2 inside X Should this be done

    Any feedback would be most appreciated -
    ak

  • GregA

    Hi there,

    So bascally you have 3 objects, Form1, Form2 and SomeClass. Form1 create Form2 and SomeClass wants to access somthing defined in Form2. If I get this far without misunderstand your problem then the following might be your solution:

    1. In Form2, make a pubic write only say
    // Sorry I come from a C# background
    public String SetTextBoxText(string _text)
    {
           set{ this.textbox1.Text = _text;}
    }

    2. Then modifiy the function X to access Form2.textbox1 as follow:
    public bool X(Form2 _someForm, .......)
    {
        // Some code before this
       _someForm.SetTextBoxText =  "The String you want to display";
      // mode code after this
    }

    Hope this helps!!!!




  • setting textbox text onto a child Form