Geting variables from UserControl
-
As the subject says, how do I get a variable out of a user control? I have a user control in my main form, which contains a text box. On pressing a button on the main form, I want a message box to pop up with whatever I put into the textbox. So how do you do this? (I can do everything but get the text from the contrl to the messagebox in my main form) Just a note, I need it this way, because later on the program is going to take a bunch of variables from multiple UserControls, and pass it into a global array. - Munty
-
As the subject says, how do I get a variable out of a user control? I have a user control in my main form, which contains a text box. On pressing a button on the main form, I want a message box to pop up with whatever I put into the textbox. So how do you do this? (I can do everything but get the text from the contrl to the messagebox in my main form) Just a note, I need it this way, because later on the program is going to take a bunch of variables from multiple UserControls, and pass it into a global array. - Munty
Hi, define a property in your user control and access it from your main form:
//in the user control
public string MtyTextBoxText {
get { return myTextBox.Text; }
}//in the main form
public void DoSomething() {
MessageBox.Show(myUserControl.MyTextBoxText);
}Robert
-
Hi, define a property in your user control and access it from your main form:
//in the user control
public string MtyTextBoxText {
get { return myTextBox.Text; }
}//in the main form
public void DoSomething() {
MessageBox.Show(myUserControl.MyTextBoxText);
}Robert
That worked perfectly, thanks. :) Now for another (probably silly) question. The user control gets created by a button press. It gets added to an array. Now I want a button in the UserControl that can remove that particular UserControl from the GUI (and stop the code from seeing it) Now I can remove it from the GUI with the button inside the UserControl, but I'm not sure how (or if) you can remove the user control from the array in the main form, from the button in the user control. So is this possible? (I'm assuming some sort of event handler could be used) If not, I can work around the problem, but it would be better (much better) to remove the control from the array. - Munty
-
That worked perfectly, thanks. :) Now for another (probably silly) question. The user control gets created by a button press. It gets added to an array. Now I want a button in the UserControl that can remove that particular UserControl from the GUI (and stop the code from seeing it) Now I can remove it from the GUI with the button inside the UserControl, but I'm not sure how (or if) you can remove the user control from the array in the main form, from the button in the user control. So is this possible? (I'm assuming some sort of event handler could be used) If not, I can work around the problem, but it would be better (much better) to remove the control from the array. - Munty
Hi, yes I would use an event in this case and also let the form remove the control instead of letting the control remove itself:
//in your userControl
public event EventHandler RemoveButtonPressed;//in the button event handler in the user control
if (RemoveButtonPressed != null)
RemoveButtonPressed(this, EventArgs.Empty);//in your main form when adding the control
myUserControl.RemoveButtonPressed += new EventHandler(OnRemoveButtonPressed);//and the appropriate event handler
private void OnRemoveButtonPressed(object sender, EventArgs ea) {
MyUserControl ctrl = (MyUserControl)sender;//don't forget to unbind the event
ctrl.RemoveButtonPressed -= new EventHandler(OnRemoveButtonPressed);//remove from array and from form
}Robert
-
Hi, yes I would use an event in this case and also let the form remove the control instead of letting the control remove itself:
//in your userControl
public event EventHandler RemoveButtonPressed;//in the button event handler in the user control
if (RemoveButtonPressed != null)
RemoveButtonPressed(this, EventArgs.Empty);//in your main form when adding the control
myUserControl.RemoveButtonPressed += new EventHandler(OnRemoveButtonPressed);//and the appropriate event handler
private void OnRemoveButtonPressed(object sender, EventArgs ea) {
MyUserControl ctrl = (MyUserControl)sender;//don't forget to unbind the event
ctrl.RemoveButtonPressed -= new EventHandler(OnRemoveButtonPressed);//remove from array and from form
}Robert