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

CommandBarTextBox Validating event

3 Answers 114 Views
This is a migrated thread and some comments may be shown as answers.
Top achievements
Rank 1
Raoul asked on 26 Apr 2012, 12:22 AM
I use an errorprovider on a radform to show input errors to the user. I can use it with a radtextbox when the validating event is triggered like so:
errProvider.SetError(txtBox, errMsg)
errProvider.Icon = Icon.FromHandle(My.Resources.input_error.GetHicon())
errProvider.SetIconAlignment(txtBox, ErrorIconAlignment.MiddleLeft)
Where txtBox is a RadtextBox.

2 problems I encounter when using a CommandBarTextBox:
first: raising the validating event
second: using the SetError method of the errorprovider to link it to a CommandBarTextBox.

I tried adding the validating event in the form_load event:
AddHandler txtPIN.TextBoxElement.TextBoxItem.Validating, txtPIN_validating()
Private Sub txtPIN_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    If HuidigeAccount.Instance.Gebruikers.Count > 0 Then
        FormHelper.RequiredTextBox(txtPIN.TextBoxElement, _errProvider, _errProviderMsg, "Er is een pincode vereist om op te slaan")
    End If
End Sub
But vs2010 throws me a compile error:
Argument not specified for parameter 'sender' of 'Private Sub txtPIN_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)'.
Argument not specified for parameter 'e' of 'Private Sub txtPIN_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs)'.

Not being able to use the errorprovider, a work around using the commandbartextbox border forecolor is an acceptable solution. But not being able to use the  validating event of the CommandBarTextBox, I can't work around that one...

3 Answers, 1 is accepted

Sort by
Richard Slade
Top achievements
Rank 2
answered on 26 Apr 2012, 02:39 PM
Hello Raoul, 

Here is a solution for you to show the error provider in the RadCommandBar and validate the RadCommandBarTextBox as required. 

Designer File
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
    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)
            If disposing AndAlso components IsNot Nothing Then
            End If
        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.components = New System.ComponentModel.Container()
        Me.RadCommandBar1 = New Telerik.WinControls.UI.RadCommandBar()
        Me.CommandBarRowElement1 = New Telerik.WinControls.UI.CommandBarRowElement()
        Me.CommandBarStripElement1 = New Telerik.WinControls.UI.CommandBarStripElement()
        Me.CommandBarTextBox1 = New Telerik.WinControls.UI.CommandBarTextBox()
        Me.RadButton1 = New Telerik.WinControls.UI.RadButton()
        Me.ErrorProvider1 = New System.Windows.Forms.ErrorProvider(Me.components)
        Me.CommandBarHostItem1 = New Telerik.WinControls.UI.CommandBarHostItem()
        CType(Me.RadCommandBar1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).BeginInit()
        CType(Me, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.RadCommandBar1.AutoSize = True
        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(292, 55)
        Me.RadCommandBar1.TabIndex = 0
        Me.RadCommandBar1.Text = "RadCommandBar1"
        Me.CommandBarRowElement1.DisplayName = Nothing
        Me.CommandBarRowElement1.MinSize = New System.Drawing.Size(25, 25)
        Me.CommandBarRowElement1.Strips.AddRange(New Telerik.WinControls.UI.CommandBarStripElement() {Me.CommandBarStripElement1})
        Me.CommandBarStripElement1.DisplayName = "CommandBarStripElement1"
        Me.CommandBarStripElement1.FloatingForm = Nothing
        Me.CommandBarStripElement1.Items.AddRange(New Telerik.WinControls.UI.RadCommandBarBaseItem() {Me.CommandBarTextBox1, Me.CommandBarHostItem1})
        Me.CommandBarStripElement1.Name = "CommandBarStripElement1"
        Me.CommandBarStripElement1.StretchHorizontally = False
        Me.CommandBarStripElement1.Text = ""
        Me.CommandBarTextBox1.AccessibleDescription = "CommandBarTextBox1"
        Me.CommandBarTextBox1.AccessibleName = "CommandBarTextBox1"
        Me.CommandBarTextBox1.DisplayName = "CommandBarTextBox1"
        Me.CommandBarTextBox1.Name = "CommandBarTextBox1"
        Me.CommandBarTextBox1.Text = "CommandBarTextBox1"
        Me.CommandBarTextBox1.Visibility = Telerik.WinControls.ElementVisibility.Visible
        Me.RadButton1.Location = New System.Drawing.Point(200, 48)
        Me.RadButton1.Name = "RadButton1"
        Me.RadButton1.Size = New System.Drawing.Size(81, 24)
        Me.RadButton1.TabIndex = 1
        Me.RadButton1.Text = "Submit"
        Me.ErrorProvider1.ContainerControl = Me
        Me.CommandBarHostItem1.AccessibleDescription = "CommandBarHostItem1"
        Me.CommandBarHostItem1.AccessibleName = "CommandBarHostItem1"
        Me.CommandBarHostItem1.DisplayName = "CommandBarHostItem1"
        Me.CommandBarHostItem1.Name = "CommandBarHostItem1"
        Me.CommandBarHostItem1.Text = "CommandBarHostItem1"
        Me.CommandBarHostItem1.Visibility = Telerik.WinControls.ElementVisibility.Visible
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(292, 84)
        Me.Name = "Form1"
        Me.RootElement.ApplyShapeToControl = True
        Me.Text = "Form1"
        CType(Me.RadCommandBar1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.RadButton1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me.ErrorProvider1, System.ComponentModel.ISupportInitialize).EndInit()
        CType(Me, System.ComponentModel.ISupportInitialize).EndInit()
    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 RadButton1 As Telerik.WinControls.UI.RadButton
    Friend WithEvents ErrorProvider1 As System.Windows.Forms.ErrorProvider
    Friend WithEvents CommandBarTextBox1 As Telerik.WinControls.UI.CommandBarTextBox
    Friend WithEvents CommandBarHostItem1 As Telerik.WinControls.UI.CommandBarHostItem
End Class

Form Code
Imports Telerik.WinControls.UI
Imports Telerik.WinControls
Imports System.ComponentModel
Public Class Form1
    Inherits RadForm
    ''' <summary>
    ''' A windows text box that is hosted inside a CommandBarHostItem
    ''' </summary>
    ''' <remarks>Simply provides an anchor for the ErrorProvider to show</remarks>
    Private m_TextBox As TextBox
    Public Sub New()
    End Sub
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Me.CommandBarStripElement1.StretchHorizontally = True
        ' Host the windows textbox next to the real on
        m_TextBox = New TextBox()
        m_TextBox.Visible = False
        Me.CommandBarHostItem1.HostedControl = m_TextBox
        Me.CommandBarHostItem1.AutoSize = False
        Me.CommandBarHostItem1.Size = New Size(1, Me.CommandBarTextBox1.Size.Height)
        ' set the validation
        AddHandler CommandBarTextBox1.TextBoxElement.TextBoxItem.Validating, AddressOf CommandBarTextBox1_Validating
        AddHandler CommandBarTextBox1.TextBoxElement.TextBoxItem.Validated, AddressOf CommandBarTextBox1_Validated
        Me.CommandBarTextBox1.Text = ""
    End Sub
    ''' <summary>
    ''' If validated, then submit
    ''' </summary>
    Private Sub RadButton1_Click(sender As System.Object, e As System.EventArgs) Handles RadButton1.Click
        If Me.ValidateChildren() Then
        End If
    End Sub
    ''' <summary>
    ''' Validate the text of the CommandBarTextBox but put the errorprovider on the Windows TextBox
    ''' </summary>
    Private Sub CommandBarTextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
        If Me.CommandBarTextBox1.Text.Trim.Length = 0 Then
            Me.ErrorProvider1.SetError(m_TextBox, "Provide a string")
            e.Cancel = True
        End If
    End Sub
    ''' <summary>
    ''' All good. It passes
    ''' </summary>
    Private Sub CommandBarTextBox1_Validated(ByVal sender As Object, ByVal e As EventArgs)
        Me.ErrorProvider1.SetError(m_TextBox, "")
    End Sub
End Class

Hope this helps. Let me know if you have any questions. Remember to mark as answer if this solves your problem
Top achievements
Rank 1
answered on 27 Apr 2012, 09:17 PM

Thanks for the detailed code. It works great.
I never considered the CommandBarHostItemwith a textbox given the RadCommandBarTextBox.
Telerik team
answered on 30 Apr 2012, 11:47 AM
Hello guys,

Thank you for writing.

The ErrorProvider requires a control in order to display the desired error. As you probably know RadTextBox internally contains the standard TextBox. So another solution will be to use the inner TextBox control:
errorProvider1.SetError(commandBarTextBox1.TextBoxElement.TextBoxItem.HostedControl, "Error")

In regards to the validating event, on my end it is successfully fired when RadCommnadBar looses its focus. Alternatively, you can invoke it by calling the ValidateChildren method of the form, just like Richard demonstrates in his snippet.

I hope this helps. Let us know if you have any other questions.
All the best,
the Telerik team
RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Asked by
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Top achievements
Rank 1
Telerik team
Share this question