How to pass variable value between identical instances of WindowsForm - c#

I have a base form From1. In the form when button is clicked, new form From2 is created.
private void Button_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
}
User can create several instances of From2. In From2 user can set a value in textBox and click a button. Once it is clicked, value from textBox has to be somehow transferred to all other created instances of From2. How can I do that?

Step 1: remember all forms you created.
private void Button_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
allForms.Add(f2); // remember it
f2.Show();
}
Step 2: when the value changes, update all remembered forms
private void textbox1_TextChanged(object sender, EventArgs e)
{
foreach (Form2 form in allForms)
{
form.MyValue = textbox1.Text;
}
}
Just write the code like that, then let the IDE help you creating the properties and adjust the visibility accordingly, e.g. have the IDE help you implement a property in Form2 that sets the text
string MyValue
{
set
{
anotherTextbox.Text = value;
}
}
You'll then notice that you need some more stuff, probably.
Step 3: remove the form from the list when it is closed.
private void Button_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
allForms.Add(f2);
f2.Closed += OnClose; // Method to be called when form is closed
f2.Show();
}
private void OnClose(object sender, EventArgs e)
{
Form2 form = (Form2) sender;
form.Closed -= OnClose; // Unregister event handler
allForms.Remove(form); // remove it
}

Related

Disabling a button when a form is loaded and enabling when the form is closed

I want to disable a button (button3) on a primary form when a second, modeless form (Form2) is loaded, and then re-enable the button when the modeless form is closed.
Here is what I've tried:
private void button3_Click(object sender, EventArgs e)
{
Form2 p = new Form2(label1.Text);
p.Show();
if (p.Shown)
this.button3.Click += new System.EventHandler(this.button3_Click);
else
this.button3.Click -= new System.EventHandler(this.button3_Click);
}
The best method for achieving this would be to disable button3 prior to showing Form2, and using the FormClosed event on Form2 to re-enable button3 once the form is closed:
public partial class Form1 : Form
{
...
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form and assign the FormClosed event
var form = new Form2(label1.Text);
form.FormClosed += Form2_FormClosed;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
// Occurs when Form2 is closed
private void Form2_FormClosed(object sender, FormClosedEventArgs e)
{
// Re-enable button3
button3.Enabled = true;
}
}
An alternative method, that assigns a lambda expression to the FormClosed event:
private void button3_Click(object sender, EventArgs e)
{
// Instantiate the form
var form = new Form2(label1.Text);
// Assign a lambda method to the FormClosed event to re-enable button3
form.FormClosed += (s, a) => button3.Enabled = true;
// Disable button3
button3.Enabled = false;
// Show the form
form.Show();
}
I did a similar thing, i m not sure to understand what you want to do but maybe it will help you.
public partial class Form2 : Form
{
private void button3_Click(object sender, EventArgs e)
{
Button3.Enabled = false;
Form2 p = new Form2(label1.Text);
p.ShowDialog();
//the code will stop here until you finish your work on form2
Button3.Enabled=true;
}
it works with me.
But if your form3 is just a short label use:
Button3.Enabled= false;
MessageBox.show ("blabla");
Button3.Enabled=true;

Set control visible from other form

I set the button visible property to false of Form2. How I will make the button(Form2) visible when I click a button(a button that also opens Form2) from Form1.
I tried this :
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.button1.Visible = true;
f2.button1.Location = new Point(200, 200);
}
Create a method in Form2
public void setButton1Visible(boolean flag){
this.button1.Visible = flag;
}
You cannot access the button directly from Form1. (Actually you can,but it is not right way to solve it.
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
f2.setButton1Visible(true);
}
I think button1 is declared as private. Your code will work if you declare button1 as public.
public System.Windows.Forms.Button button1;
Imagine you control is in form 1. Set corresponding control "modifiers = public" from control property window
Form 1
private void ShowForm2_Click(object sender, EventArgs e)
{
Form2 NewForm = new Form2();
NewForm.Owner = this;
NewForm.Show();
}
In Form 2
private void ChangeProperty_Click(object sender, EventArgs e)
{
(this.Owner as Form1).MyButton.Visible = false;
}
//while doing this Control In Form1 will be hidden :)

a problem with bringing form in front

I a form than I can open it also by pressing F1 and clicking so in this code first I check if the form has been opened or not.if was opened I just want to bring it to front. my problem is it wont bring in front by pressing F1 or clicking ToolStripMenuItem if i open it befor.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
Form2 form2;
form2 = new Form2();
if (e.KeyCode.ToString() == "F1" && Application.OpenForms.OfType<Form2>().Count() <= 0)
// the user pressed the F1 key
form2.Show();
else
{
form2.TopMost = true;
form2.BringToFront();
form2.Focus();
form2.TopMost = false;
}
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2;
form2 = new Form2();
if (Application.OpenForms.OfType<Form2>().Count() <= 0)
form2.Show();
else
{
form2.TopMost = true;
form2.BringToFront();
form2.Focus();
form2.TopMost = false;
}
}
It doesn't work because you forgot to call form2.Show() in the else clause. The code is wrong, you don't want to create a new instance of Form2 if one already exists. Also, there's a bug in Winforms that makes Application.OpenForms lose track of form instances.
Best thing to do is to explicitly keep track of the lifetime of the form with its FormClosed event handler so you don't have to find it back later:
Form2 form2;
private void showForm2() {
if (form2 == null) {
form2 = new Form2();
form2.FormClosed += delegate { form2 = null; };
}
form2.Show();
form2.Focus();
}
Call showForm2 from your event handlers.
Just call form2.Show() and BringToFront. You should declare the `new Form2()' once though.
private Form2 form2;
private void ShowForm2()
{
if (form2 == null)
{
form2 = new Form2();
form2.FormClosed += delegate { form2 = null; };
}
form2.Show();
form2.BringToFront();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
ShowForm2();
}
private void ToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowForm2();
}
From Hans' code, I added the delegate to handle null problem on closing the form and opening it again.

how to prevent opening a form multiple times in c#

i have created an application in which a menustrip is present in which 2 buttons are there, one for ADD, and another for UPDATE & both controls are in a single form, means a button of add & update is there in a single form, whenever i press add button in menustrip update button will be disabled, and when i press update on menustrip the add button will disable. how to do this? i m doing this by show method but that form is opening multiple times using show().
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button1.Enabled = true;
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form1 f2 = new Form1();
f2.MdiParent = this;
f2.Show();
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
you simply have to use a single form in this case. try using the singleton approach -
http://hashfactor.wordpress.com/2009/03/31/c-winforms-create-a-single-instance-form/
try using .ShowDialog() instead .Show() and no other form will be able to be clicked on until that one closes.
To do that you'll need to have an instance of that Form outside of those methods that you dismply show if the Form has already been created, or create and show it if it has not (this is the singleton pattern). Here's an example:
Form1 f2 = null;
private void addRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new Form1();
f2.MdiParent = this;
f2.button1.Enabled = true;
}
f2.Show();
}
private void updateRecordsToolStripMenuItem_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2.MdiParent = this;
f2.button2.Enabled = true;
f2.button1.Enabled = false;
}
f2.Show();
}
One question on your disabling of the menu items though, how do you plan on re-enabling them after they have been disabled?
just try to check that form is already opened or not by using its Text Property.. if it is opened just focus on that form other wise show that form as normally
private void button1_Click(object sender, EventArgs e)
{
bool IsOpen = false;
foreach (Form f in Application.OpenForms)
{
if (f.Text == "Form1")
{
IsOpen = true;
f.Focus();
break;
}
}
if (IsOpen == false)
{
Form f1 = new Form1();
f1.Show();
}
}
Try This Guys Its Simple

change form background image through a button click

I want to change the form's back ground image when I click the button. I'm stuck at this error. It says:
An object reference is required for
the non-static field, method, or
property
'System.Windows.Forms.Control.BackgroundImage.get'
private void pictureBox1_MouseHover(object sender, EventArgs e)
{
pictureBox1.Location = new Point(25, 9);
}
private void pictureBox1_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Location = new Point(18, 9);
}
private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
{
Form1.BackgroundImage =
}
On the last part of the code, you can see that I am attempting to change the background image of the form. but it does not allow me and I don't know how to do it properly.
Use this instead of Form1:
this.BackgroundImage = ...
Form1 is a Type, not an Instance of an Object, you're looking for this.
if you want to use Form1 use this:
Form1 f1 = new Form1();
f1.BackgroundImage = ...

Categories