I have question about the listbox. My listbox is binding to a database, and I use SelectedValueChanged to determine which my record I chose, I also get a couple button to interact with the sorting but the problem is whenever I change the database, like update new sql query, it loads the listbox again. And the time that listbox is loading, it triggers the SelectedValueChanged also. I try to use bool flag but I don't know how the flow of the program.

You can use a boolean flag like this:

bool isSuspended = false;
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isSuspended)
                return;

            //process selected index changed
            
        }

        private void button13_Click(object sender, EventArgs e)
        {
            isSuspended = true;

            //process code which causes lsit to rebind

            isSuspended = false;
            
        }

By setting the flag to true before rebinding the list when the SelectedIndexChanged event is fired the event handler quits without processing the event.

Be aware, however, that you will probably need to include a way to 'remember' which item was selected in the list before you bind it then reset the selected item, otherwise you will have data shown for a record that isnt selected in the list.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.