返回winform按钮值

发布于 2025-01-10 16:28:37 字数 943 浏览 0 评论 0原文

下面的代码是在 Ironpython 中,但我正在寻找 Ironpython 或任何其他语言(C#、VB...)中的 winforms 解决方案

我有一个 winforms 按钮:

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)

单击该按钮时会打开“选择文件夹对话框”:

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

我怎样才能调用'folderString' 在同一 winforms 表单中的其他方法中?看看它有什么价值。是否有一些像“self._button1.Value”这样的属性? 我将不胜感激任何形式的帮助。先感谢您。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

赠我空喜 2025-01-17 16:28:37

我在此处找到了解决方案
本质上,我创建了一个类,然后将其实例化为 Winforms 类全局范围:

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

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

然后在“Button1Click”方法中为其分配了一个新值:

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

最后,我只在 Winforms 的任何其他方法中调用“self.selectedFolder.Value”班级。

感谢您的帮助@Idle_Mind

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文