0

I have 2x TextBoxes which represents a BINARY numbers. I have controls (Checkbox and Combobox) which controls certain bits in each TextBox. currently, when I change the values in the controls, it changes the relevant bits in the TextBox.

I'd like to type a binary number into the Textbox.
I'd like that the other controls (CheckBox, ComboBox, Other Textbox), will be updated according the new value of the Textbox.

I Know it is possible to create a method for each control that will get the relevant bits, and will trigger by an event, but since in my real project I have 25 TextBoxes and many Checkboxes, it is hard to implement.
How can I do it right ?

enter image description here

rene
  • 41,474
  • 78
  • 114
  • 152
orenk
  • 109
  • 1
  • 2
  • 12
  • Too tired to make a proper answer, someone else shall take the credits :p... Implement one or two event methods, use the "sender" object (your methods will depend on wether this sender is a TB or a CB or else), those events will be on CheckedChanged and TextChanged, and in there you will say "apply the sender's value to every other control that has the same type". That's the general idea, you will have to provide additional information for a more precise answer. – Kilazur May 26 '14 at 07:23
  • Are you using MVVM approach? Is it WinForms or WPF? – Edin May 26 '14 at 07:23

2 Answers2

0

I don't know if you are using database for your model, if so then you can create datatable and bind all textboxes to data in to DatatTable by using DataBindings on Text property (it is availble in text box properties)

If you don't have database model, you can bind all textboxes to some custom model class (by adding object source). Your class should implement INotifyPropertyChanged as described here: http://msdn.microsoft.com/en-us/library/ms229614.aspx.

I'm not sure when the updates are occuring on GUI, i mean if this will update gui while typing or when you change your current control to another...

lag
  • 89
  • 7
  • this is an SO Article about this approach http://stackoverflow.com/questions/2820447/net-winforms-inotifypropertychanged-updates-all-bindings-when-one-is-changed-b – lag May 26 '14 at 10:50
0

The good way to implement that would be to have a model (ViewModel) to which your WinForm is bound to. That means that you have an object which will have properties that correspond to your text field. Each text field should be bound to the corresponding model property, and therefore get changes via PropertyChanged events. The logic which changes values of your properties should be implemented and encapsulated on the model level and not UI. One of many benefits of this approach, is that you can unit test your model and logic without UI being involved.

Make sure, you have a look at the following thread, which will give an insight into the MVVM pattern: MVVM: Tutorial from start to finish?

It is very commonly used and it is one of the best design patterns for UI programming. It is however up to you to decide, if it matches your use-case.

Community
  • 1
  • 1
Edin
  • 1,476
  • 11
  • 21
  • is it possible to apply MVVM with WinForms ? – orenk May 26 '14 at 11:32
  • Yes, it is possibile. MVVM is a design pattern which can be applied in various technologies. However, I must admit it is suitable the best for WPF. You might find the following thread useful since it addresses this question: http://stackoverflow.com/questions/595469/ui-design-pattern-for-windows-forms-like-mvvm-for-wpf – Edin May 26 '14 at 11:49