How can I refresh an open form from another form?
For example:
Form 1
Label (Modifiers = Public)
Button (To show Form 2)
Form 2
Text Box (Enter value for Label and display it on label)
Button (Sends value to Label)
I've notice that after I entered value in text box, the label is not updating after I closed form 2.
For C# Winforms, this is how I'd do it.
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 oForm = new Form2();
oForm.ChangeLabelText += ChangeLabelText;
oForm.Show();
}
private void ChangeLabelText(object sender, EventArgs e)
{
string sText = sender as string;
label1.Text = sText;
}
Form 2:
public partial class Form2 : Form
{
public event EventHandler ChangeLabelText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string sText = textBox1.Text;
ChangeLabelText(sText, null);
}
}
what if I use this method? Is there any disadvantage? or it's fine?
Form 1: Modifiers of label1 is Public
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 oForm = new Form2();
oForm.Owner = this;
oForm.Show();
}
Form 2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
(this.Owner as Form1).label1.Text = textBox1.Text;
}
}
Related
I can't update the first form of my application. when it opens it loads all the elements, then through a button I open a second form and from that, with a button I should reload all the controls of the first form including form1.text but this does not happen. despite the marker I saw that the text variable is updated correctly, however on a graphic level it does not change.
form1:
public partial class Form1 : Form
{
public string mail { get; private set; }
public Form1()
{
InitializeComponent();
}
public void Form1_Load(object sender, EventArgs e)
{
this.Text = "table - Last Update: " + DateTime.Now.ToString();
...some other code...
}
public void updateform()
{
this.Controls.Clear();
InitializeComponent();
Form1_Load(null, null);
this.Refresh();
}
private void button1_Click(object sender, EventArgs e)
{
mail = lbl1.Text;
Form2 form2 = new Form2(mail);
form2.Show();
}
}
form2:
public partial class Form2 : Form
{
public Form2(String stringa)
{
InitializeComponent();
email = stringa;
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.updateform();
this.Close();
}
You can pass a reference to Form1, into Form2 using the Show() command. The reference can be accessed using the .Owner property.
In Form1:
private void button1_Click(object sender, EventArgs e)
{
mail = lbl1.Text;
Form2 form2 = new Form2(mail);
form2.Show(this); // <-- pass reference to Form1
}
In Form2:
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = this.Owner as Form1; // <-- attempt to convert .Owner to Form1
if (f1 != null)
{
f1.updateform();
}
this.Close();
}
These are my 2 Forms.
These are the codes for Form 1-->
namespace Passing_Values
{
public partial class Form1 : Form
{
string a="preset value";
public Form1()
{
InitializeComponent();
}
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2().Show();
}
public void set(string p)
{
MessageBox.Show("This is Entered text in Form 2 " + p);
a = p;
MessageBox.Show("a=p done! and P is: " + p + "---and a is: " + a);
textBox1.Text = "Test 1";
textBox2.Text = a;
textBox3.Text = p;
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(a);
}
}
}
These are the codes for Form 2-->
namespace Passing_Values
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
Form1 j = new Form1();
j.set(g);
}
}
}
See the picture.You can understand the design.
This is what I want to do. 1st I open Form2 using button in Form1. Then I enter a text and click the button("Display in Form1 Textbox"). When it's clicked that value should be seen in the 3 Textboxes in Form1.I used Message Boxes to see if the values are passing or not. Values get passed from Form2 to Form1. But those values are not displays in those 3 Textboxes but the passed values are displayed in Message Boxes. Reason for the 3 Text Boxes can be understood by looking at the code. So what's the error?
Actually I have an object to pass. So I did this
in form1-->
private void btnOpenF2_Click(object sender, EventArgs e)
{
new Form2(this).Show();
}
in form2-->
public partial class Form2 : Form
{
Form1 a;
public Form2(Form1 b)
{
a = b;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string g;
g = textBox1.Text;
a.set(g);
this.Close();
}
}
I would simply pass it in the constructor.
So, the code for form2, will be:
public partial class Form2 : Form
{
string _input;
public Form2()
{
InitializeComponent();
}
public Form2(string input)
{
_input = input;
InitializeComponent();
this.label1.Text = _input;
}
}
And the call in Form1 will be:
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2(this.textBox1.Text.ToString());
fm2.Show();
}
public partial class Form1 : Form
{
Form2 fm2;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
fm2 = new Form2();
fm2.Show();
fm2.button1.Click += new EventHandler(fm2button1_Click);
}
private void fm2button1_Click(object sender, EventArgs e)
{
textBox1.Text = fm2.textBox1.Text;
}
}
And code in form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
set modifier property of textbox1 and button1 to public
Place a static string in your Form2
public static string s = string.Empty;
and, in your Display in Form1 Textbox button click event, get the value from the textbox in your string s:
s = textBox1.Text;
Form1 f1 = new Form1();
f1.Show();
once, the Form1 is showed up again, then in the Form1_Load event, just pass your Form2's text value to your Form1's textboxes, the value of which was gotten by the variable s:
foreach (Control text in Controls)
{
if (text is TextBox)
{
((TextBox)text).Text = Form2.s;
}
}
Sorry, I know this question, or similar has been asked frequently, but as I've gone through different threads, I just don't know how to apply it to my program.
Here's my situation:
In Form 1, I have a label. There's a button that opens Form 2, which has radiobuttons and a button. The button in Form 2 should send a string value from the radio button, to the label.Text in form 1. How can I go around in doing so?
So, below is what opened form 2.
private void selectkeyButton_Click(object sender, EventArgs e)
{
selectKeyboard sk = new selectKeyboard();
sk.ShowDialog();
}
And in Form 2, here's what i have so far:
public Form1 otherForm = new Form1();
string hotkey = "";
public void hotkeyChanged(object sender, EventArgs e)
{
RadioButton rr = (RadioButton)sender;
switch (rr.Name)
{
case ("buttonF1"):
hotkey = "F1 ";
break;
}
}
public void buttonConfirmKey_Click(object sender, EventArgs e)
{
hotkey = otherForm.keyLabel.Text;
this.Close();
}
Where I have public Form1 otherForm = new Form1();
and hotkey = otherForm.keyLabel.Text; I found it here.
And it doesn't seem to be working, as when I press the button on form2, the form closes but the label in form1 doesn't change...
any ideas?
thanks
There are different approaches to do this. You could go like this :
Solution one:
(Don't forget to set the modifier for you label1 in this case to Public. You can set this in the designer options > under Properties > design)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
}
Form 2:
public partial class Form2 : Form
{
private readonly Form1 _parent;
public Form2(Form1 parent)
{
InitializeComponent();
_parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
_parent.label1.Text = textBox1.Text;
Close();
}
}
Solution 2
Instead of setting label1 to public, leave it on private (as default) but set the DialogResult property of button1 on form 2 to "DialogResult OK" (under Properties > Behavior)
Form 1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
DialogResult res = frm.ShowDialog();
if (res == DialogResult.OK)
{
label1.Text = frm.MyNewText;
}
}
}
Form 2:
public partial class Form2 : Form
{
public string MyNewText;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyNewText = textBox1.Text;
Close();
}
}
in the constructor of the forms you can get the values like this :
in form2 you should add a constructor like this :
public partial class Form2: Form
{
public string _newvalue
public Form2(string value)
{
InitializeComponent();
_newvalue=value
}
//you should assign the value to the label .
}
in form1 you should do this :
form2 new=form2("sampletext");
new.showdialog();
Solution 1:
In Form1:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.ShowDialog();
}
Codes in Form2:
Form frm_;
public Form2(Form frm)
{
InitializeComponent();
frm_ = frm;
}
private void btnInForm2_Click(object sender, EventArgs e)
{
Label lbl = (Label)frm_.Controls.Find("lblInForm1", true)[0];
string PassVal="What you want";
lbl.Text = PassVal;
}
Solution 2:
in Form 1:
Form2 f = new Form2();
if (f.ShowDialog() == DialogResult.OK)
{
lblInForm1.Text = f.PassVal;
}
in Form 2:
internal string PassVal = "";
PassVal is a Field.
How to access a textBox in form1 when I click a button from form2?
I want to write a specific text in textBox in form1 after I click a button from form2 and it closes itself.
As I guessed you can solve your issue as
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
LaunchForm2();
}
private void LaunchForm2()
{
using (var form2 = new Form2())
{
form2.OnTextEnteredHandler += Form2_OnTextEnteredHandler;
form2.ShowDialog();
}
}
private void Form2_OnTextEnteredHandler(string text)
{
//This event will be fire when you click on button on form2
textBox1.Text = text;
}
}
Form2.cs
public partial class Form2 : Form
{
public delegate void TextEnteredHandler(string text);
public event TextEnteredHandler OnTextEnteredHandler;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (OnTextEnteredHandler != null)
{
OnTextEnteredHandler(textBox1.Text);
Close();
}
}
}
You need to be add textbox in form 2 as well, put text into it from form 2 then click button as shown in code.
i already worked on connecting a tool control to another1 using this code:
--- Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2(this);
frm.Show();
}
public string LabelText
{
get { return Lbl.Text; }
set { Lbl.Text = value; }
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
--- Form2.cs
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private Form1 mainForm = null;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
this.mainForm.LabelText = txtMessage.Text;
if (timer1.Enabled == true)
{
int line = 1 + richTextBox1.GetLineFromCharIndex(richTextBox1.GetFirstCharIndexOfCurrentLine());
int column = 1 + richTextBox1.SelectionStart - richTextBox1.GetFirstCharIndexOfCurrentLine();
txtMessage.Text = "line: " + line.ToString() + " , column: " + column.ToString();
}
}
}
***** output was**
text of label from Form2 was connected into Form1 .
so its already fixed .
now my problem was is there a way i can do the same way for void function?
i mean for example:
in Form1, i got 1button with a control inside of:
richTextBox1.Copy();
then this control will be for richTextBox1 on Form2 .
(which will copy the selected text in richtextbox on Form2)
is that possible? really need a help .thanks a lot in advance!
Here's something to get you started:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm2 = new Form2(this);
}
}
And make sure that richTextBox1 is declared public.
And:
public partial class Form2 : Form
{
Form1 sendingForm;
public Form2(Form1 frm1)
{
InitializeComponent();
sendingForm = frm1;
}
private void button1_Click(object sender, EventArgs e)
{
Text = sendingForm.richTextBox1.Text;
}
}
What's done here is: Initializing the Form2 instance with a reference to the sender Form1 instance, and using that reference to get to the RichTextBox.
EDIT:
Maybe (!) this is what you're looking for:
mainForm.richTextBox1.Copy();
You'd move your declaration of Form2 out to Class level:
--Form1
Form2 frm = null;
private void button1_Click(object sender, EventArgs e)
{
frm = new Form2(this);
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
if (frm != null)
{
frm.CopyRichTextBox();
}
}
--Form2
public void CopyRichTextBox()
{
this.richTextBox1.Copy();
}