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;
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 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();
}
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 want to control my form load event from another form.
my problem I create some winform control in form1 runtime, but the creation will controlled by form2.
I will read some data from user in form2 and when user enter specific text I will create winform control in form1.
I make some code to do that using from1 to create winform control in runtime.
private TextBox txtBox = new TextBox();
private Button btnAdd = new Button();
private ListBox lstBox = new ListBox();
private CheckBox chkBox = new CheckBox();
private Label lblCount = new Label();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.MaximizeBox = false;
this.MinimizeBox = false;
this.BackColor = Color.White;
this.ForeColor = Color.Black;
this.Size = new System.Drawing.Size(550, 550);
this.Text = "Test Create form in runtime ";
this.FormBorderStyle = FormBorderStyle.FixedDialog;
this.StartPosition = FormStartPosition.CenterScreen;
this.btnAdd.BackColor = Color.Gray;
this.btnAdd.Text = "Add";
this.btnAdd.Location = new System.Drawing.Point(90, 25);
this.btnAdd.Size = new System.Drawing.Size(50, 25);
this.txtBox.Text = "Text";
this.txtBox.Location = new System.Drawing.Point(10, 25);
this.txtBox.Size = new System.Drawing.Size(70, 20);
this.lstBox.Items.Add("One");
this.lstBox.Sorted = true;
this.lstBox.Location = new System.Drawing.Point(10, 55);
this.lstBox.Size = new System.Drawing.Size(130, 95);
this.chkBox.Text = "Disable";
this.chkBox.Location = new System.Drawing.Point(15, 190);
this.chkBox.Size = new System.Drawing.Size(110, 30);
this.lblCount.Text = lstBox.Items.Count.ToString() + " items";
this.lblCount.Location = new System.Drawing.Point(55, 160);
this.lblCount.Size = new System.Drawing.Size(65, 15);
this.Controls.Add(btnAdd);
this.Controls.Add(txtBox);
this.Controls.Add(lstBox);
this.Controls.Add(chkBox);
this.Controls.Add(lblCount);
}
How make the same thing from form2 ?
I don't know which kind of 'Control' you need. However in multiple forms environment, communication between forms is trivial. There are many ways to do communicate, like one can be as
Create public properties of type Form in your Parent form,
public Form propForm1 {get;set;}
When on menu item click you open form1, store it's object to that public property.
var form1 = New yourchildformname();
form1.MdiParent = this;
propForm1 = form1; // Add this line.
form1.Show();
Now every time when you will click an other button to open the form2, you will have propForm1 object, which you can use to set some data on that form.
EDIT:
On form2, you can access controls of form1 as
private void button1_Click(object sender, EventArgs e)
{
this.parent.propForm1.txtUserName = "Yokohama";
}
Remember the above code is on form2. Also set 'access modifier' property of txtUserName from private to public.
I have a treeView to open each child Form, but when I open new child form I can still see the previous child form behind the new child form. What I want to do is close the old child form when open a new one. (childForm.Close(); // not working somehow)
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
string selectedNodeText = e.Node.Text;
Form1 myform1 = new Form1();
Form2 myform2 = new Form2();
if (selectedNodeText == "1")
{
myform1.MdiParent = this;
myform1.Parent = this.splitContainer1.Panel2;
myform1.Dock = DockStyle.Fill;
this.splitContainer1.Panel2.Controls.Add(myform1);
myform1.BringToFront();
myform1.Show();
}
if (selectedNodeText == "2")
{
myform2.MdiParent = this;
myform2.Parent = this.splitContainer1.Panel2;
myform2.Dock = DockStyle.Fill;
this.splitContainer1.Panel2.Controls.Add(myform2);
myform2.BringToFront();
myform2.Show();
}
}
Take a look at the MdiChildren property of your form object. There is a reference to all forms you attached to your MdiContainer.
Another way is to store the reference to your forms in the class itself and close the old one if you open another:
private Form1 myform1 = null;
private Form2 myform2 = null;
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) {
string selectedNodeText = e.Node.Text;
if (selectedNodeText == "1" && myform1 == null) {
myform1 = new Form1()
// ... some code ...
if (myform2 != null) {
myform2.Close();
myform2 = null;
}
}
if (selectedNodeText == "2" && myform2 == null) {
myform2 = new Form2()
// ... some code ...
if (myform1 != null) {
myform1.Close();
myform1 = null;
}
}
}