Click here to Skip to main content
15,905,558 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Ok, a created a program which includes twoo forms.
Form1 is named KlientForm and Form2 is named GjendjaForm.

In KlientForm I populated the ListBox if GjendjaForm with following instantiation
C#
GjendjaForm frmGje = new GjendjaForm(emriBox.Text, mbiemriBox.Text, xhirollogariaBox.Text, statusi);
               frmGje.Show();

Also in Form2 (GjendjaForm) I created a buton and gave this Function:
C#
private void kthehuButton_Click(object sender, EventArgs e)
        {
            var frmKl = new KlientForm();
            this.Close();
            frmKl.Show();
        }

This in GjendjaForm is OK, I can Close the Form and Open the other Form.

My question:
What do I need to write in KlientForm to open the GjendjaForm and Close the ClientForm.

I gived a try with following but it does'nt works:
C#
private void GjendjaButon_Click(object sender, EventArgs e)
        {
            if (statusi == "Aktiv")
            {
               
   GjendjaForm frmGje = new GjendjaForm(emriBox.Text, mbiemriBox.Text, xhirollogariaBox.Text, statusi);
                frmGje.Show();
               this.Visible = false;
               this.Hide();
               this.Close();
               this.Dispose();

               
            }
            else
            {}
         }
Posted

1 solution

First of all, you should understand that the jargon term "open a form" is misleading; it suggests that something closed can be "opened". This is not what happens. The form instance is created, and then the form is shown. Moreover, you cannot "open" (use in any reasonable way) some form instance that was closed; the form gets disposed and hence unusable.

In view of that, if you need to replace one form with another, you need to capture the attempt to close a form and hide it instead. In this Gjendja, your Klient will get an Përshtypje that the form is closed, but, in contrast to really closed form, will be able to get Kthehu and show this form again. :-).

Here is how:
C#
public partial class MyForm { // class MyForm : System.Windows.Forms.Form is usually in a different partial class part, don't repeat base class

    protected virtual void OnFormClosing(System.Windows.Forms.FormClosingEventArgs e) {
        if (e.CloseReason == System.Windows.Forms.CloseReason.UserClosing) { // in other cases, it's good to actually close it
            e.Cancel = true; // prevent actual closing
            this.Hide();
        } // if
    } // OnFormClosing

} //class MyForm


However, I strongly recommend to avoid development of multi-form applications in most cases. Usually, it's much better to have only one form. All you implement as a form now, you can implement as some parent control as panel. Navigation between different views (what you now have as different forms) could be done by hiding one panels and showing others. Another good option is using TabControl. What you have as the forms now can became tab pages. Besides, this is actually simpler.

—SA
 
Share this answer
 
Comments
Abhinav S 17-Jan-13 23:24pm    
5!
Sergey Alexandrovich Kryukov 18-Jan-13 1:03am    
Thank you, Abhinav.
—SA
dr_iton 19-Jan-13 16:51pm    
Ok I slved the problem, but i Have another problem.
To add a client in the list I created the following Method:
ListaKlientet.Add(new KlasaKlientet(emriBox.Text, mbiemriBox.Text, dateTimePicker.Text,
vendlindjaBox.Text, xhirollogariaBox.Text, statusi));

The problem is that the xhirollogariaBox is a random number like this:
string xhiro;
Random rasti = new Random();
int numri = rasti.Next(10000000, 99999999);
xhiro = "1301" + numri;
xhirollogariaBox.Text = Convert.ToString(xhiro);

Is there a way that I can check if the random numer already exists in the LstBox.
Sergey Alexandrovich Kryukov 19-Jan-13 17:43pm    
Before we go there: are you accepting the answer formally (green button)? — thanks.
—SA
Sergey Alexandrovich Kryukov 19-Jan-13 17:44pm    
[OP commented:]

I finished my Project and making another Project it takes mee time. So you say that one solution it could be to show one Form and Hide the other one. I had in my mind to solve with that solution and I gived a try, but the problem is when I open the other form and when I get back to present form the Debuging is not stoped even if I close the Form, so I have Manualy to stop debiuging.

Once again thank you for your reply Mr.Sergey (спасибо)

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