Close previous windows form when clicked other button - c#

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;
}

Related

Only One Form is visible in the panel whichever is called first

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();
}
}
}
}

Enable menu Strip in child form using c#

how to enable menu strip in child form?
i just want to enable the menu button strip when i will close the child form
how to code that in child form?
Student....
private void tsmNewEmp_Click(object sender, EventArgs e)
{
if(NewEmp == null)
{
NewEmp = new NewEmployee();
NewEmp.MdiParent = this;
}
NewEmp.Show();
tsmNewEmp.Enabled = false;
tsmNewContract.Enabled = false;
}
You can use the FormClosed event to update the buttons in the parent form after the child is closed:
private void tsmNewEmp_Click(object sender, EventArgs e)
{
if(NewEmp == null)
{
NewEmp = new NewEmployee();
NewEmp.MdiParent = this;
NewEmp.FormClosed += FormClosed_1;
}
NewEmp.Show();
tsmNewEmp.Enabled = false;
tsmNewContract.Enabled = false;
}
private void FormClosed_1(object sender, FormClosedEventArgs e)
{
tsmNewEmp.Enabled = true;
tsmNewContract.Enabled = true;
}
Control[] controls = this.MdiParent.Controls.Find("Menu", true);
foreach (Control ctrl in controls)
{
if (ctrl.Name == "Menu")
{
MenuStrip strip = ctrl as MenuStrip;
strip.Items["login"].Enabled = false;
strip.Items["logout"].Enabled = false;
}
}

Top-level control cannot be added to a control

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);
}

C# - How to save a string entered in Form2 in Form1

I get the default path from the registry for the Steam installation. But if someone has their games installed to a different folder, the user has to enter it in the Configure form. When the form gets closed, that path entered(from the folder browser or by typing the path in manually) should get saved to a string in the main form and should enable a different Combobox which turns on different buttons. I somehow managed to do the save to the mainform string, but the 2nd combobox doesn't seem to turn on. How can I do it correctly?
** MAIN FORM **
public string NewPath { get; set; }
private ConfigForm otherForm;
string InstallPath = (string)Registry.GetValue(#"HKEY_CURRENT_USER\SOFTWARE\Valve\Steam", "SteamPath", null);
private void PortalHammerButton_Click(object sender, EventArgs e)
{
Process.Start(InstallPath + #"\SteamApps\common\Portal\bin\hammer.exe");
}
private void Gamedropdown_SelectedIndexChanged(object sender, EventArgs e)
{
if (Gamedropdown.Text == "Portal") // When Portal is selected
{
// Enable the Portal SDK buttons
PortalHammerButton.Visible = true;
PortalModelViewerButton.Visible = true;
PortalFacePoserButton.Visible = true;
// Disable the CS:GO SDK buttons
csgoFacePoserButton.Visible = false;
csgoHammerButton.Visible = false;
csgoModelViewerButton.Visible = false;
}
else if (Gamedropdown.Text == "CS:GO") // When CS:GO is selected
{
// Disable Portal SDK buttons
PortalHammerButton.Visible = false;
PortalModelViewerButton.Visible = false;
PortalFacePoserButton.Visible = false;
// Enable CS:GO SDK buttons
csgoFacePoserButton.Visible = true;
csgoHammerButton.Visible = true;
csgoModelViewerButton.Visible = true;
}
}
private void ConfigureButton_Click(object sender, EventArgs e)
{
var configdialog = new ConfigForm();
configdialog.Show();
}
private void PortalDifferentHammerButton_Click(object sender, EventArgs e)
{
Process.Start(NewPath + #"\SteamApps\common\Portal\bin\hammer.exe");
}
private void NewDropDown_SelectedIndexChanged(object sender, EventArgs e)
{
if (NewDropDown.Text == "Portal") // When Portal is selected
{
// Enable the Portal SDK buttons
PortalDifferentHammerButton.Visible = true;
PortalDifferentModelViewerButton.Visible = true;
PortalDifferentFacePoserButton.Visible = true;
// Disable the CS:GO SDK buttons
DifferentCSGOFaceposerButton.Visible = false;
DifferentCSGOHammerButton.Visible = false;
DifferentCSGOModelViewerButton.Visible = false;
}
else if (NewDropDown.Text == "CS:GO") // When CS:GO is selected
{
// Disable the Portal SDK buttons
PortalDifferentFacePoserButton.Visible = false;
PortalDifferentHammerButton.Visible = false;
PortalDifferentModelViewerButton.Visible = false;
// Enable the CS:GO SDK buttons
DifferentCSGOModelViewerButton.Visible = true;
DifferentCSGOHammerButton.Visible = true;
DifferentCSGOFaceposerButton.Visible = true;
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.Close();
}
}
}
**CONFIGURE FORM**
public partial class ConfigForm : Form
{
public ConfigForm()
{
InitializeComponent();
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
public void DifferentFolderBrowseButton_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult result = fbd.ShowDialog();
string newpath = fbd.SelectedPath;
NewPathBox.Text = newpath;
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
public void CloseButton_Click(object sender, EventArgs e)
{
this.Hide();
Form1 frm1 = new Form1();
frm1.Gamedropdown.Visible = false;
frm1.NewDropDown.Visible = true;
}
}
}
Any help would be appriciated.
Look at your ConfigForm. Here's your problem:
public ConfigForm()
{
InitializeComponent();
Form1 frm1 = new Form1();
frm1.NewPath = NewPathBox.Text;
}
What you're doing on your Form1 (which I'm guessing is your Main form) is creating a new instance of your ConfigForm and showing it. What you're doing in your ConfigForm is creating a new main form and setting the NewPath = to the value entered on your config form. The problem is this new Form1 is NOT the Form1 that created the ConfigForm. The Form1 that created your config form is not the one getting updated by your code, some arbitrary new Form1 that you create is the one getting updated. This is why your code isn't working as you expected.
Here's the approach I would take. Add a NewPath variable to your ConfigForm just like you have in Form1. Then, add a FormClosing method to FormConfig. Do something like this:
private void ConfigForm_FormClosing(object sender, FormClosingEventArgs e)
{
NewPath = NewPathBox.Text;
}
Then, change your code on Form1 to this:
private void button1_Click(object sender, EventArgs e)
{
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.ShowDialog();
this.NewPath = cfgfrm.NewPath;
}
What this code is doing is creating and showing a new ConfigForm on your Form1 when you click button1. Then, when your user closes the FormConfig, the form saves the textbox value to the NewPath variable on the FormConfig. Then, once the form is closed, the code on Form1 resumes. Form1 then looks at the NewPath value that was saved when the user closed the FormConfig. Form1 grabs this new NewPath value and puts it in its own NewPath variable.
EDIT
To show/hide comboboxes:
private void button1_Click(object sender, EventArgs e)
{
ConfigForm cfgfrm = new ConfigForm();
cfgfrm.ShowDialog();
this.NewPath = cfgfrm.NewPath;
Gamedropdown.Visible = false;
NewDropDown.Visible = true
}
I'm not sure why you are making the secondary form so complicated. You don't need a pointer to it after you use it and you should be closing instead of hiding. Try this:
class ConfigForm : Form
{
public string newPath = null;
public void CloseButton_Click(object sender, EventArgs e)
{
newPath = NewPathBox.Text;
}
public void CloseButton_Click(object sender, EventArgs e)
{
Close();
}
}
...and in your main form:
public partial class Form1 : Form
{
string steamPath = null; // set to starting path
private void ConfigureButton_Click(object sender, EventArgs e)
{
bool valueChanged = false;
using (ConfigForm form = new ConfigForm())
{
form.newPath = null;
form.ShowDialog();
if (form.newPath != null)
{
steamPath = form.newPath;
valueChanged = true;
}
}
if (valueChanged)
{
// here is where you would handle reloading and changing the ComboBoxes
}
}
}
This will more cleanly return the new string. Whatever you want to do as a result of having changed the path can be done after you have disposed of the config form (bringing it up with the "using" conditional automatically does the disposal for you)

Groupbox for Windows form

I have created gui for windows form....!
one side i have groupbox with checkbox and two buttons ">>" and "<<" and on the other side i have one more groupbox.
I need to select one checkbox at a time if the user select more than one checkbox i need to raise error..
if the user selects checkbox and click on ">>" button i need to display number of messages in the other groupbox i.e in the "List of Selected Commands"...
and on the click of ">>" the selected list of message should be deleted from list.
I have included tab control in my gui on click of this tab i need to display some list of commands how can i do it...
can any one help me on this...
This is the code..
namespace Menu_Sample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
groupBox1.Text = "MSC";
groupBox2.Text = "List Of Selected Commands";
checkBox1.Visible = false;
cb2.Visible = false;
cb3.Visible = false;
cb4.Visible = false;
label1.Visible = false;
label2.Visible = false;
label3.Visible = false;
label4.Visible = false;
}
private void submenu1ToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "ICP";
checkBox1.Visible = true;
cb2.Visible = true;
cb3.Visible = true;
cb4.Visible = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
label1.Visible = true;
label1.Text = "ibit";
groupBox2.Controls.Add(label1);
label2.Visible = true;
label2.Text = "Cbit";
groupBox2.Controls.Add(label2);
label3.Visible = true;
label3.Text = "Kbit";
groupBox2.Controls.Add(label3);
label4.Visible = true;
label4.Text = "ibit";
groupBox2.Controls.Add(label4);
}
if (cb2.Checked == true)
{
label1.Visible = true;
label1.Text = "ibit";
groupBox2.Controls.Add(label1);
label2.Visible = true;
label2.Text = "Cbit";
groupBox2.Controls.Add(label2);
label3.Visible = true;
label3.Text = "Kbit";
groupBox2.Controls.Add(label3);
label4.Visible = true;
label4.Text = "ibit";
groupBox2.Controls.Add(label4);
}
}
private void btn6_Click(object sender, EventArgs e)
{
if (label1.Enabled==true)
{
label1.Text = "";
}
}
private void btn5_Click(object sender, EventArgs e)
{
label1.Text = "";
label2.Text = "";
label3.Text = "";
label4.Text = "";
}
private void submenu2ToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "MCP";
}
private void mDPToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "MDP";
}
private void mRPPToolStripMenuItem_Click(object sender, EventArgs e)
{
groupBox1.Text = "MRPP";
}
}
}
I am giving you an idea, that how we can do that.
You have to create a group box click event and then use loop for the controls in that groupbox and if that control is checkbox and it is checked, then you just count that no-other checkbox should be checked.
I will be available with the code very shortly.

Categories

Resources