开发者

How do I loop through DataBindings to add Control.Parse and Control.Format?

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);
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜