Share via

Help in Function

Nusrat Bharucha 140 Reputation points
2023-11-24T09:21:34.8233333+00:00

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();
        }
Developer technologies | Windows Forms
Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Anonymous
2023-11-24T10:19:47.1766667+00:00

Hi @Nusrat Bharucha , Welcome to Microsoft Q&A,

I don't understand your code.

But your needs can pass, just use DialogResult.

You can put the confirmform into the mainform's window to determine whether it is open.

Use the result of mainform to determine whether to close.

Best Regards,

Jiale


If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". 

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.