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 :)
Related
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
}
I am programming something in c# for school. I have 2 Forms and while I close the child form I want to draw something on the parent Form and I don't want to use any button I would have to click afterwards. This is what I have tried but it doesn't work.
private void button1_Click(object sender, EventArgs e)
{
this.Close(); //Closes Form2(Child)
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
Graphics g;
g = f1.CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
}
I also have tried using the FormClosed event.
On your parent form, when you initialize the child form, set it's Owner property to this:
var form2 = new Form2();
form2.Owner = this;
And then from the child form, you can access the parent by doing this:
Graphics g;
g = ((Form1)this.Owner).CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
Taking in consideration your Form1 is the parent form and Form2 is your child form, that is Form2 is being called from Form1 originally. Then you should first send your copy of Form1 to the new Form2 you are creating, stored it on a private field and on closing use it to draw on it. Something similar to:
private Form1 _parent;
public Form2(Form1 parent)
{
_parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
this.Close(); //Closes Form2(Child)
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Graphics g = _parent.CreateGraphics();
g.DrawRectangle(Pens.Black, 100, 100, 50, 50);
}
Also on parent, you should have something similar to:
private void button1_Click(object sender, FormClosingEventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
The problem with the code you posted is that you are creating a new Form1, which is not the same object/instance as the original Form1 you have already when opening a new instance Form2.
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.
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
Is there any code like, frmBallloon is shown in btnShow click event of the frmBase and again I want to show frmBase if user clicks on the btnShow of the frmBalloon then there is two copy of the frmBase.I want only one copy.Then how can I alter the form view by clicking on the button.
Try this
frmBase button click
Form2 frm2 = new Form2();
this.Visible = false;
frm2.Show(this);
And here frmBalloon Button Click
if (this.Owner != null)
{
this.Visible = false;
this.Owner.Show();
}
You can try something like this
Code for Form1
public Form2 f2;
private void button1_Click(object sender, EventArgs e)
{
if (f2 == null)
{
f2 = new Form2 {f1 = this};
f2.Show();
}
else
f2.Focus();
}
Code for Form2
public Form1 f1;
private void button1_Click(object sender, EventArgs e)
{
if (f1 == null)
{
f1 = new Form1 {f = this};
f1.Show();
}
else
f1.Focus();
}