Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. ListBox close

ListBox close

Scheduled Pinned Locked Moved C#
help
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    quiteSmart
    wrote on last edited by
    #1

    hi, In my form i have a buttons, textBoxes, comboBoxes, labels,.... When i click on of the buttons a listbox that was set invisible is set visible. It shows some names. The list box is located directly at the right-bottom corner or that button. What i want is to make the listbox invisible whenever the user clicks the mouse at any location outside the listbox I tried using the FocusLost event of the listBox, but that didn't work perfectly. The problem it has is that whenever i click a label or a panel or something that doesn't receive focus, ofcourse the function is not executed. With the FocusLost i tried to implement the MouseDown event of the form. That also didn't work. Is there any way to do that without implementing a mouse click event on every label and every panel in the form. thanks in advance

    B L J 3 Replies Last reply
    0
    • Q quiteSmart

      hi, In my form i have a buttons, textBoxes, comboBoxes, labels,.... When i click on of the buttons a listbox that was set invisible is set visible. It shows some names. The list box is located directly at the right-bottom corner or that button. What i want is to make the listbox invisible whenever the user clicks the mouse at any location outside the listbox I tried using the FocusLost event of the listBox, but that didn't work perfectly. The problem it has is that whenever i click a label or a panel or something that doesn't receive focus, ofcourse the function is not executed. With the FocusLost i tried to implement the MouseDown event of the form. That also didn't work. Is there any way to do that without implementing a mouse click event on every label and every panel in the form. thanks in advance

      B Offline
      B Offline
      bobsugar222
      wrote on last edited by
      #2

      private void button1_Click(object sender, EventArgs e)
      {
      listBox1.Visible = true;
      listBox1.Focus();
      }

      private void listBox1_Leave(object sender, EventArgs e)
      {
      listBox1.Visible = false;
      }

      private void WindowsApplication1_Click(object sender, EventArgs e)
      {
      listBox1.Visible = false;
      }

      Q 1 Reply Last reply
      0
      • Q quiteSmart

        hi, In my form i have a buttons, textBoxes, comboBoxes, labels,.... When i click on of the buttons a listbox that was set invisible is set visible. It shows some names. The list box is located directly at the right-bottom corner or that button. What i want is to make the listbox invisible whenever the user clicks the mouse at any location outside the listbox I tried using the FocusLost event of the listBox, but that didn't work perfectly. The problem it has is that whenever i click a label or a panel or something that doesn't receive focus, ofcourse the function is not executed. With the FocusLost i tried to implement the MouseDown event of the form. That also didn't work. Is there any way to do that without implementing a mouse click event on every label and every panel in the form. thanks in advance

        L Offline
        L Offline
        Luc Pattyn
        wrote on last edited by
        #3

        Assuming the regular events dont offer a solution, here are two more ideas: - you could use MouseMove and try and detect a mouse move going to leave the listbox. this probably would involve two borders (two to detect the direction of motion, you must allow the mouse to enter the listbox !). You may have to allow for some mouse jitter. And be careful with the scroll bars. - you could use a timer that periodically checks the mouse is still inside the lisbox (with Control.MousePosition). :)

        Luc Pattyn

        B 1 Reply Last reply
        0
        • B bobsugar222

          private void button1_Click(object sender, EventArgs e)
          {
          listBox1.Visible = true;
          listBox1.Focus();
          }

          private void listBox1_Leave(object sender, EventArgs e)
          {
          listBox1.Visible = false;
          }

          private void WindowsApplication1_Click(object sender, EventArgs e)
          {
          listBox1.Visible = false;
          }

          Q Offline
          Q Offline
          quiteSmart
          wrote on last edited by
          #4

          hi, thanks for you reply but i have some comments first what is this:

          bobsugar222 wrote:

          private void WindowsApplication1_Click(object sender, EventArgs e)

          there is no events for the windows application. and if you mean by it form1 than i tried it and it didn't work please clarify. thanks again

          B 1 Reply Last reply
          0
          • L Luc Pattyn

            Assuming the regular events dont offer a solution, here are two more ideas: - you could use MouseMove and try and detect a mouse move going to leave the listbox. this probably would involve two borders (two to detect the direction of motion, you must allow the mouse to enter the listbox !). You may have to allow for some mouse jitter. And be careful with the scroll bars. - you could use a timer that periodically checks the mouse is still inside the lisbox (with Control.MousePosition). :)

            Luc Pattyn

            B Offline
            B Offline
            blue_arc
            wrote on last edited by
            #5

            use form_click event to do this.. if u r using windows...

            1 Reply Last reply
            0
            • Q quiteSmart

              hi, In my form i have a buttons, textBoxes, comboBoxes, labels,.... When i click on of the buttons a listbox that was set invisible is set visible. It shows some names. The list box is located directly at the right-bottom corner or that button. What i want is to make the listbox invisible whenever the user clicks the mouse at any location outside the listbox I tried using the FocusLost event of the listBox, but that didn't work perfectly. The problem it has is that whenever i click a label or a panel or something that doesn't receive focus, ofcourse the function is not executed. With the FocusLost i tried to implement the MouseDown event of the form. That also didn't work. Is there any way to do that without implementing a mouse click event on every label and every panel in the form. thanks in advance

              J Offline
              J Offline
              J4amieC
              wrote on last edited by
              #6

              Hi qS, I tried this out for ya, and could easily reproduce the problem you described. Buttons seem ok (they take care of invoking the Leave event on the ListBox), and handling the MouseDown event on the form takes care of hiding it in that instance. Panels cause a problem as they themselves capture the MouseDown event and therefore its not raised on the Form. As an aside; I was sure there was a way to override this behaviour to tell specific controls to "pass all mouse and key events up the control heirachy. Each level should then be able to do the same thing - handle or pass the event. I cannot find this although im 90% sure it exists. There is, like always, a sufficient workaround which is to intercept a control being added to the form and handle its MouseDown event also. You can re-use the same event handler as you used for your Form as they have the same signature (and you want the same behaviour - hide the listbox). I added 1 button and 1 listbox to a form. I thn added the following event handlers: //Show the listbox private void button1_Click(object sender, System.EventArgs e) { this.listBox1.Visible = true; } // hide the listbox if it looses focus private void listBox1_Leave(object sender, System.EventArgs e) { this.listBox1.Visible = false; } //event handler for Form.MouseDown (and later for each control) private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(sender != this.listBox1) this.listBox1.Visible=false; } The last method there has a check that the mouse down event isnt actually on the listbox were using, as you need the mouse down event to set your selection. Then just override OnControlAdded: protected override void OnControlAdded(ControlEventArgs e) { e.Control.MouseDown += new MouseEventHandler(Control_MouseDown); base.OnControlAdded (e); } Nice!

              --- How to get answers to your questions[^]

              Q 1 Reply Last reply
              0
              • J J4amieC

                Hi qS, I tried this out for ya, and could easily reproduce the problem you described. Buttons seem ok (they take care of invoking the Leave event on the ListBox), and handling the MouseDown event on the form takes care of hiding it in that instance. Panels cause a problem as they themselves capture the MouseDown event and therefore its not raised on the Form. As an aside; I was sure there was a way to override this behaviour to tell specific controls to "pass all mouse and key events up the control heirachy. Each level should then be able to do the same thing - handle or pass the event. I cannot find this although im 90% sure it exists. There is, like always, a sufficient workaround which is to intercept a control being added to the form and handle its MouseDown event also. You can re-use the same event handler as you used for your Form as they have the same signature (and you want the same behaviour - hide the listbox). I added 1 button and 1 listbox to a form. I thn added the following event handlers: //Show the listbox private void button1_Click(object sender, System.EventArgs e) { this.listBox1.Visible = true; } // hide the listbox if it looses focus private void listBox1_Leave(object sender, System.EventArgs e) { this.listBox1.Visible = false; } //event handler for Form.MouseDown (and later for each control) private void Control_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e) { if(sender != this.listBox1) this.listBox1.Visible=false; } The last method there has a check that the mouse down event isnt actually on the listbox were using, as you need the mouse down event to set your selection. Then just override OnControlAdded: protected override void OnControlAdded(ControlEventArgs e) { e.Control.MouseDown += new MouseEventHandler(Control_MouseDown); base.OnControlAdded (e); } Nice!

                --- How to get answers to your questions[^]

                Q Offline
                Q Offline
                quiteSmart
                wrote on last edited by
                #7

                Thank you for taking all the time to test my problem and figure out a solution. Actually i did before the same solution you provided. it is working now. thanks.:rose:

                1 Reply Last reply
                0
                • Q quiteSmart

                  hi, thanks for you reply but i have some comments first what is this:

                  bobsugar222 wrote:

                  private void WindowsApplication1_Click(object sender, EventArgs e)

                  there is no events for the windows application. and if you mean by it form1 than i tried it and it didn't work please clarify. thanks again

                  B Offline
                  B Offline
                  bobsugar222
                  wrote on last edited by
                  #8

                  Sorry, I that was from an earlier test of mine... I didn't think you were able to have the same class name as the parent namespace, so I renamed Form1 to WindowsApplication1... So it was the Form.Click event, yes. There is no reason for it not to work. If you click the form, the listBox is made invisible. I think the reason your original method didn't work was because you never explicitly set the focus to the ListBox like I did in my Button.Click event handler.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups