Search code examples
c#winformstextboxpanel

how to change a TextBox text inside a panel from another form that is inside another panel


i am working on my first application, i am using VS2017 with c# language.

i am trying hard without success to change a textbox text inside a panel from another form . i have a Main_Form that has two panels. The first panel is my "Main_Panel" which i use to show in it a lot of others forms. The second panel is my "Info_Panel", it contains a textbox to print the DateTime, and another title_textbox that contains the title of the actual form inside my Main_Panel. So far, i tried this program inside my Some_Form_load:

     private void Some_Form_Load(object sender, EventArgs e) 
     {
       MainForm f1 = new MainForm();
       f1.Title_Textbox.Text = "Some_Form";
     }

But nothing happened, probably because my title_textbox is my "Info_Panel". I am stuck for hours in this problem, a help would be very appreciate .


Solution

  • You're creating a new instance and changing that. You need to change your other one. Here is a rough test of how it should be working.

     private void Some_Form_Load(object sender, EventArgs e) 
     {
       MainForm f1 = Application.OpenForms.OfType<MainFrm>().FirstOrDefault();
       f1?.Title_Textbox.Text = "Some_Form";
     }
    

    This is a quick writeup and is untested, but it should work for what you need.

    EDIT: All that this does is grab all MainFrm's in the open applications list, and select the first one. Then it stores that in a variable and you're able to reference that variable to get the form you need :)