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();
}
}
Related
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();
}
When I'm trying to add a drop-down menu to my form in a wizard, it gives following error.
Here are the code lines I have written for drop-down menu.
dropDownMenu1 = new ToolStripDropDownMenu();
dropDownMenu1.Location = new System.Drawing.Point(90, 45);
dropDownMenu1.Size = new System.Drawing.Size(70, 20);
this.Controls.Add(dropDownMenu1);
Update
This solution didn't fix my problem. I have used following code already. But still gives the same error.
UserInputForm inputForm= new UserInputForm();
inputForm.TopLevel = false;
inputForm.ShowDialog();
On the basis of your code,you also have to set Items for dropDownMenu1 and set it to display after you set dropDownMenu1.TopLevel = false;. You could try to refer to the following code.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ToolStripDropDownMenu dropDownMenu1 = null;
private void Form1_Load(object sender, EventArgs e)
{
dropDownMenu1 = new ToolStripDropDownMenu();
dropDownMenu1.Items.Add("item1");
dropDownMenu1.Items.Add("item2");
dropDownMenu1.Items.Add("item3");
dropDownMenu1.Location = new Point(90, 45);
dropDownMenu1.Size = new System.Drawing.Size(70, 20);
dropDownMenu1.TopLevel = false;
this.Controls.Add(dropDownMenu1);
}
protected override void OnSizeChanged(EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
//_formContextMenu or this.contextMenuStrip1
dropDownMenu1.Visible = true;
dropDownMenu1.Close();
}
base.OnSizeChanged(e);
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
dropDownMenu1.Show(e.Location);
}
}
The result:
If there is not specific goal for using ToolStripDropDownMenu you can also use ContextMenuStrip component and Set ContextMenuStrip property of form. This way you don't need to write code to show context menu.
For more usage methods of ToolStripDropDownMenu, you can refer to ToolStripDropDownMenu Class and ToolStripDropDown Class.
I have a button in my dashboard form opening another form called entry, but when entry is minimised and if i click again on the open button in dashboard another window is opening instead of maximising the same minimised form. i am very new to this visual studio 2019 & c#.
private void Btngotoentryform_Click(object sender, EventArgs e)
{
FrmDataEntry f = new FrmDataEntry();
f.Show();
}
Create a global instance of the form on which you can perform nullcheck
private FrmDataEntry _instance = null;
private void Btngotoentryform_Click(object sender, EventArgs e)
{
if(_instance == null)
{
_instance = new FrmDataEntry();
_instance.Show();
}
else
{
_instance.WindowState = WindowState.Maximized;
_instance.Activate();
}
}
When user changes background color for example, the Settings.settings file is modified. And it works.
But the application doesn't change it's background color after user clicks OK.
It works only when I close and build the application again.
How can I reload my form or user control on button click? (Tried with .Refresh(), but it doesn't work)
private void refreshSettings()
{
this.BackColor = Properties.Settings.Default.bgdColor;
this.Font = Properties.Settings.Default.fontType;
this.ForeColor = Properties.Settings.Default.fontColor;
}
private void Settings_Load(object sender, EventArgs e)
{
refreshSettings();
bgdColorLBL.BackColor = Properties.Settings.Default.bgdColor;
fontColorLBL.BackColor = Properties.Settings.Default.fontColor;
fontTypeLBL.Font = Properties.Settings.Default.fontType;
fontTypeLBL.Text = Properties.Settings.Default.fontType.Name;
}
private void okBTN_Click(object sender, EventArgs e)
{
LeagueUC lg = new LeagueUC();
InitializeComponent();
this.Close();
}
private void bgdColorLBL_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.Color = Properties.Settings.Default.bgdColor;
if (dlg.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.bgdColor = dlg.Color;
Properties.Settings.Default.Save();
bgdColorLBL.BackColor = dlg.Color;
}
}
Run whatever code you have that sets the control's properties at start up from the settings file.
e.g.
private void bgdColorLBL_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
dlg.Color = Properties.Settings.Default.bgdColor;
if (dlg.ShowDialog() == DialogResult.OK)
{
Properties.Settings.Default.bgdColor = dlg.Color;
Properties.Settings.Default.Save();
Settings_Load(null, null);
}
}
On the button click event, just load the backcolor from your settings file. Something like:
this.BackColor = Properties.Settings.Default.Color;
You can create binding for it. With a little tricks the binding can even allow the immediate interface language switching.
try this, this changes background color of form in color that you chose from ColorDialog:
private void button2_Click(object sender, EventArgs e)
{
ColorDialog dlg = new ColorDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
this.BackColor = System.Drawing.Color.FromName(dlg.Color.Name);
}
}
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());
}