Results 1 to 4 of 4

Thread: Catching Setting Before Event

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    31

    Catching Setting Before Event

    Code:
    private void lstCommands_SelectedIndexChanged(object sender, System.EventArgs e)
    {}
    When a user clicks an item in the list box, I want to save certain items that users have entered while one item has been selected; however, when I look at the Selected Index of the listbox on SelectedIndexChanged, all the values have already gone to the new one. How can I get the old values?

    Dan

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Catching Setting Before Event

    Quote Originally Posted by dipique
    Code:
    private void lstCommands_SelectedIndexChanged(object sender, System.EventArgs e)
    {}
    When a user clicks an item in the list box, I want to save certain items that users have entered while one item has been selected; however, when I look at the Selected Index of the listbox on SelectedIndexChanged, all the values have already gone to the new one. How can I get the old values?

    Dan
    a listbox or a listview control? If you're talking about listview then you need to use the BeforeLabelEdit/AfterLabelEdit events. If you're using a listbox then please explain what you're doing, because it's a bit vague. How does the user change the list items?
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2005
    Posts
    31

    Re: Catching Setting Before Event

    Items are selected by clicking on the item in the listbox. That item is then parsed and the separate components loaded into several text boxes. What I want to do is capture the state of the listbox before the item was selected.

    Dan

  4. #4
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Re: Catching Setting Before Event

    Interesting. The only event I could find that captures the state before the SelectedIndexChange event is the KeyDown event. So if the user only uses the arrow keys to move through the ListBox, that would work. Of course that's unlikely, so...

    One way to solve the problem is to create a private variable that tracks the current selected item of the ListBox. This variable would get set to listBox1.SelectedIndex in the LoadEvent (or sometime before the user has a chance to change the ListBox SelectedItem). Then, in the SelectedItemChanged event, you can read this variable to discover what the previous selected item was, do whatever you need to do with this infomation, then reset it to the current selected item.

    Example:
    Code:
    private int prevSelectedItem;
    private void Form1_Load(object sender, System.EventArgs e)
    {
    	// Default select first item in list and set private variable
    	listBox1.SelectedIndex = prevSelectedItem = 0;	
    }
    private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
    {
    	// do whatever you want here with the previously selected item
    	MessageBox.Show("The previously selected item was: " + listBox1.Items[prevSelectedItem]);
    
    	// now reset the variable to the currently selected item
    	prevSelectedItem = listBox1.SelectedIndex;
    }
    Last edited by seaweed; Mar 8th, 2005 at 02:24 PM.
    ~seaweed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width