Find attached script for a windows.Form. Is this a bug? When loading the data
into the listview from a list of tuples:
data = [("one","test one"),("two","test two"), ("three","test three")]
for item in data:
temp = WF.ListViewItem(item[0])
temp.SubItems.Add(item[1])
self.listView.Items.Add(temp)
The rows are filled with the first and second characters of the first string
rather than the relevant tuple strings.
Winforms is pretty cool otherwise.
Guy
import CLR
from CLR import System
import CLR.System.Windows.Forms as WF
from CLR.System.Drawing import Size, Point, Color
class ScriptForm(WF.Form):
"""A simple hello world app that demonstrates the essentials of
WF programming and event-based programming in Python."""
def __init__(self):
self.Text = "Python scripting for the Revit API"
#self.AutoScaleBaseSize = Size(5, 13)
self.ClientSize = Size(500, 300)
h = WF.SystemInformation.CaptionHeight
self.MinimumSize = Size(300, (100 + h))
self.FormBorderStyle = WF.FormBorderStyle.FixedDialog
self.MinimizeBox = False
self.MaximizeBox = False
self.StartPosition = WF.FormStartPosition.CenterScreen
self.components = System.ComponentModel.Container()
# Create the ListView
self.listView = WF.ListView()
self.listView.Location = Point(5,5)
self.listView.Width = 370
self.listView.Height = self.ClientSize.Height-10
self.listView.TabIndex = 0
self.formatLV()
self.loaddata()
# Create the run button
self.runBut = WF.Button()
self.runBut.Location = Point(400, 100)
self.runBut.BackColor = Color.GreenYellow
self.runBut.TabIndex = 1
self.runBut.Text = "Run Script"
# Create the Cancel button
self.CanBut = WF.Button()
self.CanBut.Location = Point(400, 150)
self.CanBut.TabIndex = 2
self.CanBut.Text = "Cancel"
# Register the event handlers
self.runBut.Click += self.runScript_Click
self.CanBut.Click += self.Cancel_Click
# Add the controls to the form
self.Controls.Add(self.listView)
self.Controls.Add(self.runBut)
self.Controls.Add(self.CanBut)
def runScript_Click(self, sender, args):
""" Get and Run the selected script"""
WF.MessageBox.Show("Run script")
#return "script"
def Cancel_Click(self, sender, args):
"""Cancel don't run anything"""
#return "cancel"
self.components.Dispose()
WF.Form.Dispose(self)
self.Close()
def formatLV(self):
self.listView.View = WF.View.Details
self.listView.LabelEdit = False
self.listView.AllowColumnReorder = False
self.listView.GridLines = True
self.listView.FullRowSelect = True
self.listView.Sorting = WF.SortOrder.Ascending
#now do columns
self.listView.Columns.Add("Script Name", 100,WF.HorizontalAlignment.Left)
self.listView.Columns.Add("Description", 266,WF.HorizontalAlignment.Left)
def loaddata(self):
data = [("one","test one"),("two","test two"), ("three","test three")]
for item in data:
temp = WF.ListViewItem(item[0])
temp.SubItems.Add(item[1])
self.listView.Items.Add(temp)
def run(self):
WF.Application.Run(self)
self.components.Dispose()
WF.Form.Dispose(self)
self.Close()
def main():
ScriptForm().run()
if __name__ == '__main__':
main()