Switch form in panel - c#

I'm trying to create a panel with somes button when you click on it, it show specific form inside the panel. The panel is blocked on the first form showed even when you try to remove it.
private void button1_Click(object sender, EventArgs e)
{
Settings settings = new Settings();
Dashboard dashboard = new Dashboard();
dashboard.TopLevel = false;
panel1.Controls.Add(dashboard);
panel1.Controls.Remove(settings);
dashboard.FormBorderStyle = FormBorderStyle.None;
dashboard.Dock = DockStyle.Fill;
dashboard.Show();
settings.Hide();
}
private void button2_Click(object sender, EventArgs e)
{
Settings settings = new Settings();
Dashboard dashboard = new Dashboard();
settings.TopLevel = false;
panel1.Controls.Remove(dashboard);
panel1.Controls.Add(settings);
settings.FormBorderStyle = FormBorderStyle.None;
settings.Dock = DockStyle.Fill;
settings.Show();
dashboard.Hide();
}

Related

Navigating through different forms in C#

I recently started working on an application with windows forms using C#. To navigate through the different forms I am using:
Form Form_name = new Form ();
Form_name .TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(Form_name);
Form_name.Show();
The current setup is that I have the main menu form where you then navigate to a upload photo form. But after uploading an image in the upload photo I want to show the next form. But because we now are working in another form we cant use:
panel1.Controls.Clear();
panel1.Controls.Add(Form_name);
Because panel1 is referenced in the first form. I tried to create a public class to clear it but had no success.
My questions come down to:
Is there a better method to navigate through different forms/windows?
Can we create a public function to clear the panel in the first form?
MainMenu.cs
private void mainMenu1_Click(object sender, EventArgs e)
{
ME_Upload ME_UploadConstruct = new ME_Upload();
ME_UploadConstruct.TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(ME_UploadConstruct);
ME_UploadConstruct.Show();
}
private void mainMenu2_Click(object sender, EventArgs e)
{
IndividualEditor individualEditor = new IndividualEditor();
individualEditor.TopLevel = false;
panel1.Controls.Clear();
panel1.Controls.Add(individualEditor);
individualEditor.Show();
}
}
ME_Upload.cs
private void ME_UploadButton_Click(object sender, EventArgs e)
{
OpenFileDialog opf = new OpenFileDialog();
opf.Filter = "Choose Image(*.jpg; *.png; *.gif)|*.jpg; *.png; *.gif";
if (opf.ShowDialog() == DialogResult.OK)
{
ME_ImageEditor ME_ImageEditorConstruct = new ME_ImageEditor();
ME_ImageEditorConstruct.TopLevel = false;
panel1.Controls.Clear(); ///Not Working
panel1.Controls.Add(ME_ImageEditorConstruct); ///Not Working
ME_ImageEditorConstruct.Show();
}
}

When tabpage add controls like form, how set form owner

when i individual add two forms to two pages controls,
how set form owner?
Because owner is empty, could not set data from form's event to another form's textbox text, how slove it, please help me .
-main form-
//tabpage add controls
private void TestForm2_Shown(object sender, EventArgs e)
{
//var DataUpdate = new DataUpdate();
var DataUpdate = new TestForm3();
DataUpdate.TopLevel = false;
//DataUpdate.Visible = true;
//DataUpdate.Top = 0;
//DataUpdate.Left = 0;
DataUpdate.Dock = DockStyle.Fill;
DataUpdate.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
tabPage1.Controls.Add(DataUpdate);
//var SystemSetting = new SystemSetting();
var SystemSetting = new TestForm4();
SystemSetting.TopLevel = false;
//SystemSetting.Visible = true;
//SystemSetting.Top = 0;
//SystemSetting.Left = 0;
SystemSetting.Dock = DockStyle.Fill;
SystemSetting.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
tabPage2.Controls.Add(SystemSetting);
SystemSetting.Show();
}
// In your TestForm3,
private void button1_Click(object sender, EventArgs e)
{
TestForm4 frm = new TestForm4();
frm.owner=this;
frm.Show();
}
// In your TestForm4,
private void button1_Click(object sender, EventArgs e)
{
TestForm3 mainForm=(TestForm3)this.owner;
mainForm.LabelText = textBox1.Text;
}

Switch forms with tab control

I am a beginner in C#, so please bear with me. I have a form where I have a tab control, this tab control has 3 tabs. I am trying to show a form with each tab page. I am able to show a form on the first tab, but for some reason, the other two tabs do not load the forms. This is the code that I have. Am I doing something wrong? Do you have any suggestions?
private void MainTab_Load(object sender, EventArgs e)
{
HomeScreen fHome = new HomeScreen();
fHome.TopLevel = false;
fHome.Visible = true;
fHome.FormBorderStyle = FormBorderStyle.None;
fHome.Dock = DockStyle.Fill;
MainOptions.TabPages[0].Controls.Add(fHome);
}
private void CustomerTab_Click(object sender, EventArgs e)
{
CustomerScreen fCustomer = new CustomerScreen();
fCustomer.TopLevel = false;
fCustomer.Visible = true;
fCustomer.FormBorderStyle = FormBorderStyle.None;
fCustomer.Dock = DockStyle.Fill;
MainOptions.TabPages[1].Controls.Add(fCustomer);
}
edit:
More:
Also, in the tabcontrol- InitializeComponent I have the following
// HomeTab
//
this.HomeTab.Location = new System.Drawing.Point(4, 22);
this.HomeTab.Name = "HomeTab";
this.HomeTab.Size = new System.Drawing.Size(677, 452);
this.HomeTab.TabIndex = 0;
this.HomeTab.Text = "Home";
this.HomeTab.UseVisualStyleBackColor = true;
this.HomeTab.Click += new System.EventHandler(this.MainTab_Load);
//
// CustomerTab
//
this.CustomerTab.Location = new System.Drawing.Point(4, 22);
this.CustomerTab.Name = "CustomerTab";
this.CustomerTab.Padding = new System.Windows.Forms.Padding(3);
this.CustomerTab.Size = new System.Drawing.Size(677, 452);
this.CustomerTab.TabIndex = 1;
this.CustomerTab.Text = "Customer";
this.CustomerTab.UseVisualStyleBackColor = true;
this.CustomerTab.Click += new System.EventHandler(this.CustomerTab_Click);
I think you're just missing the Show() method...
And a suggestion: I don't know what kind of Pattern you're using, but as a beginner, you should declare your forms outside those methods, as a variable within the class scope, so you can access then later...
private HomeScreen fHome = new HomeScreen();
private CustomerScreen fCustomer = new CustomerScreen();
private void MainTab_Load(object sender, EventArgs e)
{
fHome.TopLevel = false;
fHome.Visible = true;
fHome.FormBorderStyle = FormBorderStyle.None;
fHome.Dock = DockStyle.Fill;
MainOptions.TabPages[0].Controls.Add(fHome);
fHome.Show(); // add this
}
private void CustomerTab_Click(object sender, EventArgs e)
{
fCustomer.TopLevel = false;
fCustomer.Visible = true;
fCustomer.FormBorderStyle = FormBorderStyle.None;
fCustomer.Dock = DockStyle.Fill;
MainOptions.TabPages[1].Controls.Add(fCustomer);
fCustomer.Show(); // add this
}
There's a lot to improve here, but that's a start.

Create a button which creates button

I am a begginer in C# and I want to create a button which creates button.
but these buttons never appear...
please find my code :
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
}
Thanks a lot
You have to add the button (or any other controls) on the form using the Controls property, for sample:
private void addstrat3_i_Click(object sender, EventArgs e)
{
panel3strat.Width += 200;
Button addstrat3_2 = new Button();
addstrat3_2.Size = new Size(210, 41);
addstrat3_2.Location = new Point(50,50);
addstrat3_2.Visible = true;
// add control
this.Controls.Add(addstrat3_2);
}
You need to add the button to the form.
this.Controls.Add(addstrat3_2);

How to use Multiple forms in one Csharp panel in one Windows Form panel?

I am building a kids learning application, where clicking on a button on panel, I want to show different forms in the same place of the panel. Can you please help with any walk-through or tutorial links?
This question should have been posted on Stackoverflow website rather than here.
But you can use this approach to handle the case.
subForm = new SubFormYouWantToLoad();
subForm.TopLevel = false;
subForm.FormBorderStyle = FormBorderStyle.None;
ContainerPanel.Controls.Add(subForm , 0, 1);
subForm .Visible = true;
You can add this code when you click on the specific button.
Here each subform is added to the Panel as a Control. You should remove the subform from the panel's control list before adding another subform. For this ,it is better to remove,close and dispose the first one.
ContainerPanel.Controls.Remove(activeform);
activeform.Close();
activeform.Dispose();
Instead of Forms use user controls and load them in to panels
Sample if you want to show usercontrol1
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr1 = new UserControl1();
usr1.Show();
panel1.Controls.Add(usr1);
If usercontrol2
panel1.Controls.Clear();
panel1.Visible = true;
UserControl1 usr2 = new UserControl2();
usr2.Show();
panel1.Controls.Add(usr2);
You could create a number of forms as user controls or a control that inheriets from a panel. Then have a parent form with a panel to hold the user controls. You can then change the active user control in the container when the panel needs to be changed.
There is a tutorial on msdn for creating user controls.
http://msdn.microsoft.com/en-us/library/a6h7e207(v=vs.71).aspx
I used this code to close the form on the panel but not worked..
private void button12_Click(object sender, EventArgs e)
{
dontShowPANEL();
//ActiveForm.Close();
MainImaginCp kj = new MainImaginCp();
//kj.Visible = false;
kj.panel2.Controls.Clear();
panel1.Visible = true;
EngABCLearning usr1 = new EngABCLearning();
usr1.Show();
kj.panel2.Controls.Add(usr1);
//kj.Focus();
}
And I used the following code to show the form in the panel.
private void toolStripMenuItem1_LR_ENG_Click(object sender, EventArgs e)
{
//kids.Form2 hj = new kids.Form2();
//hj.Show();
EngABCLearning gh = new EngABCLearning();
//gh.Show();
gh.TopLevel = false;
gh.FormBorderStyle = FormBorderStyle.None;
//Panel2.Controls.Add(subForm, 0, 1);
panel2.Controls.Add(gh);
gh.Visible = true;
}
This is closing my main form and exiting the application.
try this out i have loaded two forms inside a single panel
private void Form1_Load(object sender, EventArgs e)
{
Form2 f1 = new Form2();
f1.TopLevel = false;
f1.AutoScroll = true;
panel1.Controls.Add(f1);
f1.Dock = DockStyle.Left;
f1.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f1.Show();
//form2
Form3 f2 = new Form3();
f2.TopLevel = false;
f2.AutoScroll = true;
panel1.Controls.Add(f2);
f2.Dock = DockStyle.Left;
f2.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
f2.Show();
}
try this i used this method to load multiple forms at one panel
private Form activeForm = null;
public void FormLoad(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
panelName.Controls.Add(childForm);
childForm.Visible = true;
}
private void YourBtn1_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm1Name());
}
private void YourBtn2_Click(object sender, EventArgs e)
{
FormLoad(new youWantToLoadForm2Name());
}

Categories