Author Topic: How to get MouseClick listItem ?  (Read 219 times)

0 Members and 1 Guest are viewing this topic.

QQ25294717

  • Mosquito
  • Posts: 2
How to get MouseClick listItem ?
« on: January 07, 2022, 10:37:13 AM »
How to get MouseClick listItem ?
Code - C#: [Select]
  1. namespace PaletteMenu
  2. {
  3.     public class MyCommands
  4.     {
  5.         //
  6.         Editor ed = Application.DocumentManager.MdiActiveDocument.Editor;
  7.         private Control[] control = new Control[8];
  8.         private ListView[] listview = new ListView[8];
  9.         //
  10.         [CommandMethod("ttt")]
  11.         public void AddMyPaletteSet()
  12.         {
  13.             var myPaletteSet = new PaletteSet("Menu");
  14.             for (int i = 0; i < 8; i++)
  15.             {
  16.                 control[i] = new Control("Group" + i);
  17.                 myPaletteSet.Add("Group" + i, control[i]);
  18.                 listview[i] = new ListView();
  19.                 listview[i].Dock =DockStyle.Fill;
  20.                 listview[i].View =View.List;
  21.                 listview[i].BackColor = SystemColors.Control;
  22.                 control[i].Controls.Add(listview[i]);
  23.                 listview[i].BeginUpdate();
  24.                 for (int ii = 0; ii < 30; ii++)
  25.                 {
  26.                     ListViewItem lvi = new ListViewItem();
  27.                     lvi.ImageIndex = ii;
  28.                     lvi.Text = "Button" + ii;
  29.                     listview[i].Items.Add(lvi);
  30.                 }
  31.                 listview[i].EndUpdate();
  32.                 listview[i].MouseClick += new MouseEventHandler(MyCommands_MouseClick);
  33.                 listview[i].MouseEnter += new EventHandler(MyCommands_MouseEnter);
  34.                 listview[i].MouseLeave += new EventHandler(MyCommands_MouseLeave);
  35.             }
  36.  
  37.             myPaletteSet.Visible = true;
  38.         }
  39.  
  40.         void MyCommands_MouseLeave(object sender, EventArgs e)
  41.         {
  42.             ListView listview = (ListView)sender;
  43.             listview.BackColor = SystemColors.Control;
  44.         }
  45.  
  46.         void MyCommands_MouseEnter(object sender, EventArgs e)
  47.         {
  48.             ListView listview = (ListView)sender;
  49.             listview.BackColor = SystemColors.ControlDark;
  50.         }
  51.  
  52.         void MyCommands_MouseClick(object sender, MouseEventArgs e)
  53.         {
  54.             if (e.Button == MouseButtons.Left)
  55.             {
  56.                 ListView listview = sender as ListView;
  57.                 MessageBox.Show(listview.Text);
  58.                 //How to get MouseClick listItem ?
  59.                 //ed.WriteMessage(listview.Text + "\n");
  60.             }
  61.         }
  62.  
  63.     }
  64. }
  65.  

n.yuan

  • Bull Frog
  • Posts: 326
Re: How to get MouseClick listItem ?
« Reply #1 on: January 08, 2022, 09:36:40 AM »
You do not use MouseEnter[Leave, Click] to find out if user clicks a ListViewItem in a ListView (so it is selected). A UI control, such as ListBox, ListView comes with properties/events specifically designed for their functionalities. In the case of identify the selected ListViewItem or ListViewItems, you should look into properties like: SelectedItems/SelectedIndices/CheckedItems/FocusedItem and events like: SelectedIndexChanged/ItemChecked. For your code, you just need to hook to the SelectedIndexChanged event to each of the ListViews you created.

I believe you are exploring with "concept-proving" code and you are not really going to code-build the UI in a CommandMethod, are you?

badjo_5

  • Mosquito
  • Posts: 17
Re: How to get MouseClick listItem ?
« Reply #2 on: January 09, 2022, 01:45:49 PM »
You should build UserControl with ListView, and from UserContol handle all ListView events/properties/methods.
Then PaletteSet will handle all things for/from Autocad, and your UserControl is responsible for themselves.

Something like this: https://drive-cad-with-code.blogspot.com/2010/09/pluggable-paletteset.html?m=0



QQ25294717

  • Mosquito
  • Posts: 2
Re: How to get MouseClick listItem ?
« Reply #3 on: January 15, 2022, 12:46:23 AM »
Code - C#: [Select]
  1. public class MyCommands
  2.     {
  3.         //
  4.         private Editor aced = Application.DocumentManager.MdiActiveDocument.Editor;
  5.         private Control[] control = new Control[8];
  6.         private ListView[] listview = new ListView[8];
  7.         /// <summary>
  8.         /// AddMyPaletteSet
  9.         /// </summary>
  10.         [CommandMethod("ttt")]
  11.         public void AddMyPaletteSet()
  12.         {
  13.             PaletteSet myPaletteSet = new PaletteSet("Menu");
  14.             myPaletteSet.Style = PaletteSetStyles.ShowAutoHideButton;
  15.             for (int i = 0; i < 8; i++)
  16.             {
  17.                 control[i] = new Control("Group" + i);
  18.                 myPaletteSet.Add("Group" + i, control[i]);
  19.                 listview[i] = new ListView();
  20.                 listview[i].Dock =DockStyle.Fill;
  21.                 listview[i].View =View.List;
  22.                 listview[i].FullRowSelect = true;
  23.                 listview[i].BackColor = SystemColors.Control;
  24.                 for (int ii = 0; ii < 30; ii++)
  25.                 {
  26.                     listview[i].Items.Add("Button" + i + "-" + ii);
  27.                 }
  28.                 listview[i].SelectedIndexChanged += (MyCommands_SelectedIndexChanged);//add Event
  29.                 control[i].Controls.Add(listview[i]);
  30.             }
  31.             myPaletteSet.Visible = true;
  32.         }
  33.         /// <summary>
  34.         /// SelectedIndexChanged
  35.         /// </summary>
  36.         /// <param name="sender"></param>
  37.         /// <param name="e"></param>
  38.           void MyCommands_SelectedIndexChanged(object sender, EventArgs e)
  39.         {
  40.             ListView listView = sender as ListView;//get the listview
  41.             if (listView.SelectedItems.Count > 0)//have selected item&#12290;
  42.             {
  43.                 string str = listView.SelectedItems[0].SubItems[0].Text;
  44.                 aced.WriteMessage("You checked [" + str + "]\n");
  45.             }
  46.         }
  47.     }
  48.