LstCtrl multi-selection with Shift/Ctrl key

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

LstCtrl multi-selection with Shift/Ctrl key

ying
This post has NOT been accepted by the mailing list yet.
Dear  LstCtrl expert:

I am having an annoying situation with ListCtrl multi-selection.  I would like to pop up a message box to tell the user that the selection is invalid and at the same time my code deselects the invalid selection by the user. When making selection using Ctrl key, the message box only pop up once, but when using Shift key, the message box pop up a couple of times (3). It is very annoying to the user. I  have been struggling with it for several days and still could not figure out. Please help me if you know where is the root problem. Thank you very much!  Here is the basic code.

import wx
import datetime

data = {0: ['apple', '2017-01-10','USA'],
        1: ['pineapple', '2017-01-11','Mexico'],
        2: ['banana', '2017-01-12','Mexico'],
        3: ['cherry', '2017-02-10','Mexico'],
        4: ['strawberry', '2017-02-15','USA'],
        5: ['blackberry', '2017-02-17','USA'],}
 
########################################################################
class MyForm(wx.Frame):
    #----------------------------------------------------------------------
    def __init__(self):
        wx.Frame.__init__(self, None, wx.ID_ANY, "List Control Example")
 
        # Add a panel so it looks the correct on all platforms
        panel = wx.Panel(self, wx.ID_ANY)
        self.index = 0
        self.selections = []
       
        self.lc = wx.ListCtrl(panel, size=(-1,130), style=wx.LC_REPORT|wx.BORDER_SUNKEN)
        self.lc.InsertColumn(0, 'Fruit')
        self.lc.InsertColumn(1, 'Date')
        self.lc.InsertColumn(2, 'Country', width=125)
        self.lc.Bind(wx.EVT_LIST_ITEM_SELECTED,self.OnSelectListItem)
        self.lc.Bind(wx.EVT_LIST_ITEM_DESELECTED,self.OnDeselectListItem)
       
        for index in data:
            self.lc.InsertStringItem(index, data[index][0])
            self.lc.SetStringItem(index, 1, data[index][1])
            self.lc.SetStringItem(index, 2, data[index][2])
       
        btn = wx.Button(panel, label="Action")
        btn.Bind(wx.EVT_BUTTON, self.OnAction)
 
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.lc, 0, wx.ALL|wx.EXPAND, 5)
        sizer.Add(btn, 0, wx.ALL|wx.CENTER, 5)
        panel.SetSizer(sizer)
 
    #----------------------------------------------------------------------          
    def OnSelectListItem(self,event):
        count = self.lc.GetSelectedItemCount()
        index = self.lc.GetFirstSelected()
        if count  == 1:
            self.selections = [index,] # reselect the country
        else:
            prev_country = self.lc.GetItemText(self.selections[0],col=2).encode('utf8')
            while index >= 0:
                if index not in self.selections:
                    new_country = self.lc.GetItemText(index,col=2).encode('utf8')
                    if new_country != prev_country:
                        self.lc.Select(index,on=0)  # deselect it because different count not allowed
                        self.showMsg('Country is different to the previous selection, deselect it.')
                    else:
                        self.selections.append(index)                      
                index = self.lc.GetNextSelected(index)
 
    def OnDeselectListItem(self,event):
        if self.lc.GetSelectedItemCount() == 0: self.selections = []
 
    def OnAction(self, event):
        if len(self.selections) == 0:
            self.showMsg('Nothing selected. Pls make selections')
        else:
            count = self.lc.GetSelectedItemCount()
            self.showMsg(str(count) + ' selected. Action to be implemented')
         
       
    def showMsg(self,msg):
        dlg = wx.MessageDialog(self,msg,'Confirm Exit',wx.OK|wx.ICON_EXCLAMATION)
        result = dlg.ShowModal()
        dlg.Destroy()

#----------------------------------------------------------------------
# Run the program
if __name__ == "__main__":
    app = wx.App(False)
    frame = MyForm()
    frame.Show()
    app.MainLoop()
Loading...