Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone ..
I have a program that content two form
the first form is the main form and is the form that the program will start form it
and the second form is the form that will show one time only to take to values and then will comeback to the first and the second will close
note :in the start the program will do one from two

1- show second form if the values not in the program , then close the second and go to the first.
2- show the first and counties on it if the program have the values .

Note: when the second form will appear the first form will be hide , then after program takes the values from the user the second form will close and the first form will show

in anther word if the user give to the program the values from before the second form will not appear ,just the first form will appear only

What I have tried:

Code of FORM1
SqlConnection con;
        public Form1()
        {
            InitializeComponent();
            con = new SqlConnection(@"server=.\;database=Super;Integrated Security=true");
            b();
 
        }

 private void b()
        {
            Open();
            String SqlQuery = "SELECT * from test";
            DataTable dt = new DataTable();
            dt = fetchData(SqlQuery);
            if (dt.Rows.Count == 0)
            {
                MessageBox.Show("incorect user");
                Form2 fm2 = new Form2();
                fm2.Show();
                this.Hide();
            }
        }



Code FORM2

SqlConnection con;
       public Form2()
       {
           InitializeComponent();
           con = new SqlConnection(@"server=.\;database=Super;Integrated Security=true");
           Open();
       }

       public void fn()
       {

           String SqlQuery = "SELECT * from test ";
           DataTable dt = new DataTable();
           dt = fetchData(SqlQuery);
           if (dt.Rows.Count != 0)
           {
               Application.OpenForms[0].Show();
               this.Close();

           }


       }

       private void button1_Click(object sender, EventArgs e)
       {

           string insertQuery = "INSERT INTO test( name, phone) VALUES" +
               "(" + (textBox1.Text) + "," + (textBox2.Text) + ")";
           ExcuteQuery(insertQuery);
           MessageBox.Show("DONE");

           fn();
       }




the error is when I start run the program the two forms appears to the screen and the second form cant close after show the first form appears
Posted
Updated 15-May-21 2:06am
Comments
[no name] 14-May-21 18:43pm    
There seems no point in hiding Form1. It also seems Form2 is modal; you should use ShowDialog.
Hala Hani 14-May-21 19:20pm    
@Gerry
but I don't want to use ShowDialog!

Your problem is probably that form2 is only visible in the scope of method b().
Form2 fm2 = new Form2();

So it would be better to place the declaration above public Form1() like this:
private Form2 fm2;

public Form1()
{ ... }

And then use this in method b():
fm2 = new Form2();
 
Share this answer
 
Comments
Hala Hani 15-May-21 5:38am    
It did not work. There is still a problem. Form 1 and 2 open at the same time and also when finished taking values from form2 , form 2 does not disappear.
RickZeeland 15-May-21 6:17am    
Did you modify the Program.cs file maybe?
Firstly you are running code from inside the Form1 constructor, the constructor is used to initialise the form NOT run code, if you want to run code when the form is created it should be done in the Load event.

Secondly you should really send an event from the second form to any other form listing that Form2 has loaded, here is where you should signal to Form1 that it needs to hide.

And as Gerry suggested you should be using the ShowDialog() rather than Show().

Using events will solve you problems

see below

Form1 Code example
public partial class Form1 : Form
{
    Form2 form2;

    public Form1()
    {
        InitializeComponent();
        //
        // I have wired up the events manually so that you can see what events are required
        //
        Load += Form1_Load;
    }

    private void Form1_Load(object sender, EventArgs e)
    {
         // remove comments below to populate the form1 text box

         // Form1TextBox.Text = "Some text";

        if (Form1TextBox.Text == string.Empty)
        {
            form2 = new Form2();
            //
            // Wire up event handlers to recieve events from form 2
            //
            form2.form2Opened += Form2HasOpened;
            form2.form2Closed += Form2HasClosed;
            form2.ShowDialog();
        }
    }

    // This will fire when form 2 has initialised
    private void Form2HasOpened(object sender, EventArgs e)
    {
        Hide();
    }

    // This will fire when form 2 is about to close
    private void Form2HasClosed(object sender, EventArgs e)
    {
        Show();
        Form1TextBox.Text = form2.Form2Data;
        form2.Dispose();
    }
}


Form2 Code Example
public partial class Form2 : Form
{
    public event EventHandler form2Closed;
    public event EventHandler form2Opened;

    public Form2()
    {
        InitializeComponent();

        //
        // I have wired up the events manually so that you can see what events are required
        //
        Load += Form2_Load;
        MyButtonClose.Click += MyButtonClose_Click;

    }

    private void Form2_Load(object sender, EventArgs e)
    {
        // check we have wired up listeners
        if (form2Opened != null)
        {
            // we should have something listening so invoke the event to let the listeners know we have loaded
            form2Opened.Invoke(this, new EventArgs());
        }
    }

    private void MyButtonClose_Click(object sender, EventArgs e)
    {
        Form2Data = Form2TextBox.Text;

        // check we have wired up listeners
        if (form2Closed != null)
        {
            // we should have something listening so invoke the event to let the listeners know we are closing
            form2Closed.Invoke(this, new EventArgs());
        }
    }

    public string Form2Data { get; set; }
}
 
Share this answer
 
Comments
BillWoodruff 16-May-21 0:38am    
think about the difference between educating the OP and writing their code for them
Hala Hani 4-Jun-21 10:53am    
what is form2Opened ,form2Closed ??
I don't understand
Tony Hill 4-Jun-21 12:51pm    
Form2 has two public events called 'form2Opened' and 'form2Closed', other forms can ask
to be notified when these events are raised.

I will only discuss the 'form2Opened' to not complicate matters.

In the Form1 Load event handler method Form1 registers an interest in being notified when
either of these events are raised and the method which should be called when the event is raised.

In the Form1 'Form1_Load' method we create Form2 and register an interest in the 'form2Opened' event and display the Form2.

private void Form1_Load(object sender, EventArgs e)
{
...
...
...
form2 = new Form2();
form2.form2Opened += Form2HasOpened; // (1)
form2.ShowDialog();
}

After the call to ShowDialog() in the Form1 'Form1_Load' method the 'Form2_Load' method
executes, the first thing that happens is Form2 checks to see if anything is listening
to the Form2 form2Opened event by testing to see if it is null or not, if it is not null
the form2Opened event is invoked to notify Form1 that Form2 has loaded.

private void Form2_Load(object sender, EventArgs e)
{
if (form2Opened != null)
{
form2Opened.Invoke(this, new EventArgs()); // This will notify Form1 that Form2 has opened and that Form1
// should execute the Form1.Form2HasOpened method.
}
}

Control will now be passed to the Form1.Form2HasOpened method (see (1) above) and the code in the
method is executed which in the example will tell Form1 to hide itself with Hide() method,

private void Form2HasOpened(object sender, EventArgs e)
{
Hide();
}

After the Form1.Form2HasOpened method has finished executing control is returned
to Form2.Form2_Load method where the event was invoked.

Much the same sort of thing happens with 'form2Closed' event but you should be able
work it out yourself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900