I have 2 forms. One is the display form and the other is the where data is added to an array.
How do I start a function on the display form once the ok button has been clicked on the add to array form?
EDIT:
Let me rephrase. How would I go about calling a function that is in one .cs file, from another .cs file.
Form 1
private void menuItem1_Click(object sender, EventArgs e)
{
Form2 form2= new Form2();
form2.Owner = this;
form2.ShowDialog(this);
}
Form 2
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.myMethod();
}
Obviously, the form2 code is generating a new instance of form1.
How do I run the method of the form that opened the modal window?
Thanks
The owner of Form2 is a instance of Form1 so you can cast the owner to Form1 and call the method:
(this.Owner as Form1).myMethod();
Related
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.
Am a newbie when it come to C# Programming. Here's my trouble:
I want to Show a new Form 2 and Hide Form 1 on the Windows Form 1 Load.
Here's my current codes;
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Hide();
}
My en-counted Error with the current Code:
When Form 1 load its loading Form 2 but it's not hiding itself. this.Hide Statement not working, I've try this.Close but this will Close the entire software as it's closing the main form.
Can anyone kindly help me with this error.
The Error i thought is that you are showing the form2 before hiding the previous form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
form1.Visible=false;
Form2.Show();
}
You can also make form2 as modal by using show dailog method() by which focus is given to the form2 and form1 becomes inactive though it will be shown.
modal and modeless forms https://msdn.microsoft.com/en-IN/library/aa984358%28v=vs.71%29.aspx
Here's how I manage to make this work.
New Codes:
Form 1
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
}
private void Timer_Tick(object sender, EventArgs e)
{
if (Properties.Settings.Default.Status == "Form2Visible")
{
this.Hide();
}
}
Explanation:
On the Windows Form 1 Load am showing the the Form 2, then using a timer to verify a Properties.Settings Value. If the Properties.Settings = Form2Visible, Form 1 will Hide. Once am done on Form 2 I simply need to change the Properties.Settings to something else and stop the Timer on Form 1.
If their is a simplest way let me know.
You can use Visible property to hide your form.
private void Form1_Load(object sender, EventArgs e)
{
var Form2 = new Form2();
Form2.Show();
this.Visible=false;
}
I'm trying to change the names of the datagrid and labels in Form2 from Form1 based on the selection.
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.Show();
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.dataGridView1.Columns["FirstName"].HeaderText = "Prenom";
frm.dataGridView1.Columns["LastName"].HeaderText = "Nom";
this.Hide();
frm.Show();
}
The above approach is working fine for me but I have a problem. When I click on X in the second/Form 2 it's just closing the Form2 not the Form1. How can close all the application when i click on the X.
Is there any better way of doing this??? The reason why i'm not using I'm using Telerik and I don't find any option to add resource file in that. Please correct me if i'm wrong. Thank you.
You simply need to attach an event handler to the Closed event of the newly created form so that it closes the main form when it's closed:
private void button1_Click_1(object sender, EventArgs e)
{
this.Hide();
Form2 frm = new Form2();
frm.FormClosed += (_, args) => this.Close(); //Added this method
frm.Show();
}
Add that same method to the other click handler as well.
The easiest way to close an application is to call the static method: Application.Exit
Another method if you only want to close one single form would be to add a handler for the FormClosing event and close the main form there.
I designed two forms: Form1 and Form2. Form1 is the main form. There is a button in Form1, if I click the button, then Form2 will pop out. I want to do something on Form2.
// click button in Form1.
private void button1_Click(object sender, EventArgs e)
{
Form form2= new Form();
form2.ShowDialog();
}
But Form2 is a new form rather than an existing form.
It is wrong.
How? Thanks.
You are creating instance of Form class not the Form2 which you have in your project. Create instance of Form2 which you created earlier and then call ShowDialog in it.
You might have notice the in the program.cs something like Application.Run(new Form1()); Here we create the instance of Form1 and pass to Run method.
Do it this way by creating instance of Form2 and calling ShowDialog() method to show it
Form2 form2= new Form2();
form2.ShowDialog();
You create blank form with
Form Form2= new Form();
You should use
Form2 form2= new Form2();
Complete code:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2= new Form2();
form2.ShowDialog();
}
Declare
Form2 form2= new Form2();
like your class member and use it like this:
private void button1_Click(object sender, EventArgs e)
{
form2.ShowDialog(); //blocking call
//or form2.Show() //non blocking call
}
EDIT
Based on correct comments, to make this work instead of executing the Close() on the function which will lead to Dispose() you need to use form2.Hide() to make is simply invisible
private void button1_Click(object sender, EventArgs e)
{
InputForm form1 = new InputForm();
form1.Show();
}
Here InputForm means which form you want to open.
The question is "Open an existing form from the main form"
Okay lets change it a little, Open an existing instance of form from the main form.
when you show a form
new Form2().Show();
lets say you hid it using
Form2.Hide();
you guys can use this
var Form2_instance = Application.OpenForms.OfType<Form2>().Single();
Form2_instance.Show();