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);
Related
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.
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 am very new to C# so I am sorry if I use wrong terms. Here is my problem. I have 2 forms. and I want If I click the button in Form1, it shows up the Form2 by overlapping the Form1. And now, if I click the button in form2, I want the form1 overlap the form2. But I always get error "stackOverFlowException". Why did i get the error? How to resolve it? I am sorry if my question is not clear. I hope these picture can explain better about my question.
Here is the code for form1.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Form2 frm2 = new Form2();
private void Form1_Load(object sender, EventArgs e)
{
frm2.Show();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
frm2.Left += 10;
if (frm2.Left >= 750)
{
timer1.Stop();
this.TopMost = false;
frm2.TopMost = true;
timer2.Start();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
frm2.Left -= 10;
if (frm2.Left <= 535)
{
timer2.Stop();
}
}
}
}
and here is the code for form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
Form1 frm1 = new Form1();
private void Form2_Load(object sender, EventArgs e)
{
frm1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
frm1.Left += 10;
if (frm1.Left >= 750)
{
timer1.Stop();
this.TopMost = false;
frm1.TopMost = true;
timer2.Start();
}
}
private void timer2_Tick(object sender, EventArgs e)
{
frm1.Left -= 10;
if (frm1.Left <= 535)
{
timer2.Stop();
}
You are creating and endless chain of opened forms.
When you first create a Form1 object with new Form1(), the initialization code inside this form
Form2 frm2 = new Form2();
... is called. This in turn triggers the initialization code of Form2, which is
Form1 frm1 = new Form1();
This creates a new instance of this form (now you have two Form1 instances) and calls Form2 frm2 = new Form2(); again in this new instance (now you have two Form2 instances). This goes on until thousands of form objects have been created and the stack overflows.
What can you do about it?
In Form2 add a parameter to the constructor that allows you to pass a reference to the first form
private readonly Form1 _frm1;
public Form2 (Form1 frm1)
{
InitializeComponent();
_frm1 = frm1;
}
In Form1 do this
private Form2 _frm2;
private void Form1_Load(object sender, EventArgs e)
(
_frm2 = new Form2(this);
)
I.e. Form2 never creates a Form1. Instead, it gets a reference to the already opened form.
How do you bring a form to front?
The TopMost property controls the behavior of a form when it is opened. Instead, use
_frm1.BringToFront();
to change the z-order afterwards.
Can someone explain why my new form doesn't appear? The old form closes but it isn't replaced by my new one:
namespace Pong
{
public partial class Menu : Form
{
public Menu()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
}
private void PlayButton_Click(object sender, EventArgs e)
{
PongForm form = new PongForm();
form.Show();
this.Close();
}
private void ExitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Is "Menu" the startup form for your application? If yes, then when it closes the entire application closes.
One solution would be to hide the current form, display "form" with ShowDialog(), then call Close():
private void PlayButton_Click(object sender, EventArgs e)
{
this.Visible = false; // hide the current form
Application.DoEvents();
PongForm form = new PongForm();
form.ShowDialog(); // code stops here until PongForm is dismissed
this.Close(); // now close the form (and presumable the whole app)
}
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();
}