C# Winforms TabControl 元素在选择 TabPage 之前读取为空
我有用 C# 编写的 Winform 应用程序。在我的表单上,我有一个包含七个页面的 TabControl,每个页面都充满元素(主要是文本框和 DropDownList)。我使用 DataReader 提取一些信息,填充 DataTable,并使用元素的 DataBindings.Add 方法用当前值填充这些元素。
用户可以将数据输入到这些元素中,按“保存”,然后我使用元素的文本字段设置 UPDATE 查询的参数。例如:
updateCommand.Parameters.Add("@CustomerName", SqlDbType.VarChar, 100).Value = CustomerName.Text;
我遇到的问题是,一旦加载表单,所有元素显然都被视为空,直到我手动选择每个选项卡。因此,如果我在加载表单后立即按“保存”,则我尚未选择的选项卡页面上的所有字段都会尝试使用空数据进行更新(不好)。当我选择每个 TabPage 时,这些元素现在将正确发送其数据。目前,我已经制定了一个(非常)丑陋的解决方法,当第一次填充数据时,我以编程方式选择每个 TabPage,但这是一个不可接受的长期解决方案。
我的问题是,如何让 TabPage 上的所有元素在用户选择该 TabPage 之前正确返回其数据?
I have Winform app I am writing in C#. On my form, I have a TabControl with seven pages, each full of elements (TextBoxes and DropDownLists, primarily). I pull some information in with a DataReader, populate a DataTable, and use the elements' DataBindings.Add method to fill those elements with the current values.
The user is able to enter data into these elements, press "Save", and I then set the parameters of an UPDATE query using the elements' Text fields. For instance:
updateCommand.Parameters.Add("@CustomerName", SqlDbType.VarChar, 100).Value = CustomerName.Text;
The problem I have is that once I load the form, all of the elements are apparently considered empty until I select each tab manually. Thus, if I press "Save" immediately upon loading the form, all of the fields on the TabPages that I have not yet selected try to UPDATE with empty data (not nice). As I select each TabPage, those elements will now send their data along properly. For the time being, I've worked out a (very) ugly workaround where I programmatically select each TabPage when the data is populated for the first time, but that's an unacceptable long-term solution.
My question is, how can I get all of the elements on the TabPages to return their data properly before the user selects that TabPage?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
何是正确的;在选择 TabPage 之前,不会创建 TabPage 上的元素。我刚刚在表单加载时添加了一个循环来选择每个 TabPage,现在它工作正常。
ho is correct; the elements on a TabPage are not created until that TabPage is selected. I just added a loop upon form load that selects each TabPage and now it works fine.
我不确定你是否可以。以下引用自 MSDN:
“在显示选项卡页之前,不会创建 TabPage 中包含的控件,并且在显示选项卡页之前不会激活这些控件中的任何数据绑定。”
但是,您可以创建一个类来保存用于填充控件的 DataTable,而不是让更新代码直接从控件获取值,然后当调用更新代码时,它会向该类询问值和类检查控件是否已加载,否则它会从 DataTable 获取值。
I'm not sure if you can. The following is a quote from MSDN:
"Controls contained in a TabPage are not created until the tab page is shown, and any data bindings in these controls are not activated until the tab page is shown."
However, instead of having the update code get the values from the controls directly, maybe you could create a class that could hold that DataTable you use to populate the controls and then when the update code is called it asks the class for the value and the class checks if the control is loaded and otherwise it gets the value from the DataTable instead.
问题是在显示选项卡之前不会创建控件。一种解决方案是在加载页面时实际创建控件,如下所示:
然后在表单上的构造函数中,您将执行以下操作:
该解决方案依赖于这样一个事实:有一个内部 CreateControls 方法采用单个布尔参数,该参数即使该控件不可见,您也可以创建该控件。
The issue is that the controls are not Created until the tab is displayed. One solution would be to actually create the controls on loading the page like so:
Then in the constructor on your form, you'd do something like:
This solution relies on the fact that there is an internal CreateControls method takes a single boolean parameter which will let you create the control even if it is not visible.
我解决此问题的方法是将以下代码添加到表单加载事件中;
My workaround for this issue was to add the following code to the form load event;