【问题标题】:Python.NET WinForms - Loop through each comboboxPython.NET WinForms - 遍历每个组合框
【发布时间】:2021-06-26 11:31:09
【问题描述】:

循环遍历每个组合框(我有四个)并获取其文本的最佳解决方案是什么?

一些文档(通过 c#)说要创建一个组合框列表,但我不确定如何通过 .NET Framework 将其移植到 Pyhton。我所做的一切似乎都会产生一些错误。

我可以循环遍历所有控件,但研究表明这没有必要并且会消耗 CPU 时间。

说了这么多,这是我迄今为止所做的没有成功的事情。我已经删除了代码的其他部分来专注于这一点:

import clr

clr.AddReference("System.Windows.Forms")
clr.AddReference("System.Drawing")
clr.AddReference("System.Data")
clr.AddReference("System.Globalization")
clr.AddReference("System.Collections")

from System.Windows.Forms import *
from System.Drawing import *
from System.Data import *
from System.Data.SqlClient import *
from System.Globalization import *
from System.Collection.Generics import *

    def FindPopulatedDropDowns():
        # https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=net-5.0
        Items = List[string]()
        
        comboBoxes = OfType[ComboBox]().Where(x in x.Name.StartsWith('comboBox'))
        
        for cmb in comboBoxes:
            for cmbitem in cmb.Items:
                print(cmbitem.ToString())

有人对如何实现这一点有任何想法吗?

【问题讨论】:

    标签: .net combobox clr ironpython


    【解决方案1】:

    这是我的解决方案。我相信还有更优雅的方法,但这可以满足我的需求。

        def FindPopulatedDropDowns(self):
            
            # https://docs.microsoft.com/en-us/dotnet/api/system.object.gettype?view=net-5.0#System_Object_GetType
            for c in self.Controls:
                if c.GetType() == ComboBox:
                    print(c.Text)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-29
      • 1970-01-01
      • 1970-01-01
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 2014-05-08
      • 1970-01-01
      相关资源
      最近更新 更多