如何循环通过DataBindings添加Control.Parse和Control.Format? [英] How do I loop through DataBindings to add Control.Parse and Control.Format?
问题描述
这是我在想的。
首先,我将在加载方法中调用此方法:
private void PatientForm_Load(object sender,EventArgs e)
{
this.tableAdapter.Fill(this.DBDataSet.People);
UpdateBindings(this);
}
然后,我将循环通过this的控件注册EventHandler对每个TextBox控件:
private void UpdateBindings(Control controlGroup)
{
object controlItem;
foreach(controlGroup.Controls中的对象controlItemLoop)
{
//通过此控件的项目重新获得它们的绑定...
controlItem =(Control)controlItemLoop;
UpdateBindings((Control)controlItem);
//更新数据格式化控件
TextBox controlItemTextBox = default(TextBox);
if(controlItem是TextBox)
{
controlItemTextBox =(TextBox)controlItem;
int i = 0;
foreach(controlItemTextBox.DataBindings中的绑定绑定)
{
绑定b =新的System.Windows.Forms.Binding(controlItemTextBox.DataBindings [i] .PropertyName,controlItemTextBox.DataBindings [i]。 DataSource,controlItemTextBox.DataBindings [i] .BindingMemberInfo.BindingMember,true);
//这里,如何分配控件的Control.DataBindings?
//喜欢这个? controlItemTextBox.DataBindings [i] = b;
b.Parse + = new ConvertEventHandler(BindingParse_TextBox);
b.Format + = new ConvertEventHandler(BindingFormat_TextBox);
i ++;
}
}
}
this.patientsBindingSource.ResetCurrentItem();
}
然后,当然:
private void BindingFormat_TextBox(object sender,ConvertEventArgs e)
{
//解密
if(e.DesiredType!= typeof(string) )
return;
if(e.Value.GetType()!= typeof(string))
return;
string value =
try
{
e.Value = Decrypt((string)e.Value);
}
catch(异常错误)
{
MessageBox.Show(发生格式错误,请再次尝试操作。\\\
\\\
+ error.Message ,数据格式错误,MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
}
我该怎么做?
如果数据绑定已经存在,您不必重新创建它们,只是为了添加解析调用:
foreach(在controlItemTextBox.DataBindings中绑定绑定)
{
binding.Parse + = new ConvertEventHandler(BindingParse_TextBox);
binding.Format + = new ConvertEventHandler(BindingFormat_TextBox);
}
I'm somewhat new to C#. I have a WinForm with several dozen different controls (many of them TextBox controls). I used the designer to add databinding to all of them to get data when the form loads. However, the values in the database are encrypted. (They look like this: "MasAFfa31sdf3fE23AF235==" when they should look like this: "Suzy Doe".) I wanted to be able to add a method to the Format event handler to decrypt each binding's data before it appears in the form.
My questions are: How can I make a method that loops through the this.DataBindings and adds the Format event handler to decrpyt the value before the form displays? How do I assign a Binding to the DataBindings for a control?
Here is what I was thinking.
First, I would call this method in the loading method:
private void PatientForm_Load(object sender, EventArgs e)
{
this.tableAdapter.Fill(this.DBDataSet.People);
UpdateBindings(this);
}
Then, I would loop through the controls of 'this' to register the EventHandler to each TextBox control:
private void UpdateBindings(Control controlGroup)
{
object controlItem;
foreach (object controlItemLoop in controlGroup.Controls)
{
// Recurse through this control's items and get their bindings...
controlItem = (Control)controlItemLoop;
UpdateBindings((Control)controlItem);
// Update Controls for Data Formatting
TextBox controlItemTextBox = default(TextBox);
if (controlItem is TextBox)
{
controlItemTextBox = (TextBox)controlItem;
int i = 0;
foreach (Binding binding in controlItemTextBox.DataBindings)
{
Binding b = new System.Windows.Forms.Binding(controlItemTextBox.DataBindings[i].PropertyName, controlItemTextBox.DataBindings[i].DataSource, controlItemTextBox.DataBindings[i].BindingMemberInfo.BindingMember, true);
// Here, how do I assign a control's Control.DataBindings?
// Like this? controlItemTextBox.DataBindings[i] = b;
b.Parse += new ConvertEventHandler(BindingParse_TextBox);
b.Format += new ConvertEventHandler(BindingFormat_TextBox);
i++;
}
}
}
this.patientsBindingSource.ResetCurrentItem();
}
Then, of course:
private void BindingFormat_TextBox(object sender, ConvertEventArgs e)
{
// Decrypt
if (e.DesiredType != typeof(string))
return;
if (e.Value.GetType() != typeof(string))
return;
string value =
try
{
e.Value = Decrypt((string)e.Value);
}
catch(Exception error)
{
MessageBox.Show("A formatting error occurred. Please attempt the operation again.\n\n" + error.Message, "Data Formatting Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
How can I do this?
If the data bindings already exist, you don't have to recreate them just to add the parse call:
foreach (Binding binding in controlItemTextBox.DataBindings)
{
binding.Parse += new ConvertEventHandler(BindingParse_TextBox);
binding.Format += new ConvertEventHandler(BindingFormat_TextBox);
}
这篇关于如何循环通过DataBindings添加Control.Parse和Control.Format?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!