ShowDialog doesn't return DialogResult - c#

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.

Related

How to change image of a picturebox from another form?

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.

How can you hide a button from another form

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.

How to pass data from another form and back

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);
}

Passing Data form two different forms

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();
}

Closing event handlers C#

I have two forms, Form 2 is inheriting from Form 1.
What I need to do is that when I close both Form 1 and Form 2 another form which asks if user is sure to quit appears. Then if user clicks Yes, another form which asks if the user wants to save the game appears if and only if the form which the user closes is Form 2 and not Form 1 since for Form 1 there is no saving necessary.
This is what I managed to do:
// These are the Form 1 closing and closed event handlers:
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
SureClose sc = new SureClose();
sc.StartPosition = FormStartPosition.CenterScreen;
sc.Show();
}
private void GameForm_FormClosed(object sender, FormClosedEventArgs e)
{
MainMenu menu = new MainMenu();
menu.Show();
}
Then in Sure Close: // Please note that Tournament is Form 2 inheriting from GameForm (Form 1)
private void yesButton_Click(object sender, EventArgs e)
{
this.Hide();
if (GameForm.ActiveForm is Tournament)
{
SaveGame sg = new SaveGame();
sg.StartPosition = FormStartPosition.CenterScreen;
sg.Show();
}
else
{
GameForm.ActiveForm.Close();
}
}
private void noButton_Click(object sender, EventArgs e)
{
this.Hide();
}
// This is the SaveGame Form:
private void saveButton_Click(object sender, EventArgs e)
{
// Still to do saving!
}
private void dontSaveButton_Click(object sender, EventArgs e)
{
this.Hide();
GameForm.ActiveForm.Close();
}
The problem is that when in the yesButton event handler in SureClose Form I have GameForm.ActiveForm.Close(), this is going back to the GameForm Closing event handler therefore the SureClose dialog is appearing again.
I tried doing: if (e.CloseReason() == CloseReason.UserClosing)
but obviously it doesn't work either because the reason of closing will always be the user :/
How can I solve this?
Thanks a lot for any help !
Form1 :
private void GameForm_FormClosing(object sender, FormClosingEventArgs e)
{
if(SureClose())
{
SaveChanges();
}
else
{
e.Cancel = true;
}
}
private bool SureClose()
{
using(SureClose sc = new SureClose())
{
sc.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sc.ShowDialog();
if(result == DialogResult.OK)
{
return true;
}
else
{
return false;
}
}
}
protected virtual void SaveChanges()
{
}
Form2:
protected override void SaveChanges()
{
using(SaveGame sg = new SaveGame())
{
sg.StartPosition = FormStartPosition.CenterScreen;
DialogResult result = sg.ShowDialog();
if(result == DialogResult.OK)
{
//saving code here
}
}
}
SureClose form and SaveGame form:
private void yesButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void noButton_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.Cancel;
}

Categories