如何循环通过DataBindings添加Control.Parse和Control.Format? [英] How do I loop through DataBindings to add Control.Parse and Control.Format?

查看:108
本文介绍了如何循环通过DataBindings添加Control.Parse和Control.Format?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#有点新奇。我有一个WinForm与几十个不同的控件(其中很多TextBox控件)。我使用设计人员在数据绑定中添加数据,以便在表单加载时获取数据。但是,数据库中的值被加密。 (他们看起来像这样:MasAFfa31sdf3fE23AF235 ==,当他们应该看起来像这样:Suzy Doe。)我希望能够在格式事件处理程序中添加一个方法来解密每个绑定的数据,然后再出现在表单中。我的问题是:如何创建一个循环遍历this.DataBindings的方法,并在格式显示之前添加Format事件处理程序来decrpyt值?如何为控件分配绑定到DataBindings?



这是我在想的。



首先,我将在加载方法中调用此方法:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆