This is a migrated thread and some comments may be shown as answers.

CausesValidation between Main Form and Child Form

2 Answers 103 Views
Form
This is a migrated thread and some comments may be shown as answers.
Emily Fong
Top achievements
Rank 1
Emily Fong asked on 23 Aug 2011, 04:38 AM
Hi,

I have a problem on the CausesValidation between Main Form and Child Form. 
In the Main Form (midChildContainer form), I have a commandbar control with 2 buttons.  When those buttons are clicked, a function in Child Form will be involved. 

However, I want the validating event in the child form to be triggered when the first button in Main Form is clicked.  When, the other button is clicked, the validating event should not be fired.

I tried to set the causesvalidation in CommandBar to False.  Then, whatever buttons is clicked, the validating event never fire. 
Is there any way to do?  Thanks!

The following is the code:

Main Form Designer:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class FrmMainMenu
    Inherits Telerik.WinControls.UI.RadForm
 
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.RadCommandBar1 = New Telerik.WinControls.UI.RadCommandBar()
        Me.CommandBarRowElement1 = New Telerik.WinControls.UI.CommandBarRowElement()
        Me.CommandBarStripElement1 = New Telerik.WinControls.UI.CommandBarStripElement()
        Me.cmdButton = New Telerik.WinControls.UI.CommandBarButton()
        Me.cmdButton2 = New Telerik.WinControls.UI.CommandBarButton()
        CType(Me.RadCommandBar1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'RadCommandBar1
        '
        Me.RadCommandBar1.AutoSize = True
        Me.RadCommandBar1.CausesValidation = False
        Me.RadCommandBar1.Dock = System.Windows.Forms.DockStyle.Top
        Me.RadCommandBar1.Location = New System.Drawing.Point(0, 0)
        Me.RadCommandBar1.Name = "RadCommandBar1"
        Me.RadCommandBar1.Rows.AddRange(New Telerik.WinControls.UI.CommandBarRowElement() {Me.CommandBarRowElement1})
        Me.RadCommandBar1.Size = New System.Drawing.Size(579, 55)
        Me.RadCommandBar1.TabIndex = 1
        Me.RadCommandBar1.Text = "Without Validation"
        '
        'CommandBarRowElement1
        '
        Me.CommandBarRowElement1.DisplayName = Nothing
        Me.CommandBarRowElement1.MinSize = New System.Drawing.Size(25, 25)
        Me.CommandBarRowElement1.Strips.AddRange(New Telerik.WinControls.UI.CommandBarStripElement() {Me.CommandBarStripElement1})
        '
        'CommandBarStripElement1
        '
        Me.CommandBarStripElement1.DisplayName = "CommandBarStripElement1"
        Me.CommandBarStripElement1.FloatingForm = Nothing
        Me.CommandBarStripElement1.Items.AddRange(New Telerik.WinControls.UI.RadCommandBarBaseItem() {Me.cmdButton, Me.cmdButton2})
        Me.CommandBarStripElement1.Name = "CommandBarStripElement1"
        Me.CommandBarStripElement1.Text = ""
        '
        'cmdButton
        '
        Me.cmdButton.AccessibleDescription = "CommandBarButton1"
        Me.cmdButton.AccessibleName = "CommandBarButton1"
        Me.cmdButton.CanFocus = False
        Me.cmdButton.DisplayName = "Without Validation"
        Me.cmdButton.DrawText = True
        Me.cmdButton.Image = Nothing
        Me.cmdButton.ImageAlignment = System.Drawing.ContentAlignment.BottomRight
        Me.cmdButton.KeyTip = "WITHOUT VALIDATION"
        Me.cmdButton.Name = "cmdButton"
        Me.cmdButton.Text = "Without Validation"
        Me.cmdButton.TextImageRelation = System.Windows.Forms.TextImageRelation.TextAboveImage
        Me.cmdButton.Visibility = Telerik.WinControls.ElementVisibility.Visible
        Me.cmdButton.VisibleInOverflowMenu = True
        '
        'cmdButton2
        '
        Me.cmdButton2.AccessibleDescription = "With Validation"
        Me.cmdButton2.AccessibleName = "With Validation"
        Me.cmdButton2.CanFocus = False
        Me.cmdButton2.DisplayName = "CommandBarButton1"
        Me.cmdButton2.DrawText = True
        Me.cmdButton2.Image = Nothing
        Me.cmdButton2.Name = "cmdButton2"
        Me.cmdButton2.Text = "With Validation"
        Me.cmdButton2.Visibility = Telerik.WinControls.ElementVisibility.Visible
        Me.cmdButton2.VisibleInOverflowMenu = True
        '
        'FrmMainMenu
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(579, 579)
        Me.Controls.Add(Me.RadCommandBar1)
        Me.IsMdiContainer = True
        Me.Name = "FrmMainMenu"
        '
        '
        '
        Me.RootElement.ApplyShapeToControl = True
        Me.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen
        Me.Text = "FrmMainMenu"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized
        CType(Me.RadCommandBar1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
 
    End Sub
    Friend WithEvents RadCommandBar1 As Telerik.WinControls.UI.RadCommandBar
    Friend WithEvents CommandBarRowElement1 As Telerik.WinControls.UI.CommandBarRowElement
    Friend WithEvents CommandBarStripElement1 As Telerik.WinControls.UI.CommandBarStripElement
    Friend WithEvents cmdButton As Telerik.WinControls.UI.CommandBarButton
    Friend WithEvents cmdButton2 As Telerik.WinControls.UI.CommandBarButton
End Class

Main Form Code:
Public Class FrmMainMenu
    Private frmChild As FrmChild
    Private currentForm As Object
 
    Private Sub FrmMainMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        frmChild = New FrmChild
        frmChild.Top = 0
        frmChild.Left = 0
        frmChild.MdiParent = Me
        frmChild.Show()
    End Sub
 
    Private Sub cmdButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdButton.Click
        currentForm = Me.ActiveMdiChild
        currentForm.WithoutValidationClick()
    End Sub
 
    Private Sub cmdButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdButton2.Click
        currentForm = Me.ActiveMdiChild
        currentForm.WithValidationClick()
    End Sub
 
    Private Sub cmdButton2_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdButton2.MouseHover
        RadCommandBar1.CausesValidation = True
    End Sub
 
    Private Sub cmdButton2_MouseLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdButton2.MouseLeave
        RadCommandBar1.CausesValidation = False
    End Sub
End Class

Child Form Designer: 
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class FrmChild
    Inherits Telerik.WinControls.UI.RadForm
 
    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub
 
    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer
 
    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer. 
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.RadTextBox1 = New Telerik.WinControls.UI.RadTextBox()
        Me.RadLabel1 = New Telerik.WinControls.UI.RadLabel()
        CType(Me.RadTextBox1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.RadLabel1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'RadTextBox1
        '
        Me.RadTextBox1.Location = New System.Drawing.Point(13, 36)
        Me.RadTextBox1.Name = "RadTextBox1"
        Me.RadTextBox1.Size = New System.Drawing.Size(198, 20)
        Me.RadTextBox1.TabIndex = 0
        Me.RadTextBox1.TabStop = False
        Me.RadTextBox1.Text = "123"
        '
        'RadLabel1
        '
        Me.RadLabel1.Location = New System.Drawing.Point(13, 12)
        Me.RadLabel1.Name = "RadLabel1"
        Me.RadLabel1.Size = New System.Drawing.Size(153, 18)
        Me.RadLabel1.TabIndex = 1
        Me.RadLabel1.Text = "Only ABC is an validate input."
        '
        'FrmChild
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.CausesValidation = False
        Me.ClientSize = New System.Drawing.Size(293, 82)
        Me.Controls.Add(Me.RadTextBox1)
        Me.Controls.Add(Me.RadLabel1)
        Me.Name = "FrmChild"
        '
        '
        '
        Me.RootElement.ApplyShapeToControl = True
        Me.StartPosition = System.Windows.Forms.FormStartPosition.Manual
        Me.Text = "FrmChild"
        CType(Me.RadTextBox1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.RadLabel1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)
        Me.PerformLayout()
 
    End Sub
    Friend WithEvents RadTextBox1 As Telerik.WinControls.UI.RadTextBox
    Friend WithEvents RadLabel1 As Telerik.WinControls.UI.RadLabel
End Class

Child Form Code:
Public Class FrmChild
    Private oParent As Object
 
    Public Sub WithoutValidationClick()
        MessageBox.Show("Without Validation Click from Main Menu.")
    End Sub
 
    Public Sub WithValidationClick()
        MessageBox.Show("With Validation Click from Main Menu.")
    End Sub
 
    Private Sub RadTextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles RadTextBox1.Validating
        If RadTextBox1.Text <> "ABC" Then
            MessageBox.Show("Invalid input.")
        End If
    End Sub
 
    Private Sub FrmChild_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        oParent = Me.MdiParent
    End Sub
End Class

Emily

2 Answers, 1 is accepted

Sort by
0
Ivan Todorov
Telerik team
answered on 25 Aug 2011, 09:19 AM
Hi Emily Fong,

Thank you for writing.

The Validating event fires only under specific circumstances which are described in this MSDN help article. Furthermore, it can be suppressed by setting the CausesValidation property of the same control. In addition to this, the MouseHover event occurs when the mouse pointer rests on the element and in some cases it will fire after the command bar button is clicked. If you need to use events to set the CausesValidation property, I would suggest MouseEnter / MouseLeave / MouseMove events, as they fire more frequently. I have made some modifications to your FrmMainMenu.vb file and I am posting it below:

Public Class FrmMainMenu
    Private frmChild As FrmChild
    Private currentForm As Object
  
    Private Sub FrmMainMenu_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        frmChild = New FrmChild
        frmChild.Top = 0
        frmChild.Left = 0
        frmChild.MdiParent = Me
        frmChild.Show()
    End Sub
  
    Private Sub cmdButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdButton.Click
        frmChild.RadTextBox1.CausesValidation = False
        currentForm = Me.ActiveMdiChild
        frmChild.RadTextBox1.Focus()
        frmChild.SelectNextControl(frmChild.RadTextBox1,true,false,false,true)
        currentForm.WithoutValidationClick()
    End Sub
  
    Private Sub cmdButton2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdButton2.Click
        frmChild.RadTextBox1.CausesValidation = True
        currentForm = Me.ActiveMdiChild
        frmChild.RadTextBox1.Focus()
        frmChild.SelectNextControl(frmChild.RadTextBox1,true,false,false,true)
        currentForm.WithValidationClick()
    End Sub
  
End Class

I hope you find this useful. Do not hesitate to write back if you have any additional questions.

Kind regards,
Ivan Todorov
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

0
Emily Fong
Top achievements
Rank 1
answered on 25 Aug 2011, 02:51 PM
Thanks!
Tags
Form
Asked by
Emily Fong
Top achievements
Rank 1
Answers by
Ivan Todorov
Telerik team
Emily Fong
Top achievements
Rank 1
Share this question
or