Home > other >  where to put code detecting user changes in bound textboxes, comboboxes and checkboxes
where to put code detecting user changes in bound textboxes, comboboxes and checkboxes

Time:01-11

I'm trying to learn c# forms and have build a simple table editor app. i have created all the controls, a bindingnavigator, a bindingsource and a dataset. Most controls are textbox to text field so no big problem binding. I had to create format and parse functions and bind them to the checkbox and dropdown box controls, but this is working fine. I need to make the controls activate a save and cancel button when the user has changed them, but any event I try to underwire gets fired by the data being initially loaded or bound.

I've tried flagging in the binding complete event, and item changed events, but they seem to get fired before the loading is complete on the form.

Where should I put code to say this record has been loaded and any changes to controls are now user interaction?

I have tried checking in the textbox_changed event if the bindingsource.current is null. I have tried setting a flag in the end if the LoadData() function. I have tried setting a flag in the bindingsource bindingcomplete event.

None of these allow me to effectively detect when the form with it's first (or next) record to display is effectively ready for the user to start editing.

I've looked in the events of the form, bindingnavigator, bindingsource and dataset but can't see an obvious place to detect "I'm done loading and formatting stuff with this record now" I've looked at online tutorials but they all show how to achieve binding but not setting up editing.

CodePudding user response:

Since you are using DataTable, you can handle DataTable.ColumnChanged event and enable the save button there.

The event raises after the value of the column changes, which depending to the databinding settings of your control is by default after validation of the control property (DataSourceUpdateMode.OnValidation); if you want to raise the event immediately, even before leaving and validating control, then change it using code or designer to DataSourceUpdateMode.OnValidation.

Example

Assuming you have a button (button1) and two textbox controls (textBox1 and textBox2), modify the code of the form to the following to see how button1 will be enabled after modifying one of the columns through textbox controls:

public partial class Form1 : Form
{
    DataTable dt = new DataTable();
    public Form1()
    {
        InitializeComponent();
        dt.Columns.Add("C1", typeof(int));
        dt.Columns.Add("C2", typeof(string));
        textBox1.DataBindings.Add("Text", dt, "C1",
            true, DataSourceUpdateMode.OnPropertyChanged);
        textBox2.DataBindings.Add("Text", dt, "C2", 
            true, DataSourceUpdateMode.OnPropertyChanged);
    }
    private void Form1_Load(object sender, EventArgs e)
    {
        dt.Rows.Add(1, "One");
        dt.Rows.Add(2, "Two");
        dt.AcceptChanges();
        button1.Enabled = false;
        dt.ColumnChanged  = Dt_ColumnChanged;
    }
    private void Dt_ColumnChanged(object sender, DataColumnChangeEventArgs e)
    {
        button1.Enabled = true;
    }
}

Read More

  • Related