Let's say I have a basic form, there's a button and when I click it, it opens a new window.
How can I do this? I tried creating a new form instance on button click event, but it gives me exception, that something is wrong.
Form frm = new Form();
frm.ShowDialog();
//frm.Show();
Or please share your code..
//assuming that ur first form is named Form1 and ur second form is Form2
//assuming that ur button is button1
//inside form1 something like this is shown
Button1.Click += new EventHandler(this.Button1_Click);
void Button1_Click(Object sender, EventArgs e){
Form2 form = new Form2();
//you do either
Form2.Show();
//or focus remains on form2 do this
Form2.ShowDialog();
}
//hope this help
Related
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.
I have new form opening when button is clicked, like this:
private void button8_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.ShowDialog();
//more code
}
Does this mean that, when i click Button8, form2 will be opened, but code after form2.ShowDialog() will not be executed until i close form2?
If this is not the way how to do that, please, can you tell me.
Use Show(); not ShowDialog(); That should works.
I want in display child form on application load with parent form disable behind child form. How can i achive this in windows application c#?
If you want to show form2 from form1 you can follow this method also
f2 = new Form2();
f2.ShowDialog(this);
private void Form1_Shown(Object sender, EventArgs e)
{
f2 = new Form2();
f2.ShowDialog(this);
}
You can hide or show forms using form1.Show() or form1.Hide().
But before hiding i will tell you to Preserve the instance of Form1
and use it to Show or Hide
Form2 parent= new Form2();
parent.Hide();
Form1 child= new Form1();
child.Show();
or You can minimize parent Form and show child
using this
Form2 parent= new Form2();
Form2.WindowState = FormWindowState.Minimized;
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();
I have two forms. I have a button on the first form that I want to bring up a form called form2. How would I do this?
public void OnClick(object sender, EventArgs args)
{
var form = new Form2();
form.Show();
}
YourOtherFormClass form2 = new YourOtherFormClass();
form2.Show();
This can be put in your button's on_click event handler, where YourOtherFormClass is the class of your second form. You just need to instantiate it and then call the Show() method.
Double click on the button to create it's on-click handler, and simply do
Form2 f2 = new Form2(); // or whatever the name of the class is
f2.Show();