2 Replies - 711 Views - Last Post: 17 February 2013 - 11:48 AM Rate Topic: -----

#1 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Item focus listview c#

Posted 11 February 2013 - 08:38 AM

I have this piece of code in Listview-SelectedIndexChanged Event.
string get_gold = Regex.Replace(word, "[^0-9]+", string.Empty);
                if (gold >= Convert.ToInt32(get_gold))
                {
                        gold -= Convert.ToInt32(get_gold);
                }
                else
                {
                    MessageBox.Show("Error!");
                }

The problem is, when I click the item for the first time, it runs successfully. However, if I click the same item twice, it does nothing, it only does if I click in a different item.

The item is focused, but I can't get the focus out of him without giving me "errors" (because it duplicates the code).

Tried:
listview1.SelectedIndices.Clear(); 
listview1.SelectedItems.Clear();


Is This A Good Question/Topic? 0
  • +

Replies To: Item focus listview c#

#2 TheKirk  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 58
  • Joined: 10-February 13

Re: Item focus listview c#

Posted 11 February 2013 - 09:02 AM

You should use the MouseDown event rather then the SelectedIndexChanged event. That one only fires when the index changes, not if you click on it.
The mousedown event always fires when an item is clicked:

myListview.MouseDown += new MouseEventHandler(this.myListview_MouseDown);

void myListview_MouseDown(Object sender, MouseEventArgs e)
{
  // Make sure it was a single left click, like the normal Click event
  if ( (e.Button == MouseButtons.Left) && (e.Clicks == 1) )
  {
    ListViewHitTestInfo htInfo = myListview.HitTest(e.X, e.Y);
  }
}

Was This Post Helpful? 0
  • +
  • -

#3 Keylogger  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 344
  • Joined: 14-February 11

Re: Item focus listview c#

Posted 17 February 2013 - 11:48 AM

Worked perfectly. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1