How to Load other Form inside panel in a primary form.
i was trying something like
private void frmInitialEntryBooks_Load(object sender, EventArgs e)
{
frmSujbect objForm = new frmSujbect();
pnl.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
}
but it throw exception Top-level control cannot be added to a control at line pnl.Controls.Add(objForm);
Use this:
private void frmInitialEntryBooks_Load(object sender, EventArgs e)
{
frmSujbect objForm = new frmSujbect();
objForm.TopLevel = false;
pnl.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
}
You are missing objForm.TopLevel = false;
Set TopLevel = False in frmsubject then try
frmSujbect objForm = new frmSujbect();
pnl.Controls.Add(objForm);
objForm.Show();
I did the following:
Method:
private void PopulateFormIntoTab(Form form)
{
TabPage page = tabControl1.SelectedTab;
form.TopLevel = false;
form.Parent = page;
form.FormBorderStyle = FormBorderStyle.None;
form.Dock = DockStyle.Fill;
form.Show();
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Clear();
tabControl1.TabPages[tabControl1.SelectedIndex].Controls.Add(form);
}
Initialize the form:
public Form1()
{
InitializeComponent();
tabControl1.TabPages.Clear();
tabControl1.Dock = DockStyle.Fill;
tabControl1.TabPages.Add(new TabPage("Form2"));
tabControl1.TabPages.Add(new TabPage("Form3"));
tabControl1.TabPages.Add(new TabPage("Form4"));
PopulateFormIntoTab(new Form2());
}
Finally, on selected tab index change:
private void TabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
Form form = null;
switch (tabControl1.SelectedIndex)
{
case 1:
form = new Form3();
break;
case 2:
form = new Form4();
break;
default:
form = new Form2();
break;
}
PopulateFormIntoTab(form);
}
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;
}
I have created two forms ( Message1 and Message 2) and a Main Form. I am dynamically loading one of the Message Form on the basis of the Main Panel Combo Selection. Main Panel has a combobox with two options, 1 and 2 and an Empty Panel.
I tested this but only one Form is Visible, whichever one is selected first.
Below is the code :
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cbox = (ComboBox)sender;
if(cbox.SelectedIndex == 0)
{
Message1 objForm = new Message1();
objForm.TopLevel = false;
panel1.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
}
else
{
if(cbox.SelectedIndex == 1)
{
Message2 objForm = new Message2();
objForm.TopLevel = false;
panel1.Controls.Add(objForm);
objForm.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm.Dock = DockStyle.Fill;
objForm.Show();
}
}
}
What am I missing here?
Ok thanks for the Answers. The clear() works.
public partial class MainPanel : Form
{
Message1 objForm1;
Message2 objForm2;
public MainPanel()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
ComboBox cbox = (ComboBox)sender;
objForm1 = new Message1();
objForm2 = new Message2();
if (cbox.SelectedIndex == 0)
{
panel1.Controls.Clear();
objForm1.TopLevel = false;
panel1.Controls.Add(objForm1);
objForm1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm1.Dock = DockStyle.Fill;
objForm1.Show();
}
else
{
if(cbox.SelectedIndex == 1)
{
panel1.Controls.Clear();
objForm2.TopLevel = false;
panel1.Controls.Add(objForm2);
objForm2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
objForm2.Dock = DockStyle.Fill;
objForm2.Show();
}
}
}
}
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();
}
i try to close windows form in mdiparent when i click other button, the result is when i click other button, it still appear from the back of new window. so how can i handle this?
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
Detail aa = new Detail();
aa.MdiParent = this;
aa.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}
You're making new instance of form and then closing it. That way you're not closing existing window but creating new (invisible) one and closing it. You should find existing window in collection of MdiChildren and then close it. Something like this:
private void btn_ic_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
SelectIC ss = new SelectIC();
ss.MdiParent = this;
ss.Show();
var detailForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(Detail));
detailForm?.Close();
btn_ic.Enabled = false;
btn_cat.Enabled = true;
}
private void btn_cat_Click(object sender, EventArgs e)
{
pictureBox3.Visible = false;
Detail aa = new Detail();
aa.MdiParent = this;
aa.Show();
var selectForm = this.MdiChildren.FirstOrDefault(f => f.GetType() == typeof(SelectIC));
selectForm?.Close();
btn_cat.Enabled = false;
btn_ic.Enabled = true;
}
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; }
}