Denis Morgan

Denis Morgan

  • NA
  • 72
  • 22.2k

How to close current form in C# and open the second form as modal dial

Sep 26 2020 4:24 AM
I have a scenario in c# where I open first form dialog log call it form one. Form one has a button which should open Form two. My issue is upon clicking button in form one I want to close form one and show form two as modal dialog and form two shows and still form one is not closed. Here is sample code I have tried which is not working
  1. private void button1_Click(object sender, EventArgs e)    
  2. {    
  3.     Form frm=null;    
  4.     if (radioone.Checked == true)    
  5.     {               
  6.         frm=new frmPayByCash(this);    
  7.     }    
  8.     else if (radiotwo.Checked == true)    
  9.     {        
  10.         frm = new frmTwo(this);               
  11.     }    
  12.     else    
  13.     {           
  14.         frm = new frmOne(this);    
  15.     }    
  16.     if (frm != null)    
  17.     {    
  18.         this.Hide();             
  19.         frm.Closed += (s, args) => this.Close(); //have tried this but its closing both forms at ago
  20.         frm.Show();          
  21.     }  
  22. } 

Answers (5)