Home > Back-end >  Return winforms button value
Return winforms button value

Time:03-06

The code below is in Ironpython, but I am looking for winforms solution in Ironpython or any other language (C#, VB...)

I have a winforms button:

self._button1 = System.Windows.Forms.Button()
self._button1.Location = System.Drawing.Point(234, 191)
self._button1.Name = "selectFolder_button"
self._button1.Size = System.Drawing.Size(121, 23)
self._button1.TabIndex = 1
self._button1.Text = "select"
self._button1.Click  = self.Button1Click
self.Controls.Add(self._button1)

which when clicked opens 'select folder dialog' with:

def FolderDialog(self):
    dialog = System.Windows.Forms.FolderBrowserDialog()
    if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK):
        folderString = dialog.SelectedPath
        return folderString

def Button1Click(self, sender, e):
    folderString = self.FolderDialog()
    return folderString

How can I call the 'folderString' in some other method within the same winforms form? To see which value it has. Is there some 'self._button1.Value' like property? I would be grateful for any kind of help. Thank you in advance.

CodePudding user response:

I found a solution here. Essentially I created a class, then instantiated it the Winforms class global scope:

class Folder():
    def __init__(self, val):
        self.Value = val

selectedFolder = Folder(R"C:")  # instantiate class with default folder path

Then assigned to it a new value inside the "Button1Click" method:

def Button1Click(self, sender, e):
    folderString = self.FolderDialog()
    
    # assigned the 'folderString'
    self.selectedFolder.Value = folderString 

In the end I just call the 'self.selectedFolder.Value' inside any other method of the Winforms class.

Thank you for the help @Idle_Mind

  • Related