Click here to Skip to main content
11,575,921 members (59,109 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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
 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:
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:
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 17-Jan-13 13:21pm
dr_iton651

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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. Smile | :) .

Here is how:
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
  Permalink  
Comments
Abhinav S at 17-Jan-13 23:24pm
   
5!
Sergey Alexandrovich Kryukov at 18-Jan-13 1:03am
   
Thank you, Abhinav.
—SA
dr_iton at 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 at 19-Jan-13 17:43pm
   
Before we go there: are you accepting the answer formally (green button)? — thanks.
—SA
Sergey Alexandrovich Kryukov at 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 (спасибо)
Sergey Alexandrovich Kryukov at 19-Jan-13 17:46pm
   
You are welcome.
Unfortunately, I had to remove your post, because it was misplaced. I did not receive notification. You need to comment or reply on any person's post to get noticed. More importantly, "Add your solution here" is reserved for the cases when you try to provide some help, in response to other member's question.
—SA
Sergey Alexandrovich Kryukov at 19-Jan-13 17:48pm
   
This is just the matter where you put you break point. Sometimes you need to add some code only for debugging purpose. "Get back" may mean: 1) show the form (unhide it), 2) activate it. For a closing form or the one about to be closed, you have events: 1) FormClosing; 2) FormClosed. Is that clear?
—SA
dr_iton at 20-Jan-13 4:57am
   
Yes it is clear. Once again in my former Comment I asked a question and I'm realy stucked. If I make another input via xhirollogariaBox, how to check if the random nuber already exists in the listbox. xhirollogariaBox is filled with following method:
Random rasti = new Random();
int numri = rasti.Next(10000000, 99999999);
xhiro = "1301" + numri;
xhirollogariaBox.Text = Convert.ToString(xhiro);


Once again thank you for your reply and your clarifications, because as you see I'm a beginner in this forum.
Cheers.
Sergey Alexandrovich Kryukov at 20-Jan-13 13:01pm
   
First of all, will you accept this answer formally (green button)? — thanks.

What's the problem with random? Normally, you should ask a separate question.
Why "1301" + numri. What string+int could possibly mean? You cannot compile it. What "convert". You need numri.ToSting, and then work with strings. But why?

And, try not to go into the fallacy very usual these days: working with strings representing data instead of data.
—SA
dr_iton at 23-Jan-13 5:22am
   
I solved the problem about checking if the string already exists in the listbox with following:
public void Xhirollog()
{
string xhiro;
Random rasti = new Random();
int numri = rasti.Next(10000000, 99999999);
xhiro = "1301" + numri;
bool ekziston = false;
foreach (string numer in ListBox.Items)
{
if (numer == xhiro)
{
ekziston = true;
break;
}
}
if (!ekziston)
xhirollogariaBox.Text = Convert.ToString(xhiro);
}
The reason that I convert the data to string is because i store them in a text file using StreamWriter and reading them using StreamReader.
Problem solved and thank you for your reply.

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 330
1 F-ES Sitecore 145
2 OriginalGriff 125
3 Dave Kreskowiak 120
4 Abhinav S 98
0 OriginalGriff 705
1 Sergey Alexandrovich Kryukov 600
2 Abhinav S 438
3 F-ES Sitecore 410
4 Suvendu Shekhar Giri 365


Advertise | Privacy | Mobile
Web03 | 2.8.150603.1 | Last Updated 18 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100