c# how do i open a window on doubleclick once it's closed?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xinnie
    New Member
    • Jun 2010
    • 12

    c# how do i open a window on doubleclick once it's closed?

    Hi,

    I'm so new at this so my question might sound a bit odd.
    Alright, I'm trying to write a chat program that'll only work amongst the workers within the same company and network as msn and such messangers are prohibited.

    I have made this form where you have nicknames. And I have written the command for preventing a window to be opened more than once when you doubleclick on it. But the problem is, when i close the popped query, and try opening it again with doubleclick it just does nothing. how do i solve that?PS: Sorry for my english.

    Code:
    ListViewItem SecilenKisi;
    
            private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
            {
                SecilenKisi = e.Item;
    
            }
            ArrayList nameList = new ArrayList();
            
            private void listView1_DoubleClick(object sender, EventArgs e)
            {
                
                bool bulundu = false;
                
                    if(!bulundu)
                    {
                        foreach (String names in nameList)
                        {
                            if (nameList.Contains(SecilenKisi.Text))
                            {
                                bulundu = true;
                                break;
                            }
                        }
                        if (bulundu == false)
                        {
                            Form2 frm = new Form2();
                            frm.Show();
                            frm.textBox1.Text = SecilenKisi.Text;
                            nameList.Add(SecilenKisi.Text);
                        }
    That's what I've done so far... SecilenKisi indicates the person choosed and bulundu means found
    Last edited by Niheel; Jun 3 '10, 07:31 AM. Reason: merged code into question
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    Well, the way your code reads is that if nameList contains SecilenKisi.Tex t then bulundu gets set to true. Then, when bulundu is true, the form won't show. The behaviour you describe actually seems pretty expected. On the first run nameList is empty so bulundu stays false. Since it is false, the form shows and adds SecilenKisi.Tex t to nameList. The next time this is clicked, nameList contains SecilenKisi.Tex t (assuming that doesn't change) and thus the form doesn't open.

    A little debugging and the above seems pretty clear. I realize you're new so hopefully that process I described helps you in the future. Something that also helps is setting breakpoints, or even putting Console.WriteLi ne commands in to let you know what's running and the values of certain flags/variables. When you're working on something, it's pretty easy to miss things like this 'cause you've been staring at it for so long, so it's also good to just step back from it and give yourself a break. When you come back to it, you might see things from a different angle ;)

    As for what you're trying to do, if you want to prevent a window from being opened more than once perhaps just have a variable at the class level called nickFormOpen (or something like that). Set it to true when you open the form, and have that form's closing event set it to false. Then only open it if the flag specifies that the form is closed. A quick example...

    Code:
    private bool m_formOpen = false;
    private void SomeEvent(...)
    {
      SubForm newForm = new Form();
      newForm.Closing += new EventHandler(SubFormClosingEvent);
      newForm.Show();
      m_formOpen = true;
    }
    
    private void SubFormClosingEvent(...)
    {
      m_formOpen = false;
    }
    There are other ways to solve this of course, that's only one way (though not as elegant as other solutions :D). Think about what works best for you.

    Lastly, and this is more just a helpful tip, but you've got the following in your code...

    Code:
    bool someFlag = false;
    if (someFlag == false)
    {
      ...
    }
    There isn't any reason for this if to be there. In fact, if you have compile time optimization turned on, it might even be removed for you by the compiler. Not a big deal, but I thought I'd point it out.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      I would attached an event handler to the frm object to fire when the frm is closed, then you can deal with the bulundu value

      Comment

      • xinnie
        New Member
        • Jun 2010
        • 12

        #4
        Okay thanks guys, I've managed to get it done. For further questions I might show up here :) Thank you for sparing your time.

        Comment

        Working...