Winform Listbox ordered selection

Hi, 
I am trying to create a dialog window using Winform, that would give me a list of sorted parameter names selected in order from a given list. Basicaly I would like to have two windows, first showing the list of all parameters and the second showing the selected. When I doubleclick on a parameter from the first list, it moves to the second list, that is correct. But what I cant do is to remove the item from the second list after again doubleclicking on it back to the list 1. I always get this error : *List that this enumerator is bound to has been modified. An enumerarot can only be used if the list does not change*. I get the same error when I try to remove selected item from the first list. Can anyone help?

Thank you, L.

import clr
import sys
pyt_path = r’C:\Program Files (x86)\IronPython 2.7\Lib’
sys.path.append(pyt_path)

clr.AddReference(“System.Windows.Forms”)
clr.AddReference(“System.Drawing”)

from System.Windows.Forms import *
from System.Drawing import Icon, Color, Font, Point, Size, SolidBrush, Rectangle
from System import Array, Object

import System.IO

titleText = “form”
btnHeight = 30
btnWidth = 200
spacing = 20
fontMessage = Font(“ISOCPEUR”, 8)
fontCK = Font(“ISOCPEUR”, 8)
fontTI = Font(“ISOCPEUR”, 10)
fontEX = Font(“ISOCPEUR”, 7)

def button(txt, loc, clc):
btn = Button()
btn.Text = txt
btn.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right)
btn.Location = loc
btn.Click += clc
btn.Height = btnHeight
btn.Width = btnWidth
btn.BackColor = Color.FromArgb(255, 255, 255)
return btn

def tolist(obj1):
if hasattr(obj1,“iter”): return obj1
else: return [obj1]

class dimsionInputs(Form):

def __init__(self,args1):	
	self.Text = titleText             
	self.BackColor = Color.FromArgb(240, 240, 240)  
	self.WindowState = FormWindowState.Normal 
	self.CenterToScreen() 
	self.BringToFront() 
	self.Topmost = True
	self.Width = 600
	self.Height =950

	self.FormBorderStyle = FormBorderStyle.FixedDialog 	

	userMessage = Label()  
	font = fontMessage
	userMessage.Text = 'text'
	userMessage.Font = fontTI
	userMessage.Location =Point(50,20)  
	userMessage.Width = 500
	userMessage.Height = 50
	self.Controls.Add(userMessage)   

LIST 1 - parametry

	self.checklistLabel = Label()
	self.checklistLabel.Text = "text"
	self.checklistLabel.Width = 120
	self.checklistLabel.Location = Point(50,70)
	self.checklistLabel.Height = 50 
	self.Controls.Add(self.checklistLabel)	

	self.checkval = []
	self.output1 = []
	self.Font = fontCK


	self.list1 = ListBox()
	self.list1.Size = Size(350,300)
	self.list1.Location= Point(180,70)
	array = Array[Object](args1)
	self.list1.Items.AddRange(array)

for i in args1:

self.list1.Items.Add(i)

	self.list1.MouseDoubleClick += self.btn_Click


	self.Controls.Add(self.list1)
	self.AutoScroll = True

LIST 1 - vybrané parametry

	self.list2 = ListBox()
	self.list2.Size = Size(350,300)
	self.list2.Location= Point(180,420)
	
	self.list2.MouseDoubleClick += self.btn_Clickbck
	
	
	self.Controls.Add(self.list2)
	self.AutoScroll = True

OK AND CANCEL BUTTONS

	btnOkClick = self.update 
	btnOkLoc = Point(50,830)
	btnOk = button('OK', btnOkLoc, btnOkClick) 
    
  
	btnCnclClick = self.CnlButtonPressed 
	btnCnclLoc = Point(330,830)
	btnCancel = button('Cancel', btnCnclLoc, btnCnclClick)
    
  
	self.Controls.Add(btnOk)
	self.Controls.Add(btnCancel)   	

def btn_Click(self,sender, event):
	for item in self.list1.SelectedItems:

		self.list2.Items.Add(item)


def btn_Clickbck (self,sender, event):
	for item in self.list2.SelectedItems:
		self.list2.Items.Remove(item)
		



def update(self, sender, event):
	self.output1 = form.list2.Items


	self.Close()


	
### buttons ###
def okButtonPressed(self, sender, event):

self.output1 = self.checkbox.SelectedItem

	self.Close()    
	
    
def CnlButtonPressed(self, sender, event):
	self.Close()	

if IN[0] == None: l1 =
else: l1 = tolist(IN[0])

form = dimsionInputs(l1)

Application.Run(form)

OUT = form.output1

Application.Exit()

import clr
import sys
pyt_path = r'C:\Program Files (x86)\IronPython 2.7\Lib'
sys.path.append(pyt_path)

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")


from System.Windows.Forms import *
from System.Drawing import Icon, Color, Font, Point, Size, SolidBrush, Rectangle
from System import Array, Object

import System.IO


titleText = "form"      
btnHeight = 30 
btnWidth = 200
spacing = 20  
fontMessage = Font("ISOCPEUR", 8)
fontCK = Font("ISOCPEUR", 8) 
fontTI = Font("ISOCPEUR", 10)
fontEX = Font("ISOCPEUR", 7)


def button(txt, loc, clc):
	btn = Button()
	btn.Text = txt
	btn.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right)
	btn.Location = loc
	btn.Click += clc
	btn.Height = btnHeight
	btn.Width = btnWidth
	btn.BackColor = Color.FromArgb(255, 255, 255) 
	return btn
	
def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]	



	
class dimsionInputs(Form):  	
	
	def __init__(self,args1):	
		self.Text = titleText             
		self.BackColor = Color.FromArgb(240, 240, 240)  
		self.WindowState = FormWindowState.Normal 
		self.CenterToScreen() 
		self.BringToFront() 
		self.Topmost = True
		self.Width = 600
		self.Height =950
#   
		self.FormBorderStyle = FormBorderStyle.FixedDialog 	
	
		userMessage = Label()  
		font = fontMessage
		userMessage.Text = 'text'
		userMessage.Font = fontTI
		userMessage.Location =Point(50,20)  
		userMessage.Width = 500
		userMessage.Height = 50
		self.Controls.Add(userMessage)   
		
		
      
## LIST 1 - parametry ##	
		self.checklistLabel = Label()
		self.checklistLabel.Text = "text"
		self.checklistLabel.Width = 120
		self.checklistLabel.Location = Point(50,70)
		self.checklistLabel.Height = 50 
		self.Controls.Add(self.checklistLabel)	

		self.checkval = []
		self.output1 = []
		self.Font = fontCK
	
	
		self.list1 = ListBox()
		self.list1.Size = Size(350,300)
		self.list1.Location= Point(180,70)
		array = Array[Object](args1)
		self.list1.Items.AddRange(array)
#		for i in args1:
#			self.list1.Items.AddRange(Array[i])

#		for i in args1:
#			self.list1.Items.Add(i)
#
		self.list1.MouseDoubleClick += self.btn_Click

	
		self.Controls.Add(self.list1)
		self.AutoScroll = True
		



## LIST 1 - vybrané parametry ##	

		self.list2 = ListBox()
		self.list2.Size = Size(350,300)
		self.list2.Location= Point(180,420)
		
		self.list2.MouseDoubleClick += self.btn_Clickbck
		
		
		self.Controls.Add(self.list2)
		self.AutoScroll = True



## OK AND CANCEL BUTTONS ##		
	
		btnOkClick = self.update 
		btnOkLoc = Point(50,830)
		btnOk = button('OK', btnOkLoc, btnOkClick) 
        
      
		btnCnclClick = self.CnlButtonPressed 
		btnCnclLoc = Point(330,830)
		btnCancel = button('Cancel', btnCnclLoc, btnCnclClick)
        
      
		self.Controls.Add(btnOk)
		self.Controls.Add(btnCancel)   	

	def btn_Click(self,sender, event):
		for item in self.list1.SelectedItems:

			self.list2.Items.Add(item)


	def btn_Clickbck (self,sender, event):
		for item in self.list2.SelectedItems:
			self.list2.Items.Remove(item)
			



	def update(self, sender, event):
		self.output1 = form.list2.Items


		self.Close()
	

		
	### buttons ###
	def okButtonPressed(self, sender, event):
#		self.output1 = self.checkbox.SelectedItem
		self.Close()    
		
        
	def CnlButtonPressed(self, sender, event):
		self.Close()	







if IN[0] == None: l1 = []
else: l1 = tolist(IN[0])


form = dimsionInputs(l1)

Application.Run(form)


OUT =  form.output1

Application.Exit()
	
	

Apologies for a bad formatting :frowning:

1 Like

@Lucy_Lastparametrics this is what you need:
Winform.dyn (9.1 KB)

def btn_Click(self,sender, event):
		for item in self.list1.SelectedItems:
			self.list2.Items.Add(item)
		while len(self.list1.SelectedItems) > 0:
			self.list1.Items.RemoveAt(self.list1.SelectedIndices[0])
		self.list2.Sorted = True

	def btn_Clickbck (self,sender, event):
		for item in self.list2.SelectedItems:
			self.list1.Items.Add(item)
		while len(self.list2.SelectedItems) > 0:
			self.list2.Items.RemoveAt(self.list2.SelectedIndices[0])
		self.list1.Sorted = True