I am wondering how can I correctly switch between forms by button click event.
I have Form1 and Form2.
Form1 have: -TextBoxForm1
-ButtonForm1
Form2 have: -TextBoxForm2
-ButtonForm2
I would like to on_click ButtonForm1 event go to the Form2. Then I want to write some message to TextBoxForm2 and press ButtonForm2 it will go to the Form1 again and message from TextBoxForm2 will appear in TextBoxForm1.
Everything works fine, but I have one problem. When I close application and I wanna debug and start it again, some errors appear like:"application is already running".
Form1:
public static string MSG;
public Form1()
{
InitializeComponent();
TextBoxForm1.Text = MSG;
}
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
this.Hide();
//There is probably my fault but when I was trying this.Close(); everything shutted down
form2.Show();
}
Form2:
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
Form1 form= new Form1();
form.Show();
this.Close();
}
How can I do this correctly please? :) I am beginner, thank you!
I would not go the route of using a STATIC for passing between forms as you mention you are a beginner, but lets get the closing to work for you.
In your main form create a new method to handle an event call as Hans mentioned in the comment. Then, once you create your second form, attach to its closing event to force form 1 to just become visible again.
// Inside your Form1's class..
void ReShowThisForm( object sender, CancelEventArgs e)
{
// since this will be done AFTER the 2nd form's click event, we can pull it
// into your form1's still active textbox control without recreating the form
TextBoxForm1.Text = MSG;
this.Show();
}
and where you are creating form2
private void ButtonForm1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Closing += ReShowThisForm;
this.Hide();
form2.Show();
}
and in your second form click, you should only need to set the static field and close the form
private void ButtonForm2_Click(object sender, EventArgs e)
{
Form1.MSG = TextBoxForm2.Text;
this.Close();
}
Easy solution is to use modal form. Display Form2 temporarily, when it is displayed Form1 is invisible.
var form2 = new Form2();
this.Visible = false; // or Hide();
form2.ShowDialog(this);
this.Visible = true;
And to pass data you can define property in Form2, to example:
public string SomeData {get; set;}
Form1 has to set SomeData, which you take in Shown and display. Same property can be used to get data from Form2 (before closing).
// form 1 click
var form2 = new Form2() { SomeData = TextBoxForm1.Text; }
this.Visible = false;
form2.ShowDialog(this);
this.Visible = true;
TextBoxForm1.Text = form2.SomeData;
// form 2 shown
TextBoxForm2.Text = SomeData;
// form 2 click
SomeData = TextBoxForm2.Text;
Close();
Related
i have two forms which i want to move back and forth with without loosing the data that i have entered on both form, when i go back to form1 from form2 the data remains in form1, but when i go to form2 in which i have entered data before, the data are all gone, is there a solution to this?
first form:
public userform1()
{
InitializeComponent();
}
private void jThinButton1_Click(object sender, EventArgs e)
{
userform2 form2 = new userform2();
form2.Show();
this.Hide();
form2.Hide();
form2.ShowDialog();
this.Show();
second form:
private void jThinButton3_Click(object sender, EventArgs e)
{
this.DialogResult = DialogResult.OK;
}
going back to form2 from form1 works fine, but the problem is I loose the data I have entered in form2 when I click next in form1, I want to keep the entered data in form 2, is it possible?
Encapsulate your userform2 instance in a readonly property which creates a new one if it's not created already
private userform2 _form2;
private userform2 form2
{
get
{
if (_form2 == null)
_form2 = new userform2();
return _form2;
}
}
Then use it like this
this.Hide();
form2.ShowDialog();
this.Show();
now whenever you access form2 its the same instance of userform2.
Or simply if you want to use the field only, but the instance is created when userform1 is constructed.
private userform2 form2 = new userform2();
I wants to show form2(Filter Form) first. Actually I am calling form2 from Form1(Report Test Form) page load. But It appears Form2 in back position and Form1 in First position.
Code
private void ReportTestForm_Load(object sender, EventArgs e)
{
ReportFilterForm report = new ReportFilterForm();
report.Show();
}
Screenshot
Note
I don't want to hide Form 1
Use TopMost Property:
private void ReportTestForm_Load(object sender, EventArgs e)
{
ReportFilterForm report = new ReportFilterForm();
report.TopMost = true;
report.Show();
}
ShowDialog will force Form2 to be close to be able to return to Form1
report.ShowDialog();
I have two forms. First one has one textbox.Second one has a devexpress data grid.
I want to achieve that:
first click a button and form2 opens.
if I click a row in the data grid in form2, this value should be shown inside the textbox in form1.(form1 is already opened.)
ı m a beginner. thanks for your help.
Form1 frm1 = new Form1();
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
frm1.Show();
when I do that, a new form opens. I dont want to open a new form. Form1 is already opened. I want to add values to its textbox.
Pass form1 as a reference to form2:
In your button click handler on form 1 to open form 2
private void Button_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
form2 code
private Form1 frm1;
// constructor (pass frm1 as reference)
public Form2(Form1 frm1) {
this.frm1 = frm1;
}
//put this in your event handler for a row click in the grid
frm1.textBox1.Text = gridView1.GetFocusedRowCellValue("ID").ToString();
you can try something like this.
When you click on frm1.SimpleButton this function will be called a show your Form2. On Form2 you have to set InnerProperty (from your datagrid?) a when you close Form2 you can use this property.
private void SimpleButton_Click(object sender, EventArgs e)
{
using (Form2 frm = new Form2())
{
frm.InnerProperty = "default text";
DialogResult result = frm.ShowDialog();
SimpleButton.Text = frm.InnerProperty;
}
}
Finally, I solved the problem.
Application.OpenForms["form1"].Controls["textBox1"].Text =
gridView1.GetFocusedRowCellValue("ID").ToString();
Thanks for help.
So basically i have Form1
From Form1 I can open Form2 with this code:
private void btn_Komplexity_Click(object sender, EventArgs e)
{
Form2 kompleksaForma = new Form2();
kompleksaForma.ShowDialog();
}
When Form2 is opened there is something and at the end there is this.Close();
After this.Close(); (closing Form2) is it possible to call instant action on Form1?
If you're sticking to ShowDialog(), this function will block until the form is closed.
private void btn_Komplexity_Click(object sender, EventArgs e)
{
using (Form2 kompleksaForma = new Form2())
{
kompleksaForma.ShowDialog();
PutStuffHereAfterClose(); // (or outside the using block if it doesn't need
// to access properties of kompleksaForma)
}
}
If you're showing Form2 as a modal window using form2.ShowDialog() or form2.ShowDialog(this), then...
form2.ShowDialog(this);
if (form2.DialogResult == DialogResult.OK)
{
CallOtherStuffHere();
}
... as the ShowDialog() method will block execution until the closure of Form2, then continue.
I'm using DialogResult above to test for validity, but you could implement some other method, if you wish.
If you're showing Form2 as a non modal window, then you should pass a reference of Form1 to Form2 first. This could be done in its constructor...
var form2 = new Form2(form1);
Or, you can pass it in the Show() method, to set form1 as its parent...
var form2 = new Form2();
form2.Show(form1);
Then, you can access the parent form via form2.Parent. However, you may have to cast it to a Form1 instance before you call your methods explicitly. And this can be done in the Closing event handler of Form2.
Further info here regarding modal and modeless windows:
https://msdn.microsoft.com/en-us/library/aa984358(v=vs.71).aspx
There are some events triggered after the window is closed. You can subscribe to them and add your code to the handler method:
Form2 kompleksaForma = new Form2();
kompleksaForma.FormClosing += KompleksaForma_FormClosing;
kompleksaForma.FormClosed += KompleksaForma_FormClosed;
kompleksaForma.Deactivate += KompleksaForma_Deactivate;
kompleksaForma.ShowDialog();
And then implement one of the handlers like that:
private void KompleksaForma_FormClosing(object sender, FormClosingEventArgs e)
{
// Your code here
}
private void KompleksaForma_FormClosed(object sender, FormClosedEventArgs e)
{
// or here
}
private void KompleksaForma_Deactivate(object sender, EventArgs e)
{
// or here
}
First will trigger FormClosing, then FormClosed. Last one is Deactivate.
I'm trying to solve this problem for a long time.
I have 2 forms, my objectives are:
When user minimize form2, form1 must minimize too.
When user maximize form2, form1 must maximize too.
When both forms are obscured by another window, and user clicks in the form2 icon in taskbar, form1 must also come to front.
The first 2 things I solved with the a_Resize method. But I can't do the third one. I tried with activate event but when I do that the form2 keeps blocked.
Here is my code:
public partial class Form1 : Form
{
Form2 form2;
public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Show();
form2.Resize += new EventHandler(a_Resize);
}
void a_Resize(object sender, EventArgs e)
{
if (((Form)sender).WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Minimized;
}
else
{
this.WindowState = FormWindowState.Normal;
}
}
}
If I add a handler to the form2 activate event:
form2.Activated += new EventHandler(form2_Activated);
And call for instance the Focus method (I tried other methods too), the form2 keeps blocked behind form1.
void form2_Activated(object sender, EventArgs e)
{
this.Focus();
}
Someone have any ideas how I can do that?
When you create form2, just pass this as a parameter to Show() to signify that form1 is the owner. With an owner link, the forms will always be raised together (at least in my experience -- I don't have a specification to back me up on this).
public Form1()
{
InitializeComponent();
form2 = new Form2();
form2.Show(this); //pass 'this' as argument to Show() to link them
form2.Resize += new EventHandler(a_Resize);
}