Winform input value after pressing a button, to OUT

Hi @bjwprinsen
In the future, It’s easier for others to help if you attach the file or paste the code as text.

I don’t know much about WF, but this solution works.
There is probably a better way of doing this but below is how I solved it in a different scenario.

I think ideally you would find the control by name

Good luck!

import clr
clr.AddReference('System')
clr.AddReference('System.Drawing')
clr.AddReference("System.Windows.Forms")
from System.Windows import Forms
from System.Windows.Forms import Application, Button, Form, Label, CheckBox, DialogResult, TextBox
from System.Drawing import Point, Icon


class pyRevitPlusForm(Form):

    def __init__(self):
        self.Text = 'pyRevit Plus'
        textbox = TextBox()
        textbox.Location = Point(0, 0)
        textbox.Text = "Available Packages"
        textbox.AutoSize = True
        textbox.Name = 'value'
        self.Controls.Add(textbox)        
        
        button_update = Button()
        button_update.Text = "Update"
        button_x = 0
        button_y = 40
        button_update.Location = Point(button_x, button_y)
        button_update.Click += self.form_update
        self.Controls.Add(button_update)
        self.Height = button_y + 80
        self.Width = button_x + button_update.Width + 40

    def form_update(self, sender, event):
		for control in self.Controls:
			if control.Name == 'value':
				self.value = control.Text
		self.Close()


if IN[0]:
	form = pyRevitPlusForm()
	Application.Run(form)

	OUT = form.value

I’m a beginner in WinForms, but this should work:

import clr

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

import System

from System.Windows.Forms import *
from System.Drawing import *

# Create a Class Form
class CreateWindow(Form):
    def __init__(self): 
    
        # Create the Form
        self.Name = "Create Window"
        self.Text = "Create Window"
        self.Size = Size(500, 100)        
        self.CenterToScreen()

        # Create Label = label
        label = Label(Text = "Property")
        label.Parent = self
        label.Location = Point(30, 20)

        # Create TextBox = textbox
        self.textbox = TextBox()
        self.textbox.Parent = self
        self.textbox.Text = "Give Up Value"
        self.textbox.Location = Point(150, 20)
        self.textbox.Width = 150
        
        # Create Button = button
        button = Button()
        button.Parent = self
        button.Text = "Apply"
        button.Location = Point(400, 20)
        # Register event
        button.Click += self.ButtonClicked
        
    # Create button event
    def ButtonClicked(self, sender, args):
        if sender.Click:
            self.value = self.textbox.Text
            self.Close()            

if IN[0]:
	form = CreateWindow()
	Application.Run(form)
	
	OUT = form.value

Saludos.

3 Likes