WinForms and DataBinding

Public forum to share code snippets, screen shorts, experiences, etc.
Post Reply
User avatar
wriedmann
Posts: 3756
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

WinForms and DataBinding

Post by wriedmann »

After working a lot with WPFs databinding, I have researched a bit about WinForms databinding, and I have to present a possible solution (again influenced from the years of VO programming :) ).
WinForms databinding seems to be not so powerful as WPFs databinding, but simple solutions are possible also here. Please look at this peace of code:

Code: Select all

using System.Windows.Forms        
using System.ComponentModel

class BaseView inherit Form
	
constructor()
	
return
	
method Use( oViewModel as INotifyPropertyChanged ) as void 
local oControls	as System.Windows.Forms.Control.ControlCollection
local oTextBox as TextBox

oControls := self:Controls
foreach oControl as Control in oControls 
  do case
  case oControl is TextBox
    oTextBox := ( TextBox ) oControl
    oTextBox:DataBindings:Add( "Text", oViewModel, oTextBox:Name )
  endcase
next
	
return
	
end class
Inherit your window class from this class (BaseView), and then build a ViewModel that implements the INotifyPropertyChanged interface. Assign this ViewModel then to your window in the Use() method, and the databinding will work:

Code: Select all

oSampleView := SampleView{}
oSampleView:Use( SampleViewModel{} )
Application.Run( oSampleView )
Of course, the data in your ViewModel (or you can use the terminus "server") if you prefer :) ) can come from any datasource, even from a DBF.

As always you can find a complete working sample application in XIDE export format attached to this message.

Wolfgang
Attachments
WinformsDataBindingApp.zip
(3.39 KiB) Downloaded 134 times
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
JohnBonnett88
Posts: 51
Joined: Mon Mar 07, 2022 12:34 am

Re: WinForms and DataBinding

Post by JohnBonnett88 »

Is there any other documentation, examples or advice on doing VO.GUI to Windows.Forms conversions?
User avatar
wriedmann
Posts: 3756
Joined: Mon Nov 02, 2015 5:07 pm
Location: Italy

Re: WinForms and DataBinding

Post by wriedmann »

Hi John,
the attachment is also available here:
https://www.riedmann.it/download/Winfor ... gApp.viaef
And I may point you to the material of my session in Memmingen about the GUI choices for VO programmers:
https://www.riedmann.it/download/GUI_Ch ... terial.zip
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
Post Reply