简体   繁体   English

文本框值在BindingContext Changed事件上未更改

[英]Textbox values not changing on BindingContext Changed event

Currently to change the record displayed in textfields in the form on the click of next button, I am programmatically assigning values of next record to the relevant textboxes using below code: 当前,要更改单击下一步按钮形式在文本字段中显示的记录,我正在使用以下代码以编程方式将下一记录的值分配给相关文本框:

int CurrentRowIndex = this.BindingContext[DS1, "Table1"].Position;
int NextRoecordIndex = CurrentRowIndex + 1;

I am then using the NextRecordIndex ot populate values in the textboxes from the DataSet. 然后,我在数据集中的文本框中使用NextRecordIndex ot填充值。 I want that as soon as the below code is executed on click the next button, next record should automatically get populated. 我希望在单击“下一步”按钮后执行以下代码,则下一条记录应自动填充。

this.BindingContext[DS1, "Table"].Position -= 1;

Are you listening to the BindingContextChanged event? 您在听BindingContextChanged事件吗? Maybe you need to add an event delegate to the textbox's BindingContextChanged event or to override in it's derived class.. 也许您需要向文本框的BindingContextChanged事件添加事件委托,或在其派生类中重写。

You need to implement the event handler. 您需要实现事件处理程序。 To do so create the member function which shall be called each time the event occurs in your text control. 为此,创建成员函数,每次事件在文本控件中发生时都将调用该成员函数。 To listen to this event attach (register) your created member function to the event delegate of your textbox. 要收听此事件,请将您创建的成员函数附加(注册)到文本框的事件委托。 Now every time the textbox fires its BindingContextchanged event all functions attached to its delegate are invoked. 现在,每次文本框触发其BindingContextchanged事件时,都将调用附加到其委托的所有函数。

Create the function to be called each time the event occours. 创建每次事件发生时要调用的函数。

private void AssignNextRecordIndex_OnBindingContextChanged(object sender, EventArgs e) 
{
    // do ur task on bindingcontext changed
}

Add or register to listen to the event: 添加或注册以收听事件:

MyTextBox.BindingContextChanged += AssignNextRecordIndex_OnBindingContextChanged;

Thats it. 而已。

To detach or to stop listening to this event (if neccessary) use: 要分离或停止收听此事件(如有必要),请使用:

MyTextBox.BindingContextChanged -= AssignNextRecordIndex_OnBindingContextChanged;

You can attach as many functions to events as you like. 您可以根据需要为事件添加尽可能多的功能。

You also find information on this special event on MSDN: Control.BindingContextChanged Event 您还可以在MSDN上找到有关此特殊事件的信息: Control.BindingContextChanged事件

And also some basics about events in .NET Events (C# Programming Guide) 以及.NET 事件(C#编程指南)中有关事件的一些基础知识

In case you created your own custom TextBox control like: 如果您创建了自己的自定义TextBox控件,例如:

class MyCustomTextBox : TextBox
{...}

you can override the inherited OnBindingContextChanged method instead (don't forget to call the base method): 您可以改写继承的OnBindingContextChanged方法(不要忘记调用基本方法):

protected override void OnBindingContextChanged(EventArgs e)
{
    base.OnBindingContextChanged(e);
    // do your stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM