Python CLR Winforms - Passing data between .NET Winforms

Viewed 4

I have a fairly simple task that has eluded me when using Python to generate and automate .NET WinForms. How do I pass data between forms?

I've tried everything: using global variables, using immutable strings, etc. and nothing seems to stick. Can someone show me an example, send me a link, or let me know what I am doing wrong? I have been at this for over a week and frustration is starting to mount.

Below is a (sloppy) example of taking data from one form - a string - and sending it to another form in a Textbox.

MYSTRING = ''

import clr

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

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

class MyForm(Form):
    def __init__(self):
        self.Text1 = TextBox()
        self.Button1 = Button()
        
        self.Button1.Location = Point(0, self.Text1.Bottom + 10)
        self.Button1.Text = 'Send'
        
        self.Controls.Add(self.Text1)
        
        self.Controls.Add(self.Button1)
        
        self.Button1.Click += self.Button1_Click
        
    def Button1_Click(self, sender, args):
        MYSTRING = self.Text1.Text
        self.TopLevel = False
        f2 = MyForm2()
        f2.Show()
        self.TopLevel = True
        

class MyForm2(Form):    
    def __init__(self):
        self.Text2 = TextBox()
        
        self.Controls.Add(self.Text2)
        
        self.Load += self.MyForm2_Load
        
    def MyForm2_Load(self, sender, args):
        self.Text2.Text = MYSTRING

Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)

Application.Run(MyForm())
0 Answers
Related