This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
accessing controls on parentform from childform
I have parent form form1 and child form test1 i want to change label text of parent form from child form in my parent form i have method to showresult()
public void ShowResult()
{ label1.Text="hello"; }
I want to change label.Text="Bye"; form my child form test1 on button click event. Please give any suggestions.
when calling the child form, set the Parent property of child form object like this..
Test1Form test1 = new Test1Form();
test1.Show(this);
On your parent form, make the property of your label text like as..
public string LabelText
{
get
{
return Label1.Text;
}
set
{
Label1.Text = value;
}
}
From your child form you can get the label text like that..
((Form1)this.Owner).LabelText = "Your Text";
No doubt there are plenty of short cut ways to do this, but in my view a good approach would be to raise an event from the child form that requests that the parent form changes the displayed text. The parent form should register for this event when the child is created and can then respond to it by actually setting the text.
So in code this would look something like this:
public delegate void RequestLabelTextChangeDelegate(string newText);
public partial class Form2 : Form
{
public event RequestLabelTextChangeDelegate RequestLabelTextChange;
private void button1_Click(object sender, EventArgs e)
{
if (RequestLabelTextChange != null)
{
RequestLabelTextChange("Bye");
}
}
public Form2()
{
InitializeComponent();
}
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.RequestLabelTextChange += f2_RequestLabelTextChange;
}
void f2_RequestLabelTextChange(string newText)
{
label1.Text = newText;
}
}
Its a bit more long winded but it de-couples your child form from having any knowledge of its parent. This is a good pattern for re-usability as it means the child form could be used again in another host (that doesn't have a label) without breaking.
Try something like this:
Test1Form test1 = new Test1Form();
test1.Show(form1);
((Form1)test1.Owner).label.Text = "Bye";
Related
I know this might sound basic, but as a beginner, I've been confused about the following:
I have two forms, the first one is where all connections and calculations are made, and the second one is where I want to display all the results.
FORM 1, Where I call to open the second form.
private void button_Click1(object sender, EventArgs e)
{
Button selected = sender as Button;
openClientForm(new Form2());
}
private Form activeForm = null;
private void openClientForm( Form clientForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = clientForm;
clientForm.TopLevel = false;
clientForm.FormBorderStyle = FormBorderStyle.None;
clientForm.Dock = DockStyle.Fill;
panelClientForm.Controls.Add(clientForm);
panelClientForm.Tag = clientForm;
clientForm.BringToFront();
clientForm.Show();
}
FORM 2)
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
//Form2_Load();
updateText(txtCmdConsole, Form1.cmdres);
}
private void updateText(TextBox text, string strdata)
{
text.AppendText(strdata);
}
}
I have 1 question.
When I open the second form, How can I run the Method updateText from form 1 and display the result in the second form?
The reason I ask is that form1 should still be running in the background, and if a certain condition is met, I want the update text to be run in form 1(to get values from there) and update the second form.
First, your activeForm should be of type Form2, not just Form, otherwise you cannot call specific methods from it (at least not easily). The code should look like this:
private Form2 activeForm = null;
private void openClientForm(Form2 clientForm)
{
if (activeForm != null)
activeForm.Close();
activeForm = clientForm;
//...
Form2 can then offer a public interface to update certain text from the outside, with methods which don't expect internal controls as parameters. Something along the lines of
// inside Form2
public void UpdateCmdText(string strdata)
{
updateText(txtCmdConsole, strdata);
}
Now you can call this method inside Form1 whereever you like:
activeForm.UpdateCmdText(someNewResult)
Hope this helps.
I have a User control with a textbox. I want to pass the text from textbox on the User control back to the parent form, when the text changes.
I found the following code in my user control which work well for doing this. This includes a TextChanged event.
public string pub_callsign
{
get { return textBox1.Text; }
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
var textBoxContent = this.textBox1.Text;
var parent = this.Parent as form1;
parent.pub_callsign = pub_callsign;
}
In my parent form I have
public string pub_callsign
{
set { txtbx_callsign.Text = value; }
}
However I would like to use this User Control on more than 3 form,
So how do I get the form name from the form to the user control. Thereby what is shown below as form1 or form2 to be a variable containing the formname ??
var parent = this.Parent as form1;
var parent = this.Parent as form2;
var parent = this.Parent as formname_value;
hope this makes sense.
Dave
Add a property in the user control that gives you access to the text box contents.
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public string Contents { get => textBox1.Text; set => textBox1.Text = value; }
}
In the form use the property to read the contents of the text box
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void ReadContents()
{
var contents = userControl11.Contents;
}
}
You need to be able to have the user control tell the parent - no matter if the parent is a form or other user control - that the value of the text box has changed.
I created a user control called MyUserControl with a single text box called textBox1.
I added a TextBoxTextChanged event to MyUserControl and ended up with the following code:
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
public event EventHandler<string> TextBoxTextChanged;
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.TextBoxTextChanged?.Invoke(this, textBox1.Text);
}
}
Now, on Form1, I added the an instance of MyUserControl called myUserControl1 and added the handler for TextBoxTextChanged. I ended up with this code:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void myUserControl1_TextBoxTextChanged(object sender, string e)
{
this.label1.Text = e;
}
}
When I run that the text in the user control is automatically updated in the label as I type.
Where do you want to put the text box text? If it's in the title bar, do this:
formName.Text = yourTextBox.Text;
If it's in a label or another text box, do the same thing but use the control with its text property, and set it to the text box's text property.
I my game on the WF I have a main form (left) and it's child form (right). In the child form I can change value of the progress bar and it works fine. But I have the same progress bar on the main form and I want to update it too. I can't do it.
VIDEO
Code of the child form:
private MainForm mainForm = new MainForm();
// Button "Close"
private void button2_Click(object sender, EventArgs e)
{
mainForm.MyInitializeComponent(); // This code must update value on the main form
this.Close();
}
Code of the main form:
public void MyInitializeComponent()
{
label33.Text = indicators.Food + "%";
progressBar1.Value = indicators.Food;
}
If I'll open child form after I closed it, I see the changed value, ie it is stored. In both forms, the value is taken from a single variable.
Indicators.cs:
public sealed class Indicators
{
public Indicators()
{
Indicators.food = 75;
}
private static int food;
public int Food
{
get { return food; }
set { food = value; }
}
}
Have the main form add a form closed handler to the child form to update it's UI when the child is closed:
var childForm = new ChildForm();
childForm.FormClosed += (s, args) => MyInitializeComponent();
//...
childForm.Show();
I have 2 forms. The second form is opened from a method in the first form and I wish to be able to update the textbox that exists within that second form.
Basically I have the following code:
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI();
sendOutGUI.Show();
sendOutGUI.sendConsoleTextBox.Text = "Test";
}
When I press the button the second form (SendConsoleGUI form) opens but "Test" is never added to its textbox.
Am I doing something wrong here?
You need to use invoke method.
sendOutGUI.Invoke((MethodInvoker) delegate { sendOutGUI.sendConsoleTextBox.Text = "Test"; });
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI("Test");
sendOutGUI.Show();
}
}
public partial class ChildForm : Form
{
public ChildForm(string str)
{
InitializeComponent();
sendConsoleTextBox.Text = str;
}
}
This will work for you if you only wish to update it when the ChildForm is initially created.
form A opens form B, and form A.visible = false;
form A has a public int variable and I need controls in form B to be able to access and modify this variable. could this be done as passing the value through the constructor is only one way!
and so if it could be done, if form A is not visible could the value still be accessed?
(form b is not supposed to be dialog!)
many thanks!
edited: I do not quite get the explanations actually. so far it is like that:
in form a:
//in global space
public int temp = 123;
//in form_load event
Form setup = new setup();
setup.Show();
this.Visible = false;
in form setup:
//in form_load event
textBox1.text = temp.toString();
//in button_press event
form a.temp = "456";
I hope I have explained my stance clearly!
First, have member field in form B of type form A:
private FormA parent;
Second, have such constructor in form B:
public FormB(FormA parent)
: this()
{
this.parent = parent;
}
Now when you create instance of form B, pass reference to the running form A instance:
FormB formB = new FormB(this);
formB.Show();
And you can access the public property through the parent field e.g.
//inside Form B code..
public Foo()
{
parent.PUblicProp = "Hello";
}
this is one way to pass values from one form to another form
public partial class Form2 : Form
{
public string MyProperty { get; set; }
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = MyProperty;
}
}
after that in the button click event handler in Form1
add the following code
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.MyProperty = "This is from Form1";
form2.Show();
}
}
Since your int variable is public then your controls in formB can access and modify it, formA visibility won't affect this it will be accessed disregarding the visibility of it:
FormA f = new FormA();
int newValue = f.yourintvariable;
And there are a lot of alternative ways like using constructor to send the variable in the FormA constructor and initialize it from FormB,
or you can define it as static but in this case will be one variable for all the instances of thid form