I have 3 forms named as masterform , mainform and confirmform . a confirm form has two buttons yes or no . Both of these forms will be used multiple times in my project so i decided to create a function so as to avoid code duplicity . the code is as follows
Masterform.cs
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// get reference of Panel
Panel masterpanelrefernece = GetPanelInstance();
// if the user presses escape , a mainform should open
if (keyData == (Keys.Escape))
{
CreateForms(True , Color.Yellow);
}
return base.ProcessCmdKey(ref msg, keyData);
}
public void CreateForms( bool boolVar, Color colorVar)
{
if(boolVar ==true)
{
mainform for1 = new mainform();
for1.BackColor = Color.Yellow;
for1.Dock = DockStyle.Fill;
for1.Show();
}
using (confirmform mx = new confirmform ()) //
{
mx.StartPosition = FormStartPosition.CenterParent; //
mx.ShowInTaskbar = false; //
mx.Show();
if (mx.DialogResult == DialogResult.Cancel) ---> error
{
mx.Close();
}
}
for1.close();
for1 = null;
explaination : what i am trying is when the user on the masterform , presses escape, a form should be opened named as mainform with backcolor as yellow (only if boolVar is set to true) and after that another form :-confirmform should opened . this confirmform has two buttons yes or no . i am stuck in the logic of how to get to know user clicked which button . if the user clicked yes button then the application should terminate and if the user presses no , both the mainform and the confirmform should close .
public confirmform ()
{
InitializeComponent();
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Enter) || keyData == (Keys.Y))
{
Application.ExitThread();
return true;
}
else if (keyData == (Keys.N) || keyData == (Keys.Escape))
{
this.Close();
return false;
}
return base.ProcessCmdKey(ref msg, keyData);
}
private void button1_Click(object sender, EventArgs e)
{
Application.ExitThread();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}