开发者

C# WinForms - Databound simple components with Cancelable form

How can I implement a form with simple components (text boxes, comboboxes) bound to an object's properties and have Save/Cancel support?

I have tried the code snippet at http://fgheysels.blogspot.com/2009/06/winforms-databinding-on-cancellable.html but I dont know what is required for this to work bindin开发者_如何学运维g to object properties.

This is what I have but I cant figure out how to get the cancel button to prevent the objects property from being updated. The properties are updated with whatever value was last input, even when the cancel button was pressed.

    public DocumentProperties(FileFormatReader fileReader)
    {
        binding = new BindingSource();
        binding.DataSource = fileReader.Header;
        bindingManager = BindingContext[binding.DataSource];

        unitComboBox.DataSource = Enum.GetNames(typeof(Constants.Units));
        unitComboBox.DataBindings.Add("SelectedIndex", binding.DataSource, "UnitNumberIndex");

        operatorTextBox.Text = fileReader.Header.OperatorName;
        operatorTextBox.DataBindings.Add("Text", binding, "OperatorName");

        binding.SuspendBinding();       // Doesnt work
   }

    private void okButton_Click(object sender, EventArgs e)
    {
        binding.ResumeBinding();
        this.DialogResult = DialogResult.OK;
    }

    private void cancelButton_Click(object sender, EventArgs e)
    {
        this.DialogResult = DialogResult.Cancel;
    }

Any suggestions on how to do this would be appreciated.


Cache the value when you open it, and reset it to the cached value in the cancel buttons handler.


The object you are binding should implement IEditableObject Interface which has methods for beginning, ending and canceling editing.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜