How can I change the image of a button from form 3 to form 2? I use those buttons to navigate between forms, and want to, when I click on button in form 3, to get back to form 2, to have changed the picture of button in form 2.
This is how I navigate between forms.
FORM2 BUTTON
private void button1_Click(object sender, EventArgs e)
{
Form3 m = new Form3();
m.Show();
this.Visible = false;
this.Hide();
}
FORM3 BUTTON
private void button14_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.Show();
this.Visible = false;
this.Hide();
}
In your button on FORM 3, do something like this:
private void button14_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.button1.BackgroundImage = Image.FromFile(#"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
m.Show();
this.Visible = false;
this.Hide();
}
In order for this to work, the button1 on your FORM 3 must be accessible. So go to FORM 3 and mark button1 as public.
Related
I have main form which has several buttons, each button opens new form. When i click button1, form1 opens and when i click button2, form2 opens but form1 goes back to the main form. I want functionality such that each new form opens over the parent form and the most recent form on the top.
This is my code
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.Show();
}
Each form has a topmost property, just set them to true
private void button1_Click(object sender, EventArgs e)
{
Form1 form = new Form1();
form.Show();
form.TopMost = true;
form.Activate();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form = new Form2();
form.Show();
form.TopMost = true;
form.Activate();
}
private void button3_Click(object sender, EventArgs e)
{
Form3 form = new Form3();
form.Show();
form.TopMost = true;
form.Activate();
}
I have the following code:
in form1
private void button6_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form5 = new Form5();
Form Form6 = new Form6();
Form4.Show();
Form5.Show();
Form6.Show();
}
in form5 i have a button that must close form4, form5 and form6. as following:
private void button2_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form6 = new Form6();
Form4.Close();
Form6.Close();
this.Close();
}
but Form4 and Form6 are still open!!!
private void button2_Click(object sender, EventArgs e)
{
Form4 form4 = (Form4) Application.OpenForms["Form4"];
Form5 form5 = (Form5) Application.OpenForms["Form5"];
form4.Close();
form5.Close();
}
To close in proper way you have to know what do you want to close. In your case you are creating new Forms on beggining, and another ones with the same names visible locally before trying to destroy them.
Form Form6 = new Form6(); are totally different in both of your clicked buttons. Then, if you want to make it more visible generate interesting forms in constructor method and put definition as class fields like.
public partial class Form1 : Form
{
Form form2; // be sure all componentes see all forms
Form form3;
Form form4;
public Form1()
{
InitializeComponent();
form2 = new Form(); // create new Forms
form3 = new Form();
var button = new Button();
button.Click += new EventHandler(button_Click); // tell the button what should be called when click
form4 = new Form();
form4.Controls.Add(button); // add button progrimicaly to form
}
void button_Click(object sender, EventArgs e)
{
form2.Hide(); // hide on click
form3.Hide();
form4.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
form2.Show(); // show on load
form3.Show();
form4.Show();
}
}
I have 2 forms
Form1
Form2
I have one button in Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.ShowDialog();
f2.Dispose();
}
but issue is while opening form it's bliking and diasparing
i have tried to use show() also but not solved the problem
If i have not used Disposed method then first time when run the form it appering and disappered but sencond time onward by clicking on button it's working fine...
In Form2_Load event i am using this two property
private void Form2_Load(object sender, EventArgs e)
{
this.RightToLeft = RightToLeft.Yes;
this.RightToLeftLayout = true;
}
Don't change the form layout while its loading. Change it before you launch. Remove the code from Form2_Load and put it in button1_Click:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 =new Form2();
f2.RightToLeft = RightToLeft.Yes;
f2.RightToLeftLayout = true;
f2.ShowDialog();
}
I would guess you want to show and close the form2 using the same button. And I doubt your initial problem description
"issue is while opening form it's bliking and diasparing"
I think form2 is not 'blinking' while opening, but is 'blinking' while you try to click the button again in form1
ShowDialog() will exit your execution after u called it. Mean, it will exit the execution after you click the button.
Thus, you should try Show() with conditional statement within the button click event
In form1.cs
bool flag = false;
Form2 frm2;
private void button1_Click(object sender, EventArgs e)
{
if (flag == false)
{
frm2 = new Form2();
frm2.Show();
frm2.Load += new EventHandler(frm2_Load);
frm2.FormClosed += new FormClosedEventHandler(frm2_FormClosed);
flag = true;
}
else
{
frm2.Close();
flag = false;
}
}
void frm2_Load(object sender, EventArgs e)
{
//set what ever properties you like
}
void frm2_FormClosed(object sender, FormClosedEventArgs e)
{
flag = false;
}
See also: A dialog disables all of the windows that your program displays
Remove this Property
this.RightToLeft = RightToLeft.Yes;
and run your form...
Try This :
private void button1_Click(object sender, EventArgs e)
{
using(Form2 f2 =new Form2())
{
f2.ShowDialog();
}
}
My mainForm contains two buttons(btnLoad & btnChange) and a panel
When the btnLoad is clicked, it loads the other forms(there are 5 different froms with different controlers) into the panel. Let me assume one of it named Form2 which contains a label(labelMessage)
My problem is, when I click the btnChange the following statement won't work.
f2.labelMessage.Text = "Button Change Clicked";
My codes are
// codes on mainFrom
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = new From2();
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
private void btnLoad_Click(object sender, EventArgs e)
{
panelDock.Controls.Clear();
Form f2 = new Form2();
f2.TopLevel = false;
panelDock.Controls.Add(f2);
f2.Show();
}
is this wrong?
Since Form2 is already shown you should use Application.OpenForms instead of creating a new instance of Form2
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)Application.OpenForms["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
From your comment that Form2 is in a panel you can try
private void btnChange_Click(object sender, EventArgs e)
{
Form2 f2 = (Form2)panel1.Controls["Form2"];
f2.labelMessage.Text = "Button Change Clicked"; //labelMessage's modifier is public
}
how to resize lable text form2 at run time in C# windows application?.. for example if click button in form1 to open form2 then how to change form2 label text at run time...normaly form2 label text "saran" but if you can click form1 button1 click automatically change to "SARAN"
Example code:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
You will either have to make the modifier public of the Textbox on form2 or you will have to create a method in form2 that will change the text on the form.
Something like
Example 1: textbox modifier is public
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.myTextBox.Text = "TADA";
form2.Show();
}
Example 2: public method on form2
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.SetMyTextBox("TADA");
form2.Show();
}