2016-04-08 29 views
0

我們使用vb.net/dev快速工具。我們有幾個控件文本框,組合等...而不是檢查每個值更改我們想要通過所有控件,並檢查是否有任何已編輯,然後保存表單關閉。以下是我嘗試完成此操作的一些代碼。問題是,儘管它在技術上工作...它使用遞歸AddDirtyEvent(c),所以當我去關閉窗體並單擊是保存..由於多個控件它多次調用該消息框...如果我採取它不會工作,並檢測到骯髒的變化。我只是想知道我怎麼能得到這個工作,我想或者如果有更簡單的方法的方式......檢查是否有任何控件已更改並在表格關閉時保存數據

Dim is_Dirty As Boolean = False 

    Private Sub AddDirtyEvent(ByVal ctrl As Control) 

    For Each c As Control In ctrl.Controls 
     If TypeOf c Is TextEdit Then 
      Dim tb As TextEdit = CType(c, TextEdit) 
      AddHandler tb.EditValueChanged, AddressOf SetIsDirty 

     End If 
     'If TypeOf c Is ComboBoxEdit Then 
     ' Dim cb As ComboBoxEdit = CType(c, ComboBoxEdit) 
     ' AddHandler cb.SelectedIndexChanged, AddressOf SetIsDirty 

     'End If 
     If c.Controls.Count > 0 Then 
      AddDirtyEvent(c) 
     End If 

    Next 

End Sub 

Private Sub SetIsDirty(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    is_Dirty = True 
End Sub 

Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing 

    If is_Dirty = True Then 
     Dim dr As DialogResult = MessageBox.Show("Do you want save changes before leaving?", "Closing Well Info", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) 
     If dr = Windows.Forms.DialogResult.Yes Then 
      SimpleButtonSave.PerformClick() 
      Me.Close() 
     End If 
    End If 
End Sub 

回答

0

代碼的處理事件將髒標誌工程確定的部分。

您的MessageBox出現多次,因爲您在FormClosing事件處理程序中調用Me.Close。調用Close會再次觸發FormClosing事件 - 遞歸。只要刪除Me.Close;表格已經關閉。

+2

天狼星,如果(當)該解決您的問題,一定要點擊勾選推動這一關未答覆的名單。不是沒有,但你有許多問題,沒有被接受的答案。 – Plutonix

+0

好的謝謝..還有一個問題。在devexpress中,我們有一個gridcontrol和一個gridview。 gridiview是在gridcontrol內,我試圖找到窗體上的控件..找不到gridview,但我可以找到gridcontrol。但一旦我找到網格控制,我需要改變,如果gridview中的任何東西改變......任何想法 – Sirus

0

作爲一名前MS訪問程序員,我也非常渴望爲vb.net開發一個表單髒屬性的實現,我發現它對用戶更友好(我的用戶習慣了它)來檢測控件的更改在窗體上或留下髒表單並要求確認,而不是經典的編輯和保存按鈕。 我以前在文本庫,組合框,列表框等創建自定義控件...因爲我喜歡控制有焦點有不同的背景和放大文本等能力...

因此,我添加到他們(文本框代碼):

> Protected Overrides Sub OnModifiedChanged(e As EventArgs) 
>  MyBase.OnModifiedChanged(e) 
>  If _DirtyEnabled and Me.Modified Then FormDirty(Me) = Me.Modified 
> End Sub 
> Private _DirtyEnabled As Boolean = False 
> <Category("Misc"), Description("When Enabled triggers Dirty event for form"), Browsable(True)> _ 
> Public Property DirtyEnabled As Boolean 
>  Get 
>   Return _DirtyEnabled 
>  End Get 
>  Set(value As Boolean) 
>   DirtyEnabled = value 
>  End Set 
> End Property 

這是一個文本框,組合框爲列表框和我用SelectIndexChanged事件。 如果DirtyEnabled屬性設置爲True,則任何更改都會觸發FormDirty屬性的調用。 然後圖書館的公共模塊:

> #Region "Dirty" 
> Private Structure FormInfo ' used in DirtyForm dictionary to keep a list of dirty forms 
>  Dim Name As String 
>  Dim Time As Date 
>  Dim Ctrl As String 
> End Structure 
> Private DirtyForms As New Dictionary(Of IntPtr, FormInfo)  ' key = form handle as the form could be opened more then once, value FormInfo 
> 
>  Public Property FormDirty(frm As Form) As Boolean 
>  Get 
>   If DirtyForms.Count > 0 Then 
>    Return DirtyForms.ContainsKey(frm.Handle) 
>   Else 
>    Return False 
>   End If 
>  End Get 
>  Set(IsDirty As Boolean) 
>   EditDirtyForms(frm, IsDirty) 
>  End Set 
> End Property 
> 
> Public Property FormDirty(Ctrl As Control) As Boolean 
>  Get 
>   If DirtyForms.Count > 0 Then 
>     Return DirtyForms.ContainsKey(Ctrl.FindForm.Handle) 
>    Else 
>     Return False 
>    End If 
>  End Get 
>  Set(IsDirty As Boolean) 
>   EditDirtyForms(Ctrl.FindForm, IsDirty, Ctrl.Name) 
>  End Set 
> End Property 
> 
> Private Sub EditDirtyForms(frm As Form, IsDirty As Boolean, Optional CtrlName As String = Nothing) 
>  If IsDirty Then 
>   If DirtyForms.Count = 0 OrElse Not DirtyForms.ContainsKey(frm.Handle) Then 
>    Dim Info As New FormInfo With {.Name = frm.Name, .Time = Now, .Ctrl = CtrlName} 
>    DirtyForms.Add(frm.Handle, Info) 
>   End If 
>  ElseIf DirtyForms.Count > 0 Then 
>   If DirtyForms.ContainsKey(frm.Handle) Then DirtyForms.Remove(frm.Handle) 
>  End If 
> End Sub 
> 
> Public Function DirtyFormList() As String 
>  Dim p As New FormInfo, s As String = String.Empty 
>  If DirtyForms.Count > 0 Then 
>   For Each f As KeyValuePair(Of IntPtr, FormInfo) In DirtyForms 
>    s &= f.Value.Name & cSpace & If(f.Value.Ctrl, String.Empty) & ": " & f.Value.Time & vbNewLine 
>   Next 
>  End If 
>  Return s 
> End Function 
> 
> Public Function DirtyFormCount() As Integer 
>  Return DirtyForms.Count 
> End Function 
> #End Region 

表單保存會打電話Formdirty(我)= FALSE 它仍然處於測試階段,還惦記要求確認從用戶等.. 它的工作原理,但因爲我在.net編程方面很新穎,所以任何評論或批評都是非常受歡迎的。

RGDS

相關問題