I have 2 forms (Form1 and Form2). Form1 has a splitter with two panels. I have added Form2 in the panel2 of the splitter control. I want to pop in and pop out Form2 without creating a new instance of Form2. Please find the code snippet below:
public partial class Form1 : Form
{
private Form2 form2 = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
form2 = new Form2();
form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form2.Dock = DockStyle.Fill;
form2.TopLevel = false;
splitContainer1.Panel2.Controls.Add(form2);
form2.Pop += new EventHandler(PopForm);
form2.Show();
}
//button click event handler from Form2
private void PopForm(object sender, EventArgs e)
{
Button b = sender as Button;
if(b.Text.ToUpper() == "POPOUT")
{
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel2.Controls.Remove(form2);
//need to show the form without creating a new instance to maintain state
form2 = new Form2();
form2.SelectedMailId = 1;
form2.Pop += new EventHandler(PopForm);
form2.SetButtonText = "PopIn";
form2.Show();
}
else
{
//this works fine
splitContainer1.Panel2Collapsed = false;
form2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
form2.Dock = DockStyle.Fill;
form2.TopLevel = false;
splitContainer1.Panel2.Controls.Add(form2);
}
}
}
How can I show the Form2 without creating a new instance when popping out?
While popout setup the form border styled and toplevel
if(b.Text.ToUpper() == "POPOUT")
{
splitContainer1.Panel2Collapsed = true;
splitContainer1.Panel2.Controls.Remove(form2);
//need to show the form without creating a new instance to maintain state
form2.TopLevel = true;
form2.FormBorderStyle = FormBorderStyle.Sizable;
// setup your settings
form2.Show();
}
Related
hello I have a problem with TabControl. When I open the second Form it is always empty with no controls or anything.
my code. When I open the first tabpage everything is fine. Everything is in place
private void ShowFormInTabPage(TabPage tp , Form frm)
{
tabControl1.Controls.Add(tp);
frm.TopLevel = false;
tp.Text = frm.Text;
frm.Visible = true;
frm.FormBorderStyle = FormBorderStyle.None;
frm.Dock = DockStyle.Fill;
tabControl1.TabPages[0].Controls.Add(frm);
}
I add form to tabcontrol through such a method. the methods are the same in both cases
private void guna2Button1_Click(object sender, EventArgs e)
{
_tpEmployees = new TabPage();
if (EmployeesForm.IsNull)
{
ShowFormInTabPage(_tpEmployees , EmployeesForm.Instance);
}
else
{
tabControl1.SelectedTab = _tpEmployees;
}
My problem is that I have inserted a tab to a tabControl and added a Form to it (basically I wanted to display all the forms I was going to open as tabs).
Code for adding form to tabControl as tab in the Main class:
private void new_form_Click(object sender, EventArgs e)
{
add = new Add(null);
add.TopLevel = false;
add.Visible = true;
add.FormBorderStyle = FormBorderStyle.None;
add.Dock = DockStyle.Fill;
var tabIndex = tabControl1.TabCount;
tabControl1.TabPages.Insert(tabIndex, "New Tab");
tabControl1.SelectedIndex = tabIndex - 1;
tabControl1.TabPages[tabIndex - 1].Controls.Add(add);
}
When I was using multiple forms it was easy to rename the title from the form class:
private void surname_Leave(object sender, EventArgs e)
{
this.Text = surname.Text;
}
How can I rename the tab programmatically from inside the added form?
Edit:
I know how to rename a tab from the same class. I need to rename the tab from the form I open in the tab.
What I was trying to accomplish I achieved by passing the reference of one to another form.
in Form2:
public Form2()
{
InitializeComponent();
}
private Form1 mainForm;
public Form2(Form callingForm)
{
mainForm = callingForm as Form1;
InitializeComponent();
}
private void surname_Leave(object sender, EventArgs e)
{
mainForm.setTabTitle = surname.Text;
}
in Form1:
private void new_form_Click(object sender, EventArgs e)
{
add = new Add(this); \\this part was missing
add.TopLevel = false;
add.Visible = true;
add.FormBorderStyle = FormBorderStyle.None;
add.Dock = DockStyle.Fill;
var tabIndex = tabControl1.TabCount;
tabControl1.TabPages.Insert(tabIndex, "New Tab");
tabControl1.SelectedIndex = tabIndex - 1;
tabControl1.TabPages[tabIndex - 1].Controls.Add(add);
}
public string setTabTitle
{
set { tabControl1.TabPages[tabControl1.SelectedIndex].Text = value; }
}
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);
public static object loadForm(Form formToLoad, TabControl homeTabControl)
{
//Check if formToLoad parameter is NULL
if (formToLoad == null) throw new ArgumentNullException("formToLoad");
//get the parent/ownining form
Form form1 = new Form1();
//set formToLoad properties
formToLoad = new Form
{
Owner = form1,
FormBorderStyle = FormBorderStyle.None,
TopLevel = false,
Dock = DockStyle.Fill
};
//add formToLoad to tabControl tabPage
homeTabControl.TabPages["tabPageHome"].Controls.Add(formToLoad);
formToLoad.Show();
return formToLoad;
}
How come formToLoad does not show in the tabControl Page when i call my code from a button click?
private void button3_Click(object sender, EventArgs e)
{
LeaveMainForm lM = new LeaveMainForm();
AppCode.FormLoader.loadForm(lM, homeTabControl);
}
You are over-writing the actual form you are trying to load with a new Form instance, in this line :
formToLoad = new Form
Try this :
//set formToLoad properties
formToLoad.Owner = form1;
formToLoad.FormBorderStyle = FormBorderStyle.None;
formToLoad.TopLevel = false;
formToLoad.Dock = DockStyle.Fill;
I'm creating LinkLabel in Form2 and Form3 with the same code. Form2 and Form3 are separate classes so the names don't interferer. They are both created, but in the Form 3 links open, in the Form2 nothing happens.
This is the code for Form2
public partial class Form2 : Form
{
public void createFormEntry(List<string> videoId)
{
LinkLabel link = new LinkLabel();
link.Name = "link";
link.AutoSize = true;
link.Location = new Point(76, 8);
link.Text = "www.example.com";
link.LinkClicked += new LinkLabelLinkClickedEventHandler(link_LinkClicked);
this.Controls.Add(link);
}
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.example.com");
}
}
And this is for the Form3
public partial class Form3 : Form
{
private void createFormEntry(Feed<Video> videoFeed)
{
LinkLabel link = new LinkLabel();
link.Name = "link";
link.AutoSize = true;
link.Location = new Point(76, 8);
link.Text = "www.example.com";
link.LinkClicked += new LinkLabelLinkClickedEventHandler(link_LinkClicked);
this.Controls.Add(link);
}
private void link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
System.Diagnostics.Process.Start("http://www.example.com");
}
}
They are in different classes. Form2 opens before Form3.
What could be wrong?
EDIT: now when I added more code, I see in Form2 createFormEntry is public and in Form3 it is set as private.
Could that be a reason?
Your trying to open a link without telling the program how or what to open that link in. You should tell it to search it in a program or something!