Closing a background form from a custom message box - c#

Brief description:
Cannot close form1 by clicking a button that is on form2.
I have the following forms:
form1
formMessageBox which has button1
form3
I want to:
Pop up formMessageBox from form1 (this is working ok) then from formMessageBox, click button1 to close form1 and open form3.
I have used a variety of techniques however form1 cannot be targeted.
Help much appreciated.
Edit
When form1 and the messagebox are open at the same time. Form1 does not close or hide when button1 is clicked to try and close form1.

Not really sure what you're looking for, but I think you are needing something like this.
Say you have Form1 and you pop up a formMessageBox using ShowDialog().
public class Form1 : Form
{
if (somethingHappened)
{
var formMessageBox = new FormMessageBox();
//ShowDialog() sets the formMessageBox object as a modal dialog owned by Form1
DialogResult result = formMessageBox.ShowDialog();
//Assuming you have OK buttons or something like that in formMessageBox
if (result == DialogResult.OK)
{
var form3 = new Form3();
/*Can't use this.Close() here as it would close the entire application
Just hide it and don't forget to dispose of it later when you actually
want the application to close*/
this.Hide();
form3.Show();
}
}
}
Then in your formMessageBox code, just set the DialogResult according to what occurs:
public class FromMessageBox : Form
{
private void okButton_Click(object sender, EventArgs e)
{
//Do some stuff
this.DialogResult = DialogResult.OK
this.Close();
}
}

Related

How to let the program detect the previous form where a new form was accessed so that the input from the new form is transferred to that previous form

I have multiple forms (e.g. Form1, Form2) that both contains a button that opens another form (Form3). In Form3 (pop-up form), the user is prompted to pick among the options, and once these were submitted through a button in Form3, the selected options will be transferred to the previous form where it was opened (either form1 or form2). Both forms1 and 2 are linked to one input form3, so im thinking of using a conditional statement upon clicking the "Submit" button in Form 3 that will determine whether the active form/currently maximized form is Form1 or Form2, and will let the program redirect and transfer the data accordingly to the specific form.
In maximized Form1 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form1
In maximized Form2 > clicks a button > Form 3 pop-up opens > User Input is submitted through a button > User Input is transferred to Form2
private void button1_Click(object sender, EventArgs e)
{
if (Form1.ActiveForm != null)
{
Form1.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form1.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
else if (Form2.ActiveForm != null)
{
Form2.transfer.labQuan.Text = label8.Text;
double InitAmount, AmountwFee;
InitAmount = Convert.ToDouble(label12.Text);
AmountwFee = InitAmount + 100;
Form2.transfer.labAmount.Text = String.Format("P {0:N2}", AmountwFee);
this.Hide();
}
}
It shows the output for Form1, but for Form2 there's no output. I tried placing Form2 in the first condition (if) and that works but not for Form1 this time. Apparently, what comes first is the only condition performed by the program, and the else if is not executed.
I tested if (Form1.Visible = true) works, but I've already tried and there was an error in the program. Should there be additional declarations or such or perhaps a new public class?
You're thinking about it backwards. Instead of "pushing" from Form3 back to Form1 or Form2, you should "pull" from Form3 directly from within the code in either Form1 or Form2. You can do this by showing Form3 with ShowDialog(), which will cause execution in the current form (Form1 or Form2j) to STOP until Form3 is dismissed. In Form3, you can make public properties that can be accessed to retrieve the values from it.
For example, here's a boiled down Form3:
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
public String LabQuantity
{
get
{
return label8.Text;
}
}
public double AmountwFee
{
get
{
return Convert.ToDouble(label12.Text) + 100;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
}
Then in either Form1 or Form2, you'd do something like:
private void button1_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
if (f3.ShowDialog() == DialogResult.OK) // code STOPS here until "f3" is dismissed!
{
// ... do something with the data from "f3" ...
Console.WriteLine(f3.LabQuantity);
Console.WriteLine(f3.AmountwFee);
}
}
Thank you for all the suggestions! I'm learning a lot from the feedback because I'm only new to this as a student. Since coding is still very complicated for me, I decided to learn how to duplicate forms and proceeded to make a separate form3 for both form1 and form2. They were the common "copy, paste" techniques plus the renaming of forms, but it worked well in the end.
I will try to implement the suggestions the next time we have a coding assignment. Thank you!

How to call a method from another Form?

I have my dataGridView in Form1. I have UpdateForm method in Form2. I have Click Event in dataGridview Form1 that loads to Form2 UpdateForm. But when I Close Form2 after made Update then dataGridview Form1 still shows the old information. I have to reload Form1 to see Changes I made. My question is I want to see Changes I made directly after I Close Form2.
I tried like following:
In Form1
Public void RealoadForm()
{
dataGridView1.Update();
RealoadForm();
this.Refresh();
}
Then in Form2 update button and tried even in FormClosed:
Form1 frm = new Form1();
frm.RealoadForm();
But still not working.
Just create public methode that reload your dataGridview. The same method that load information to load to your dataGridview and call it from Form2 like this:
Form1 form = Application.OpenForms.OfType<Form1>().FirstOrDefault();
if(form != null)
{
form.YourLoadMethode();
}
You can change the FormattingApplied = true; in OnCellFormatting event
OnCellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
e.FormattingApplied = true;
}

How do I pass the text of a textbox from a pop-up window to the main form?

My program is basically reading barcodes, but I want to allow users to enter barcode manually, so I created a pop up window for that. After entering the barcode, I want the pop up window to disappear and send data to the main form when ENTER is hit, but I have no clue how this data can be passed to the main form.
Ok, this is pretty straightforward, I'll show you with an example. You have two forms: Form1 mainForm and Form 2 subForm.
mainForm calls subForm as follows:
using (Form2 subForm = new Form2())
{
if (subForm.ShowDialog() == DialogResult.OK)
{
string my_text = subForm.TextToReturn;
// Do stuff with my_text
}
}
In SubForm you will have something like this declared in the class scope:
public string TextToReturn;
private void button1_Click(object sender, EventArgs e)
{
TextToReturn = text_box.Text;
this.DialogResult = DialogResult.OK;
}

Open Form1 as new instance after successful operations on Form2

I am having two forms as Form1 and Form2 on Form1 button click I am opening the form2 as follows
private Form2 form2;
this.Hide();
form2.ShowDialog();
this.Show();
I have a back button and submit button on Form2 when I click on Back this is how I am showing Form1
this.Hide();
this.DialogResult = DialogResult.OK;
But when I submit the form I need to load the Form1 with new instance, this is what I tried but Form1 is showing multiple times
if (DialogResult.OK == MessageBox.Show("Installation was done", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information))
{
Form f1 = new Form ();
f1.Show();
this.Close();
}
When I execute my application this is how it looks after entering data to my textbox
After clicking on button1 it will show the data in Form2 if user wants to modify data he can come back if he clicks on submit I would like to have the new instance of Form1
What you need to do is after you open form 2 do something like
Form1.close();
and then when you are done with the operation:
Form1 form1 = new Form1();
form1.ShowDialog();
this.Close();
Ok if you want to save the data from form1 then i would either fave the data as a file that you could retrieve when ever of pass all of the variables you want to save into Form2 and then save them there. also instead of using a new form 1 you could use the existing one and reset any values that you want to reset.
I hope this helps.
Here is the code I tried, on your Form1 button click event write the following
private void button1_Click(object sender, EventArgs e)
{
form2.ShowData(textBox1.Text);
form2.ShowDialog(this);
}
In your Form2 submit click have the following code
private void button2_Click(object sender, EventArgs e)
{
this.Owner.Hide();
this.Owner = new Form1(this);
this.Hide();
this.Owner.Show();
}

How to not allow the user to touch the Form1 While Form2 is opened

Is possible to do not allow the user to click on the Form1 while Form2 is opened?
e.g.: I Debug my application, and it opens my MainForm Form1.
There's a button that opens Form2, but the user still able to minimize the Form and interact with the Form1. How may I cut this out ?
Use ShowDialog() rather than Show() to show the second form.
Open Form2 using ShowDialog():
new Form2().ShowDialog();
Of course using ShowDialog seems to be the best. You can use this code to achieve what you want in another way:
public class Form1 : Form {
public Form1(){
InitializeComponent();
Button button = new Button() {Text = "Show Form2"};
button.Click += (s,e) => {
f = new Form2();
f.Show();
};
button.Parent = this;
}
private Form f;
private void Form1_Activated(object sender, EventArgs e){
if(f!=null&&f.Visible) f.BringToFront();
}
}

Categories

Resources