在C#WinForms中提升自定义事件 [英] Raise custom events in C# WinForms
问题描述
可能我的应用程序设计也搞砸了,可能会
这是结构
ProgramContext
- Form MainWindow
+控件TextBox
+控件按钮
+ ...
在这种情况下, MainWindow.TextBox
保存一些在运行时经常更新的信息。所以,我需要它来刷新自己,当我想(所以它可以从数据库中重新加载数据库,它存储在哪里)
我尝试挂钩一个code> EventHandler 到它的验证
-Event,但似乎没有这样做。
所以,基本上我有一个方法来重新加载 ProgramContext
DataTable table = _adapter.GetData();
foreach(table.Rows中的DataRow行)
{
MainWindow.TextBox.Text + = table.Text.ToString();
}
只要有其他方法(将新数据写入<$执行c $ c>表)
任何想法?
编辑:似乎你的问题更多的是关于一个特定的事件,但下面的FWIW是如何消除一般的常规事件。
处理TextBox更改事件
根据我的理解,您希望外部方监视Form上的文本框中引发的事件,然后在另一个表单上重新加载数据?
快速而肮脏的方式是使Form TextBox公开,然后其他人可以订阅此事件
MainForm.textBox1.TextChanged + = new System.EventHandler(this.textBox1_TextChanged);
或者,在更新版本的C#中:
MainForm.textBox1.TextChanged + = this.textBox1_TextChanged;
添加自己的自定义事件
另一个更干净的方法是提出自定义事件, - 例如 MyDataChangedEvent
以下。这将允许您抽出一个事实,即更改来自一个文本框。
//假设您需要为您的活动定制签名。如果没有,请使用现有的标准事件委托
public delegate void myDataChangedDelegate(object sender,YourCustomArgsHere args);
//暴露您的组件的事件
public event myDataChangedDelegate MyDataChangedEvent;
//并提高
var eventSubscribers = MyDataChangedEvent;
if(eventSubscribers!= null)
{
eventSubscribers(this,myCustomArgsHere);
}
您还可以查看Ent Lib复合应用程序块和智能客户端软件工厂 - 这是一个非常灵活的事件经纪/ pub子机制,用于在SmartParts(控件,表单对话框等)中以松散耦合的方式同步。(CAB现在已经过时了) 。
I have some Events I created on my own and was wondering on how to raise them when I want.
Probably my application design is also messed up, might take a look at that if you like.
This is the Structure
ProgramContext
- Form MainWindow
+ Control TextBox
+ Control Button
+ ...
In this case, the MainWindow.TextBox
holds some information that is updated quite often at runtime. So, I somehow need it to refresh itself when I want to (so it can reload its data from the database, where the it's stored)
I tried hooking an EventHandler
to its Validating
-Event, but that didn't seem to do the trick.
So, basically I have a method that reloads the data in ProgramContext
DataTable table = _adapter.GetData();
foreach (DataRow row in table.Rows)
{
MainWindow.TextBox.Text += table.Text.ToString();
}
That needs to be done whenever another method (that writes new data into table
) is executed.
Any ideas?
Edit : It seems your question is more about hooking into a specific event, but FWIW below is how to fire custom events in general.
Handling the TextBox Changed Event
From what I understand, you want an external party to monitor events raised from a textbox on a Form and then to reload data on another form?
A quick and dirty would be to make the Form TextBox public and then others could subscribe to this event
MainForm.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
OR, in more recent versions of C#:
MainForm.textBox1.TextChanged += this.textBox1_TextChanged;
Adding Your own Custom Event
Another, cleaner way would be to raise a custom event, - e.g. MyDataChangedEvent
below. This will allow you to abstract away the fact that the changes are coming from a textbox at all.
// Assuming you need a custom signature for your event. If not, use an existing standard event delegate
public delegate void myDataChangedDelegate(object sender, YourCustomArgsHere args);
// Expose the event off your component
public event myDataChangedDelegate MyDataChangedEvent;
// And to raise it
var eventSubscribers = MyDataChangedEvent;
if (eventSubscribers != null)
{
eventSubscribers(this, myCustomArgsHere);
}
You might also look at the Ent Lib Composite Application Block and Smart Client Software Factory - this has a very flexible event broking / pub sub mechanism for synchronising across UI "SmartParts" (controls, forms dialogs etc) in a loose-coupled fashion. (CAB is now very dated).
这篇关于在C#WinForms中提升自定义事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!