Python CLR Winforms – Passing data between .NET Winforms

Question:

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())
Asked By: EricALionsFan

||

Answers:

So, I figured it out…again.

I had to set a python global variable within one of my events that triggers an event, like so…

def dgvExpanderInfo_CellDoubleClick_Event(self, sender, args):
    global SelectedExpanderData_List

    ...

…then I could access whatever is in that globabl variable – in this case it was a list.

    def MyForm2_Form_Load_Event(self, sender, args):
        self.textbox1.Text = SelectedExpanderData_List[0]
        self.textbox2.Text = SelectedExpanderData_List[1]
        self.textbox3.Text = SelectedExpanderData_List[2]
        ...

I hope this helps others as I have found no real documentation on this anywhere.

Answered By: EricALionsFan