How to close a Form embedded in a panel - c#

So I have 4 Forms. Inside Form1 there are 2 Panels and a button. When I click the button1 on Form1, it opens the Form2 on the first panel. So inside the Form2 there are 2 buttons. The button1_Click method shows the Form3 on the second panel of Form1. How can I show the Form4
on the second panel by the method button2_Click from Form2?
private void button2_Click(object sender, EventArgs e)
{
Form4 myForm = new Form4();
myForm.TopLevel = false;
myForm.AutoScroll = true;
var Form1 = Application.OpenForms.OfType<Form1>().Where(x => x.Name == "Form1").FirstOrDefault();
Form1.panel2.Controls.Add(myForm);
myForm.Show();
}`
This is the code I used to try showing Form4 on the second panel. But it wont replace Form3 that is currently appearing on the second panel of Form1. I really don't know what to do. Please help. Thanks in advance.

So from form 2 you want to be able to load form3 OR form4 inside panel2 ?
private Form3 form3 = null;
private Form4 form4 = null;
private void button2_Click(object sender, EventArgs e)
{
if (form3 != null)
{
Form1.panel2.Controls.Remove(form3);
form3.Close();
form3 = null;
}
form4 = new Form4();
form4.TopLevel = false;
form4.AutoScroll = true;
var Form1 = Application.OpenForms.OfType<Form1>().Where(x => x.Name == "Form1").FirstOrDefault();
Form1.panel2.Controls.Add(form4);
form4.Show();
}`
and do the same for Form3 (remove and close form4 before creating form3)
Also consider setting your form3 and form4 properties as HansPassant suggested in his comment.

Related

How to update an active windows form textbox from a value in a second form?

I have a Windows Form (let's call it Form1) that opens a second windows form (Form2) to make the user select something from a comboBox.
// create form1
var Form1 = new Form1();
Form1.Show();
// event to open form2
private void openForm2(object sender, EventArgs e)
{
Form2 Form2= new Form2();
Form2.Show();
}
After selecting an item in the comboBox, the user clicks on the "Update" button to update a textBox in Form1 with the selected value.
private void updateForm1(object sender, EventArgs e)
{
// Form1.textBox1 is not accessible
}
I know how to pass a value from Form1 to Form2, but how can I pass a value from Form2 back to the active Form1 ? I can't access the active Form1 without recreating the form.
I found the answer by passing an event handler.
// propertie in form1 to hold the form2 instance to get the value back
private Form2 _form2;
public Form2 form2
{
get { return _form2; }
set { this._form2= value; }
}
// opens the form1
var Form1 = new Form1();
Form1.Show();
// event to open form2
private void openForm2(object sender, EventArgs e)
{
Form2 Form2= new Form2();
Form2.FormClosing += new FormClosingEventHandler(getValueFromForm2);
Form2.Show();
this.form2 = Form2;
}
// event to retrieve the value and update textbox of form1
private void getValueFromForm2(object sender, FormClosingEventArgs e)
{
Control ctrl = this.form2.Controls.Find("theControlWithTheValue", true)[0];
// control is a listbox
ListBox lbox = ctrl as ListBox;
// update form1 textbox
textBoxInForm1.Text = lbox.SelectedItem.ToString();
}

How to change button image?

How can I change the image of a button from form 3 to form 2? I use those buttons to navigate between forms, and want to, when I click on button in form 3, to get back to form 2, to have changed the picture of button in form 2.
This is how I navigate between forms.
FORM2 BUTTON
private void button1_Click(object sender, EventArgs e)
{
Form3 m = new Form3();
m.Show();
this.Visible = false;
this.Hide();
}
FORM3 BUTTON
private void button14_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.Show();
this.Visible = false;
this.Hide();
}
In your button on FORM 3, do something like this:
private void button14_Click(object sender, EventArgs e)
{
Form2 m = new Form2();
m.button1.BackgroundImage = Image.FromFile(#"C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg");
m.Show();
this.Visible = false;
this.Hide();
}
In order for this to work, the button1 on your FORM 3 must be accessible. So go to FORM 3 and mark button1 as public.

Not getting New Tab in WebBrowser Visual Studio 2012

I have two forms Form1 and Form2.
Form1 has a WebBrowser and Form2 contains tab code.
In Form1 on load I have kept the following code to get first tab:
public void Form1_Load(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
tabControl1.TabPages.Add(t);
}
This is working fine.
Now in Form2 I have a menu item which when clicked will open new tab. Its code is as follows:
public void addTabToolStripMenuItem_Click(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show();
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
Form1 b = new Form1();
b.tabControl1.TabPages.Add(t);
}
But I don't get a new tab. i.e. When I click the menu item nothing happens.
You are creating a new Form1 in your click Handler, it is not the Form1 that you used to create the instance of Form2 in. Your best bet would be to use the Show method that allows you to set the owning form and as long as your TabControl's Modifier is public it will work. This is IMHO really not the way to go. I would look into creating an event and subscribe to that.
public void Form1_Load(object sender, EventArgs e)
{
TabPage t = new TabPage();
Form2 newtab = new Form2();
newtab.Show(this); //note the change here
newtab.TopLevel = false;
newtab.Dock = DockStyle.Fill;
t.Controls.Add(newtab);
tabControl1.TabPages.Add(t);
}
This will enable you to do this:
((Form1)this.Owner).tabControl1.TabPages.Add(t);

Closing three forms form button

I have the following code:
in form1
private void button6_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form5 = new Form5();
Form Form6 = new Form6();
Form4.Show();
Form5.Show();
Form6.Show();
}
in form5 i have a button that must close form4, form5 and form6. as following:
private void button2_Click(object sender, EventArgs e)
{
Form Form4 = new Form4();
Form Form6 = new Form6();
Form4.Close();
Form6.Close();
this.Close();
}
but Form4 and Form6 are still open!!!
private void button2_Click(object sender, EventArgs e)
{
Form4 form4 = (Form4) Application.OpenForms["Form4"];
Form5 form5 = (Form5) Application.OpenForms["Form5"];
form4.Close();
form5.Close();
}
To close in proper way you have to know what do you want to close. In your case you are creating new Forms on beggining, and another ones with the same names visible locally before trying to destroy them.
Form Form6 = new Form6(); are totally different in both of your clicked buttons. Then, if you want to make it more visible generate interesting forms in constructor method and put definition as class fields like.
public partial class Form1 : Form
{
Form form2; // be sure all componentes see all forms
Form form3;
Form form4;
public Form1()
{
InitializeComponent();
form2 = new Form(); // create new Forms
form3 = new Form();
var button = new Button();
button.Click += new EventHandler(button_Click); // tell the button what should be called when click
form4 = new Form();
form4.Controls.Add(button); // add button progrimicaly to form
}
void button_Click(object sender, EventArgs e)
{
form2.Hide(); // hide on click
form3.Hide();
form4.Hide();
}
private void Form1_Load(object sender, EventArgs e)
{
form2.Show(); // show on load
form3.Show();
form4.Show();
}
}

Block Form1 until Form2 closes c#

In my code i from form1 launch form2 using .Show() .
Form2 f2 = new Form2();
f2.show();
block form1 until form2 close and when form2 closed continue my code.
.Show() will show the new form you are displaying but it will enable you to go back and use the controls in the Main Form and .ShowDialog() wont allow you to access your main form unless its closed.
f2.ShowDialog();
Try using
Form2 f2 = new Form2();
f2.showDialog();
I tested that ways and don't worked but only below code worked:
private void button1_Click_1(object sender, EventArgs e)
{
Form2 frm = new Form2();
this.Enabled = false;
frm.Show();
frm.FormClosing += new FormClosingEventHandler(frm_FormClosing);
frm.Show();
}
private void frm_FormClosing(object sender, FormClosingEventArgs e)
{
this.Enabled = true;
}

Categories

Resources