Writing windows forms application is very easy if you’re using ironpython. You can accept input from user with ironpython just like the way you do with C# windows forms. In this article you’ll learn how to work with textbox widget to accept user input. If you want to use single lines of text then this widget is very useful in your application.
We’re going to continue with program in previous tutorial of labels. This is going to be a short tutorial for adding and displaying textbox widget on your program. In future tutorials we’ll cover how to handle the text input from the events and changing the label or other parts of program.
Step 1: In order to create a textbox, call the constructor with no arguments. Now our textbox is added to the current program, we just need to set the properties of text box.
self.textbox = TextBox()
Step 2 : You have to add custom text to textbox widget.
self.textbox.Text = "Baby Text Widget"
Step 3 : You can also edit the position of the widget as per your wish. Use the following positions for now in your program.
self.textbox.Location = Point(50, 50)
Step 4: After position it’s time to set the width for the text widget. I’m setting width of ‘200’ to the widget. You can increase or decrease it as per you wish.
self.textbox.Width = 200
Now our program is ready to display the textwidget on windows form. You can run this program now on terminal or command prompt.
import clr clr.AddReference("System.Drawing") clr.AddReference("System.Windows.Forms") from System.Drawing import Point from System.Windows.Forms import Application, Form, Label,TextBox class LabelDemoForm(Form): def __init__(self): self.Text = 'Text Widget Demo' self.label = Label() self.label.Text = "This is text widget Demo" self.label.Location = Point(100, 150) self.label.Height = 50 self.label.Width = 250 self.textbox = TextBox() self.textbox.Text = "Baby Text Widget" self.textbox.Location = Point(50, 50) self.textbox.Width = 200 self.Controls.Add(self.label) self.Controls.Add(self.textbox) form = LabelDemoForm() Application.Run(form)
Note: Make sure you’re keeping proper indentation in the program otherwise there are chances of warning throw on your program. Also don’t forget to define ‘textbox’ in line no 5.