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. List Box Sudden Exit On Selection

List Box Sudden Exit On Selection

Scheduled Pinned Locked Moved C#
databasesql-serverhelpquestion
5 Posts 4 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.
  • P Offline
    P Offline
    PDTUM
    wrote on last edited by
    #1

    Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat

    Richard Andrew x64R L B 3 Replies Last reply
    0
    • P PDTUM

      Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat

      Richard Andrew x64R Offline
      Richard Andrew x64R Offline
      Richard Andrew x64
      wrote on last edited by
      #2

      You say that an exception causes your program to close. This is normal. You must show what code you're using in the selection event handler to get any real help with this.

      The difficult we do right away... ...the impossible takes slightly longer.

      1 Reply Last reply
      0
      • P PDTUM

        Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat

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

        A lot of Controls, including System.Windows.Forms.ListBox will fire unexpected events, forcing you to code defensively. As an example, a SelectionChanged event (or whatever it is called) may be fired when an item gets deselected, which you can check by looking for a negative SelectedIndex, or a SelectedItem being null. If you don't, you're bound to run into trouble. Also don't be surprised it you use OwnerDraw mode and get MeasureItem/DrawItem events with an index of -1. IMO all these can be avoided by proper testing before acting; and all can be caught with proper try-catch constructs. Finally, without actual code, we can't provide more specific help. I have noticed you seldom provide code when in fact you should. :)

        Luc Pattyn [My Articles] Nil Volentibus Arduum

        P 1 Reply Last reply
        0
        • P PDTUM

          Gentleman, I am using a list box to list some string information gathered from an MSSQL database. I noticed that if I select the box and should "miss" the string line (selecting empty space), a null exception is thrown. The problem is that it also closes the application. I have tried several different ways to escape this by showing the exception (try, catch) and trying to return to the application, but they have all failed. Ideas? Thank You, Pat

          B Offline
          B Offline
          BobJanova
          wrote on last edited by
          #4

          My guess is you're doing SelectedItem.something in the event handler. If you select nothing, SelectedItem will be null, and SelectedIndex will be -1. You should always check for these conditions in any event handler that uses one of these properties; I typically put a guard at the top, since you usually don't want to do anything in that case:

          void ListSelectionChanged(object sender, EventArgs e){
          ListControl list = (ListControl)sender;
          if(list.SelectedIndex < 0) return;
          ... // do stuff with list.SelectedItem
          }

          1 Reply Last reply
          0
          • L Luc Pattyn

            A lot of Controls, including System.Windows.Forms.ListBox will fire unexpected events, forcing you to code defensively. As an example, a SelectionChanged event (or whatever it is called) may be fired when an item gets deselected, which you can check by looking for a negative SelectedIndex, or a SelectedItem being null. If you don't, you're bound to run into trouble. Also don't be surprised it you use OwnerDraw mode and get MeasureItem/DrawItem events with an index of -1. IMO all these can be avoided by proper testing before acting; and all can be caught with proper try-catch constructs. Finally, without actual code, we can't provide more specific help. I have noticed you seldom provide code when in fact you should. :)

            Luc Pattyn [My Articles] Nil Volentibus Arduum

            P Offline
            P Offline
            PDTUM
            wrote on last edited by
            #5

            Thank you Luc (nice of you to reply to me again) and to all of you in fact for your excellent replies. All are good advice and I intent to use it all. Re: The Issue; most of the time, I usually find myself embarrassed when I finally solve some of these things. I employed the following simple one line of code in the SelectedIndexChanged event to resolve the issue:

            private void listBoxXxx_SelectedIndexChanged(object sender, EventArgs e)
            {
            if (listBoxXxx.Items.Count < 1)
            {
            string msg = "There are no Items in the list to select from. ";
            MessageBox.Show(msg, "Required Information Missing", MessageBoxButtons.OK, MessageBoxIcon.Information);
            return;
            }

                    if (listBoxXxx.SelectedIndex != -1)
                    {
                        labelXxx.Text = listBoxXxx.SelectedItem.ToString();
                    }
                }
            

            To my friend and mentor Luc...actually, I looked up my history and I usually do supply the code when the issue is code failure but not when (I don't know how) which is sometimes the case. In this case, I should have supplied the following, but it was so basic I could not understand the possible link, so please accept my apology.

                            //Load the Main page
                            frmMain m = new frmMain(fname, title, firm, firmId, permission, theDate, userId, chal, CONNSTR);
                            this.Hide();
                            m.ShowDialog();
                        }
                    }
                    catch (SqlException Sqlex)  //Catch Sql Errors
                    {
                        MessageBox.Show("Error: " + Sqlex, "SQL Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
            
                    catch (Exception ex)  //Catch All Errors
                    {
                        MessageBox.Show("Error: " + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
            

            Most important...thank you all for your interest and expertise. Luc, your answer was closest to what I used and I am marking it as the correct solution. Best Regards, Pat :) :)

            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