反射获取窗体所有控件的Text

可以直接通过反射获取当前窗体的所有控件的Text(具有Text属性),具体代码如下:

foreach (var field in form.GetType().GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public))
            {
                string fieldValue = "";
                try
                {
                    PropertyInfo proText = field.FieldType.GetProperty("Text");
                    if (field.FieldType == typeof(System.Windows.Forms.Label) ||
                        field.FieldType == typeof(DevComponents.DotNetBar.LabelX)
                        )
                    {
                        fieldValue = proText.GetValue(field.GetValue(form), null).ToString();
                    }
                    else if (field.FieldType == typeof(System.Windows.Forms.Button) ||
                        field.FieldType == typeof(DevComponents.DotNetBar.ButtonX) ||
                        field.FieldType == typeof(GPOS.Controls.ButtonNew)
                        )
                    {
                        fieldValue = proText.GetValue(field.GetValue(form), null).ToString();
                    }
                    else if (field.FieldType == typeof(DevComponents.DotNetBar.ButtonItem) ||
                        //field.FieldType == typeof(DevComponents.DotNetBar.TextBoxItem) ||
                                field.FieldType == typeof(DevComponents.DotNetBar.LabelItem)
                        )
                    {
                        fieldValue = proText.GetValue(field.GetValue(form), null).ToString();
                    }
                    else if (field.FieldType == typeof(System.Windows.Forms.ToolStripMenuItem)
                        )
                    {
                        fieldValue = proText.GetValue(field.GetValue(form), null).ToString();
                    }
                    else if (field.FieldType == typeof(System.Windows.Forms.ToolStripButton)
                        )
                    {
                        // fieldValue = proText.GetValue(field.GetValue(form), null).ToString();
                        PropertyInfo proToolTipText = field.FieldType.GetProperty("ToolTipText");
                        fieldValue = proToolTipText.GetValue(field.GetValue(form), null).ToString();
                    }
                    else if (field.FieldType == typeof(System.Windows.Forms.CheckBox) ||
                         field.FieldType == typeof(DevComponents.DotNetBar.Controls.CheckBoxX)
                        )
                    {
                        fieldValue = proText.GetValue(field.GetValue(form), null).ToString();
                    }
                    else if (field.FieldType == typeof(System.Windows.Forms.DataGridViewTextBoxColumn) ||
                         field.FieldType == typeof(System.Windows.Forms.DataGridViewCheckBoxColumn)
                        )
                    {
                        PropertyInfo proHeaderText = field.FieldType.GetProperty("HeaderText");
                        fieldValue = proHeaderText.GetValue(field.GetValue(form), null).ToString();
                    }
                    else
                    {
                        continue;
                    }
                }
                catch
                { }
            }
代码

 

转载于:https://www.cnblogs.com/sczmzx/p/3605349.html

### 回答1: 在Python中,可以使用Tkinter库来创建窗体控件,并使用相应的方法获取窗体中的所有控件。以下是一个示例代码: ```python import tkinter as tk def get_all_widgets(widget): # 获取窗体中的所有控件 widget_list = [widget] for child in widget.winfo_children(): widget_list.extend(get_all_widgets(child)) return widget_list # 创建一个窗体 window = tk.Tk() # 添加控件窗体 label = tk.Label(window, text="Hello World!") button = tk.Button(window, text="Click Me!") # 打印窗体中的所有控件 widget_list = get_all_widgets(window) for widget in widget_list: print(widget) # 运行窗体的消息循环 window.mainloop() ``` 以上代码中,首先导入了Tkinter库,并定义了一个`get_all_widgets`函数,该函数递归地获取窗体中的所有控件。然后创建了一个窗体对象`window`,并在窗体中添加了一个`label`标签和一个`button`按钮。最后,通过调用`get_all_widgets`函数并遍历打印出窗体中的所有控件。 你可以根据实际需求对控件进行相应的处理,例如对按钮添加事件处理等。 ### 回答2: 要获取窗体中的所有控件,我们可以使用Python中的第三方库PyQt或Tkinter。以PyQt为例,我们可以使用递归方法实现该功能。 首先,我们需要导入PyQt库中的所有控件模块。然后,创建一个递归函数,该函数接受一个控件作为参数。在函数内部,我们通过判断当前控件是否有子控件,如果有,则递归调用该函数来获取控件。同时,我们可以将控件属性和信息保存到列表或字典中,以便后续使用。 下面是一个使用PyQt获取窗体所有控件的示例代码: ```python from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QLabel, QVBoxLayout from PyQt5 import sip def get_all_widgets(container): for widget in container.children(): if isinstance(widget, QWidget): # 在这里可以保存控件属性和信息 print(widget.objectName(), type(widget)) get_all_widgets(widget) else: # 在这里可以保存控件属性和信息 print(widget.objectName(), type(widget)) if __name__ == '__main__': app = QApplication([]) window = QWidget() layout = QVBoxLayout() button = QPushButton('Button') label = QLabel('Label') layout.addWidget(button) layout.addWidget(label) window.setLayout(layout) get_all_widgets(window) sip.delete(window) window = None app.exec_() ``` 在以上示例代码中,我们首先创建了一个QWidget窗体,并添加了两个控件,一个QPushButton按钮和一个QLabel标签。然后,我们调用get_all_widgets函数并传入容器window,函数将通过递归获取窗体中的所有控件,并打印控件的对象名称和类型。最后,我们释放内存资源。 通过运行以上示例代码,我们可以在控制台看到输出窗体中所有控件的对象名称和类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值