How can I change the image of a form PictureBox, from another form?
Example. Form2 image needs to change Form1 image.
I tried this.
exmp.Code 1
//This is for Form1
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Form2 Fr2 = new Form2();
Fr2.Owner = this;
}
private void pictureBox2_Click(object sender, EventArgs e)
{
Fr2.Show();
}
//This is for Form2
private void pictureBox2_Click(object sender, EventArgs e)
{
try
{
(this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
}
catch
{
}
}
This code doesn't work!
But if I try this it will work.
exmp.Code 1
//Form1
private void pictureBox1_Click(object sender, EventArgs e)
{
Form2 Fr2 = new Form2();
Fr2.Show();
Fr2.Owner = this;
}
//Form2
private void pictureBox2_Click(object sender, EventArgs e)
{
try
{
(this.Owner as Form1).pictureBox1.Image = pictureBox2.Image;
}
catch
{
}
}
But I cannot use the second code because I need to click a ContextMenuStrip to open Form2.
Any question write in comments!
In Form1, "Fr2" needs to be declared at Form level so you can access it from both "pictureBox1_MouseEnter" and "pictureBox2_Click". Set the Owner by passing "this" to Show().
This is for Form1:
private Form2 Fr2;
private void pictureBox1_MouseEnter(object sender, EventArgs e)
{
Fr2 = new Form2();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (Fr2!=null && !Fr2.IsDisposed)
{
Fr2.Show(this); // <-- pass Form1 as the Owner
}
}
This is for Form2:
private void pictureBox2_Click(object sender, EventArgs e)
{
Form1 Fr1 = this.Owner as Form1;
if (Fr1!=null)
{
Fr1.pictureBox1.Image = pictureBox2.Image;
}
}
To access "pictureBox1" in Form1, though, you'd have to change the Modifiers property of it to public.
Related
I have a C# project that has 2 forms. The first has 3 buttons. I need to be able from the second form to hide 2 (button1 and button2 )buttons with a checkbox, and I don't know how to call the buttons from the first form.
this is form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
}
}
and this is Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
?????????
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
Another option is to pass the form as the "owner" in the Show() command:
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this); // pass Form1 reference in to our instance of Form2
}
In Form2, cast the Owner property back to Form1 so you can access it (assuming you've changed the modifiers property of the buttons to public as already suggested):
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false; // or whatever your buttons are called
}
}
This is almost exactly what I had posted previously...you need to change the Modifiers property of the buttons so they are public and can be seen from Form2.
this is the final version that works in my case thanks to those who answered my question and helped me to get this answer
Form1
namespace test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Form2 frm = new Form2();
frm.checkBox1.Checked = Properties.Settings.Default.checkB;
if (frm.checkBox1.CheckState == CheckState.Checked)
{
button1.Visible = false;
}
}
private void button3_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show(this);
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
}
}
}
Form2
namespace test1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
checkBox1.Checked = Properties.Settings.Default.checkB;
if (checkBox1.CheckState == CheckState.Checked)
{
Form1 f1 = (Form1)this.Owner;
f1.button1.Visible = false;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Properties.Settings.Default.checkB = checkBox1.Checked;
Properties.Settings.Default.Save();
}
}
}
The buttons and checkBox are set to Modifiers - Public
you need to make the buttons on the first form public and then you can access them once you create a instance of the first form you will need to pass that form to the second form.
Might want to look into building an event that fires from one form and gets handled by the other form to disable the button.
I have 6 forms. 3 forms are for information which are forms 4-6 these forms function is just a basic open and close. Then there are forms 1-3 all have menu strip which connects to forms 4-6. Form 1 is the initial page which opens either form 2 or 3 depending on what button you click. Form 2 opens form 3 upon button click and form 3 has an option to open form 2.
My problem now is I have a null reference exception everytime I open form 2 from form 3.
Form 2 codes:
Form3 game = new Form3();
Form4 oneplayer = new Form4();
Form5 twoplayer = new Form5();
Form6 creditpage = new Form6();
public Form2()
{
InitializeComponent();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void onePlayerToolStripMenuItem1_Click(object sender, EventArgs e)
{
oneplayer.Show();
}
private void twoPlayerToolStripMenuItem1_Click(object sender, EventArgs e)
{
twoplayer.Show();
}
private void creditToolStripMenuItem_Click(object sender, EventArgs e)
{
creditpage.Show();
}
private void button1_Click(object sender, EventArgs e)
{
this.Hide();
game.Show();
}
Form 3 codes:
Form2 choice;
Form4 oneplayer = new Form4();
Form5 twoplayer = new Form5();
Form6 creditpage = new Form6();
public Form3()
{
InitializeComponent();
}
private void twoPlayerToolStripMenuItem_Click(object sender, EventArgs e)
{
choice.Show();
this.Close();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void onePlayerToolStripMenuItem1_Click(object sender, EventArgs e)
{
oneplayer.Show();
}
private void twoPlayerToolStripMenuItem1_Click(object sender, EventArgs e)
{
twoplayer.Show();
}
private void creditToolStripMenuItem_Click(object sender, EventArgs e)
{
creditpage.Show();
}
New to this. Please forgive me :P
You aren't setting the form to anything:
Form2 choice; // <===== Here
Form4 oneplayer = new Form4();
Form5 twoplayer = new Form5();
Form6 creditpage = new Form6();
You need to create a method in form3 like this:
public void setForm2 (Form2 choice_)
{
choice = choice_;
}
and add this line to your button1.click
game.setForm2(this);
I've been trying to get this working. I'm sure I've written in correctely
Form 1
private string _temp;
public string Temp
{
get { return _temp; }
set { _temp = value; }
}
private void button1_Click(object sender, EventArgs e)
{
_temp = "test";
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
Form1 main = new Form1();
MessageBox.Show(main.Temp);
}
You have to get the reference to the Form1 instance via the sender parameter (assuming that Form2's button1_Click handler is attached to a button on Form1):
//Form2
private void button1_Click(object sender, EventArgs e)
{
Button button = (Button) sender;
Form1 main = (Form1) button.FindForm();
MessageBox.Show(main.Temp);
}
My objective is to be able to enter a value in the textbox in form1 then press enter (when pressing the enter button the value will be pass to a method called setvalue. When the switch button is pressed then the form one will hide and open form2. Form2 has two buttons, show and exit. When show is clicked i need to display a messagebox that display the data in textbox in form1 by calling the getvalue method.
Please I'm open for ideas.
This is form one
public partial class Form1 : Form
{
private int secretValue;
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Visible = true;
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
this form two
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Add a constructor value on your form 2. It should look like this:
public Form2(int secValue)
Then you can call it on your form 1 by passing the value
Form2 frm2 = new Form2(secretValue);
Maybe you can assign it to a global variable in your form 2, so that it can be referenced by your code in all of the form. Your form two can now be:
public partial class Form2 : Form
{
int passedValue = 0;
public Form2(int secValue)
{
InitializeComponent();
passedValue = secValue;
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
MessageBox.Show(string.Format(passedValue.ToString(), "My Application", MessageBoxButtons.OK));
}
}
In my opinion, there's no need for the property you created. Always remember, the values you assign to a class will be null if you change from form to form.
Following code will not work becuase you are creating new instance of form1 and trying to get the value from there, It will only have the default vaue of int.
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = frm1.GetValue();
}
Simply what you can do is to have a public property defined within Form2. and set the value of this property before showing Form2,
private void button2_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//settting the value to Form2's property SecrectValueOfForm2, Now this value is available within Form2
frm2.SecrectValueOfForm2 = ValueInForm1;
frm2.Visible = true;
this.Hide();
}
Plz try code as shown below:
public partial class Form1 : Form
{
private int secretValue;
Form2 frm2 = new Form2();
public void SetValue (int value){
secretValue = value;
}
public int GetValue ()
{
return secretValue;
}
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
frm2 .Show();
this.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
frm2.Owner = this;
}
private void btnEnter_Click(object sender, EventArgs e)
{
secretValue = Convert.ToInt32(txtInput.Text);
SetValue(secretValue);
}
}
And On Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
Environment.Exit(0);
}
private void btnShow_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
int val = ((Form1 )this.Owner).GetValue();
MessageBox.Show(string.Format(val.ToString(), "My Application", MessageBoxButtons.OK));
}
}
Form2
public Int32 _txtval { get; set; }
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(_txtval.ToString());
}
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2._txtval = Convert.ToInt32(textBox1.Text);
frm2.Visible = true;
this.Hide();
}
I made a form2 that shows and there are buttons which return DialogResult but I have no idea why this doesn't work:
Form1:
private void buttonEvent_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
if (form2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
labelEvent.Text = hEvent.GetName; //Breakpoint here but it doesn't stops!
}
Form2:
String Name;
public String GetName
{
get { return Name; }
}
private void button1_Click(object sender, EventArgs e)
{
button1.DialogResult = DialogResult.OK;
this.Close();
}
I think you should use
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
Just set button1 to the AcceptButton on the Form object. You won't need a single line of code.