Dialog Box Like SelChain Selection Box

How do I show a dialog box similar to the SelChain command in Python where if the user left clicks I can control the active list item and if the user right clicks it allows me to close the dialog box?

Selection Menu

What is it that you’re seeing there?
It’s a FORM containing a list.

Where does it happen?
Current mouse location

How is the form spawned?
By the Event “object selected”, when you select an object that triggers the event that opens the form.

So we will need :

  1. A form
  2. A list view
  3. An event handler to determine when to spawn the form
  4. Getting the mouse location when the event occus

So something like this would work :

import clr
clr.AddReference('System.Windows.Forms')
clr.AddReference('System.Drawing')

from System.Windows.Forms import Application, Form, ListBox
from System.Drawing import Point, Size
from System.Windows.Forms import Control

class MyForm(Form):
    def __init__(self):
        self.InitializeComponent()
    
    def InitializeComponent(self):
        self.listBox = ListBox()
        self.SuspendLayout()

        for item in ["1", "2", "3", "4"]:
            self.listBox.Items.Add(item)
        self.listBox.Location = Point(10, 10)
        self.listBox.Size = Size(50, 70)
        self.listBox.SelectedIndexChanged += self.listBox_SelectedIndexChanged

        self.ClientSize = Size(70, 90)
        self.Controls.Add(self.listBox)
        self.Name = "MyForm"
        self.Text = "Select Number"
        self.Load += self.MyForm_Load

        self.ResumeLayout(False)

    def MyForm_Load(self, sender, e):
        current_pos = Control.MousePosition
        self.Location = Point(current_pos.X, current_pos.Y)

    def listBox_SelectedIndexChanged(self, sender, e):
        if self.listBox.SelectedIndex >= 0:
            print(self.listBox.SelectedItem)

def main():
    form = MyForm()
    Application.EnableVisualStyles()
    Application.Run(form)

if __name__ == "__main__":
    main()

In case I wasn’t clear and you have any doubts don’t hesitate to ask.

Hope this helps,
Farouk

1 Like