Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.02 KB | None | 0 0
  1. bool HasChanged()
  2. {
  3. return this.currentState != this.initialState;
  4. }
  5.  
  6. bool HasChanged()
  7. {
  8. // this._hasChanged is set to true via event handlers
  9. return this._hasChanged;
  10. }
  11.  
  12. Imports System.ComponentModel.Design
  13. Imports System.Windows.Forms
  14. Imports System.ComponentModel
  15.  
  16.  
  17.  
  18. Public Class BindingSourceExIsDirty
  19. Inherits System.Windows.Forms.BindingSource
  20. Implements INotifyPropertyChanged
  21.  
  22. #Region "DECLARATIONS AND PROPERTIES"
  23.  
  24. Private _displayMember As String
  25. Private _dataTable As DataTable
  26. Private _dataSet As DataSet
  27. Private _parentBindingSource As BindingSource
  28. Private _form As System.Windows.Forms.Form
  29. Private _usercontrol As System.Windows.Forms.Control
  30.  
  31.  
  32.  
  33. Private _isCurrentDirtyFlag As Boolean = False
  34.  
  35. Public Property IsCurrentDirty() As Boolean
  36. Get
  37. Return _isCurrentDirtyFlag
  38. End Get
  39. Set(ByVal value As Boolean)
  40. If _isCurrentDirtyFlag <> value Then
  41. _isCurrentDirtyFlag = value
  42. Me.OnPropertyChanged(value.ToString())
  43. If value = True Then 'call the event when flag is set
  44. OnCurrentIsDirty(New EventArgs)
  45.  
  46. End If
  47. End If
  48. End Set
  49. End Property
  50.  
  51. Private _objectSource As String
  52.  
  53. Public Property ObjectSource() As String
  54. Get
  55. Return _objectSource
  56. End Get
  57. Set(ByVal value As String)
  58. _objectSource = value
  59. Me.OnPropertyChanged(value)
  60. End Set
  61. End Property
  62.  
  63.  
  64. ' Private _autoSaveFlag As Boolean
  65. '
  66. ' Public Property AutoSave() As Boolean
  67. ' Get
  68. ' Return _autoSaveFlag
  69. ' End Get
  70. ' Set(ByVal value As Boolean)
  71. ' _autoSaveFlag = value
  72. ' Me.OnPropertyChanged(value.ToString())
  73. ' End Set
  74. ' End Property
  75.  
  76. #End Region
  77.  
  78. #Region "EVENTS"
  79.  
  80.  
  81. 'Current Is Dirty Event
  82. Public Event CurrentIsDirty As CurrentIsDirtyEventHandler
  83.  
  84. ' Delegate declaration.
  85. Public Delegate Sub CurrentIsDirtyEventHandler(ByVal sender As Object, ByVal e As EventArgs)
  86.  
  87. Protected Overridable Sub OnCurrentIsDirty(ByVal e As EventArgs)
  88. RaiseEvent CurrentIsDirty(Me, e)
  89. End Sub
  90.  
  91. 'PropertyChanged Event
  92. Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
  93.  
  94. Protected Overridable Sub OnPropertyChanged(ByVal info As String)
  95. RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
  96. End Sub
  97.  
  98.  
  99.  
  100. #End Region
  101.  
  102. #Region "METHODS"
  103.  
  104.  
  105. Private Sub _BindingComplete(ByVal sender As System.Object, ByVal e As System.Windows.Forms.BindingCompleteEventArgs) Handles Me.BindingComplete
  106.  
  107.  
  108. If e.BindingCompleteContext = BindingCompleteContext.DataSourceUpdate Then
  109. If e.BindingCompleteState = BindingCompleteState.Success And Not e.Binding.Control.BindingContext.IsReadOnly Then
  110.  
  111. 'Make sure the data source value is refreshed (fixes problem mousing off control)
  112. e.Binding.ReadValue()
  113. 'if not focused then not a user edit.
  114. If Not e.Binding.Control.Focused Then Exit Sub
  115.  
  116. 'check for the lookup type of combobox that changes position instead of value
  117. If TryCast(e.Binding.Control, ComboBox) IsNot Nothing Then
  118. 'if the combo box has the same data member table as the binding source, ignore it
  119. If CType(e.Binding.Control, ComboBox).DataSource IsNot Nothing Then
  120. If TryCast(CType(e.Binding.Control, ComboBox).DataSource, BindingSource) IsNot Nothing Then
  121. If CType(CType(e.Binding.Control, ComboBox).DataSource, BindingSource).DataMember = (Me.DataMember) Then
  122. Exit Sub
  123. End If
  124.  
  125. End If
  126.  
  127. End If
  128. End If
  129. IsCurrentDirty = True 'set the dirty flag because data was changed
  130. End If
  131. End If
  132.  
  133.  
  134.  
  135. End Sub
  136.  
  137. Private Sub _DataSourceChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.DataSourceChanged
  138. _parentBindingSource = Nothing
  139. If Me.DataSource Is Nothing Then
  140. _dataSet = Nothing
  141. Else
  142. 'get a reference to the dataset
  143. Dim bsTest As BindingSource = Me
  144. Dim dsType As Type = bsTest.DataSource.GetType
  145. 'try to cast the data source as a binding source
  146. Do While Not TryCast(bsTest.DataSource, BindingSource) Is Nothing
  147. 'set the parent binding source reference
  148. If _parentBindingSource Is Nothing Then _parentBindingSource = bsTest
  149. 'if cast was successful, walk up the chain until dataset is reached
  150. bsTest = CType(bsTest.DataSource, BindingSource)
  151. Loop
  152. 'since it is no longer a binding source, it must be a dataset or something else
  153. If TryCast(bsTest.DataSource, DataSet) Is Nothing Then
  154. 'Cast as dataset did not work
  155.  
  156. If dsType.IsClass = False Then
  157. Throw New ApplicationException("Invalid Binding Source ")
  158. Else
  159. _dataSet = Nothing
  160.  
  161. End If
  162. Else
  163.  
  164. _dataSet = CType(bsTest.DataSource, DataSet)
  165. End If
  166.  
  167.  
  168. 'is there a data member - find the datatable
  169. If Me.DataMember <> "" Then
  170. _DataMemberChanged(sender, e)
  171. End If 'CType(value.GetService(GetType(IDesignerHost)), IDesignerHost)
  172. If _form Is Nothing Then GetFormInstance()
  173. If _usercontrol Is Nothing Then GetUserControlInstance()
  174. End If
  175. End Sub
  176.  
  177. Private Sub _DataMemberChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.DataMemberChanged
  178. If Me.DataMember = "" Or _dataSet Is Nothing Then
  179. _dataTable = Nothing
  180. Else
  181. 'check to see if the Data Member is the name of a table in the dataset
  182. If _dataSet.Tables(Me.DataMember) Is Nothing Then
  183. 'it must be a relationship instead of a table
  184. Dim rel As System.Data.DataRelation = _dataSet.Relations(Me.DataMember)
  185. If Not rel Is Nothing Then
  186. _dataTable = rel.ChildTable
  187. Else
  188. Throw New ApplicationException("Invalid Data Member")
  189. End If
  190. Else
  191. _dataTable = _dataSet.Tables(Me.DataMember)
  192. End If
  193. End If
  194. End Sub
  195.  
  196. Public Overrides Property Site() As System.ComponentModel.ISite
  197. Get
  198. Return MyBase.Site
  199. End Get
  200. Set(ByVal value As System.ComponentModel.ISite)
  201. 'runs at design time to initiate ContainerControl
  202. MyBase.Site = value
  203. If value Is Nothing Then Return
  204. ' Requests an IDesignerHost service using Component.Site.GetService()
  205. Dim service As IDesignerHost = CType(value.GetService(GetType(IDesignerHost)), IDesignerHost)
  206. If service Is Nothing Then Return
  207. If Not TryCast(service.RootComponent, Form) Is Nothing Then
  208. _form = CType(service.RootComponent, Form)
  209. ElseIf Not TryCast(service.RootComponent, UserControl) Is Nothing Then
  210. _usercontrol = CType(service.RootComponent, UserControl)
  211. End If
  212.  
  213. End Set
  214. End Property
  215.  
  216. Public Function GetFormInstance() As System.Windows.Forms.Form
  217. If _form Is Nothing And Me.CurrencyManager.Bindings.Count > 0 Then
  218. _form = Me.CurrencyManager.Bindings(0).Control.FindForm()
  219.  
  220. End If
  221. Return _form
  222. End Function
  223.  
  224. ''' <summary>
  225. ''' Returns the First Instance of the specified User Control
  226. ''' </summary>
  227. ''' <returns>System.Windows.Forms.Control</returns>
  228. Public Function GetUserControlInstance() As System.Windows.Forms.Control
  229. If _usercontrol Is Nothing And Me.CurrencyManager.Bindings.Count > 0 Then
  230. Dim _uControls() As System.Windows.Forms.Control
  231. _uControls = Me.CurrencyManager.Bindings(0).Control.FindForm.Controls.Find(Me.Site.Name.ToString(), True)
  232. _usercontrol = _uControls(0)
  233.  
  234. End If
  235. Return _usercontrol
  236. End Function
  237.  
  238.  
  239. '============================================================================
  240.  
  241. 'Private Sub _PositionChanged(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PositionChanged
  242.  
  243. ' If IsCurrentDirty Then
  244. ' If AutoSave Then ' IsAutoSavingEvent
  245. ' Try
  246. ' 'cast table as ITableUpdate to get the Update method
  247. ' ' CType(_dataTable, ITableUpdate).Update()
  248. ' Catch ex As Exception
  249. ' ' - needs to raise an event
  250. ' End Try
  251. ' Else
  252. ' Me.CancelEdit()
  253. ' _dataTable.RejectChanges()
  254. ' End If
  255. ' IsCurrentDirty = False
  256. ' End If
  257. 'End Sub
  258.  
  259.  
  260. #End Region
  261.  
  262. End Class `
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement