Link to home
Start Free TrialLog in
Avatar of Greg_L_WER
Greg_L_WERFlag for Canada

asked on

Getting Value of TextBox on Main Form From Child Control

I've built a Windows Forms application (using C#) that loads user controls into a Panel control on the "MainSwitchboard" form.  I am trying to access the value of a textbox on the MainSwitchboard from a user control but haven't had any luck.  I've done a lot of reading and have gotten much closer but not quite there.  From my reading I needed to change the Modifiers on that control to Internal or Public which I've done.  On the user control I instantiated an instance of the MainSwitchboard:

    public partial class ReportsQueries : UserControl
    {
        public ReportsQueries()
        {
            InitializeComponent();

            MainSwitchboard ms = new MainSwitchboard();
            textBox1.Text = ms.txtGUIDToProcessMAIN.Text;
       }
   }

and then get access to the textbox on the MainSwitchboard.  The only problem is that the textbox is empty when the application is started and the user control doesn't "see" the current value in the textbox once it's changes.  If I set a value to the textbox when the MainSwitchboard is loaded like:

    public partial class MainSwitchboard : Form
    {
        public MainSwitchboard()
        {
            InitializeComponent();

            txtGUIDToProcessMAIN.Text = "Text Text";
        }
           ....... extra text removed.....
     }


The user control will "see" the "Test Text" as the value of the textbox... when I change the text it still "sees" the original text.  I wondered about a "Refresh" type process (something that works in Access 97 (VB) applications (on the text changed event) but I haven't had any success with something like this process here.  Any assistance would be appreciated.

Thanks,
Greg
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

"On the user control I instantiated an instance of the MainSwitchboard"

You created a new, invisible instance of the form, which is why the field is blank.  You need the actual instance of the form which your usercontrol already resides in.

The first, obvious question is, why is your usercontrol attempting to access the main form?  This immediately jumps out as a bad design.  Can you explain the process here?
Avatar of Greg_L_WER

ASKER

Thanks Idle_Mind,

My logic here is that I am using multiple different user controls that are doing different functions that are all related to the same GUID.  By placing this text box on the main page I do not have to duplicate the same control over all the different user controls and thereby the user doesn't need to enter in the same GUID more than the one time.  Maybe there's a better solution to this situation?  

Thanks,
Greg
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Excellent!  Works perfectly.  This is my first Windows Forms application and it's definitely a little different way of thinking than web development and drastically different/better than my Access 97 projects :)  

Thanks for the help,
Greg
Haha...I'm in the opposite boat.  WebForms are weird to me.  I guess this approach is not unlike using a session variable for a web page?
So very true...  Once you've got a session in place you can refer to it anywhere you happen to be... pro's and con's to both processes I'd suspect... so long as it works :)  Thanks again for the help... my monthly subscription fee to EE is more than worth it...