How do I implement a TextChanging property on a TextBox? - vb.net

I figured there would be a question like this already, but I didn't have any luck searching. I saw a question where someone asked the same thing but the answer was to use the TextChanged event. That's not what I want though.
TextBox has an event for TextChanged that occurs after the Text property has been changed. I need my control to raise an event before the Text property is actually changed to validate data. If it is valid the Text can be changed, if it is not valid the Text does not get changed.
Here's what I tried:
Public Class TextChangingEventArgs
Inherits System.ComponentModel.CancelEventArgs
Private p_sNewValue As String
Public Sub New()
p_sNewValue = String.Empty
End Sub
Public Sub New(sNewValue As String)
p_sNewValue = sNewValue
End Sub
Public Property NewValue As String
Get
Return p_sNewValue
End Get
Set(value As String)
p_sNewValue = value
End Set
End Property
End Class
Public Class BetterTextBox
Inherits TextBox
Public Event TextChanging(sender As Object, e As TextChangingEventArgs)
Public Overrides Property Text As String
Get
Return MyBase.Text
End Get
Set(value As String)
Dim e As New TextChangingEventArgs(value)
RaiseEvent TextChanging(Me, e)
If e.Cancel = False Then
MyBase.Text = value
End If
End Set
End Property
End Class
My in my Form I handle the TextChanging event:
Private Sub BetterTextBox1_TextChanging(sender As System.Object, e As TextChangingEventArgs) Handles BetterTextBox1.TextChanging
e.Cancel = Not Regex.IsMatch(e.NewValue, REGEX_PATTERN)
End Sub
This works for programmatically setting the Text value of the BetterTextBox control, but it does not work when you are typing into the text box.
Does anyone know what I need to do to get this to work?

Since you are already inheriting from TextBox, you can override WndProc and check for paste messages. This should resolve the right-click > Paste problem. You could then handle "regular," typed text in KeyPressed or KeyDown, as others have suggested.
Here's an example of what I'm talking about:
Private Const WM_PASTE As Integer = &H302
Protected Overrides Sub WndProc(ByRef m As Message)
If m.Msg = WM_PASTE AndAlso Clipboard.ContainsText() AndAlso ShouldAllowPaste(Clipboard.GetText()) Then
MyBase.WndProc(m)
ElseIf m.Msg <> WM_PASTE Then
MyBase.WndProc(m)
End If
End Sub

Try this:
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = 8465 Then
If HIWORD(m.WParam) = 1024 Then
' raise your TextChanging event
End If
End If
MyBase.WndProc(m)
End Sub
Public Function HIWORD(ByVal n As Integer) As UInteger
Return CUInt(n >> 16) And CUInt(&HFFFF)
End Function

I would suggest you should try with keydown event. It occurs before textchanged or even before
keypress.

Try the KeyPress event of the Textbox instead.
EDIT : to disable right-click in the Textbox, you can do something like this.
Put this code inside the Textbox's MouseDown event.
If e.Button = MouseButtons.Right Then
MessageBox.Show("can't paste!")
Exit Sub
End If
however I noticed you could still paste text using the key combination Ctrl + V. You can write this code to stop that in the KeyPress event.
If Keys.ControlKey And Keys.V Then
MessageBox.Show("can't paste!")
Exit Sub
End If

Related

Getting Event Handler from Instance of Class

I'm just getting started with custom classes so I wrote a button strip. It inherits a panel and populates it with buttons from strings passed to my .Add().
ButtonStrip1.Add("Button","Texts","Go Here")
I'd like for it to be able to naturally grab ButtonStrip1.Click's handler and pass it on to child buttons and delete it from ButtonStrip1.
I haven't been able to figure that out, so I've been doing
Public Class ButtonStrip
Inherits Panel
Public Property Innermargin As Integer = 5
Dim Offset As Integer = Innermargin
Dim Buttons = New List(Of ButtonStrip_Button)
Dim StoredFN As EventHandler
Public Sub New()
End Sub
Function Add(fn As EventHandler, ParamArray ByVal Values As String())
StoredFN = fn
For Each V In Values
Dim B As New ButtonStrip_Button
Buttons.Add(B)
Me.Controls.Add(B)
B.Text = V
B.Left = Offset + Innermargin
B.Top = Innermargin
Offset = B.Left + B.Width
AddHandler B.Click, fn
Next
RemoveHandler Me.Click, fn
Me.Width = Offset + Innermargin
Me.Height = Buttons(0).height + Innermargin * 2
End Function
Function Add(ParamArray ByVal Values As String())
If StoredFN Is Nothing Then
Throw New Exception("First Add() must supply a function")
End If
Me.Add(StoredFN, Values)
End Function
End Class
Public Class ButtonStrip_Button
Inherits System.Windows.Forms.Button
Public Sub New()
AutoSize = True
AutoSizeMode = AutoSizeMode.GrowAndShrink
End Sub
End Class
which is called by
ButtonStrip1.Add(AddressOf ButtonStrip1_Click,"Button","Texts","Go Here")
What I'd basically like to do is (psuedo-code)
Function Add(fn As EventHandler, ParamArray ByVal Values As String())
If StoredFN is Nothing Then StoredFN = Me.Click
...
AddHandler B.Click, Me.Click
Next
RemoveHandler Me.Click, Me.Click
...
End Function
I've tried changing a few things and googled a lot. I've also tried using CallByName(Me,"Click",CallType.Method) and with CallType.Get, but the error I get is Expression 'Click' is not a procedure, but occurs as the target of a procedure call. It also returns this exact same message for unhandled events, such as ButtonStrip1 has no MouseDown Event.
I've also tried using MyClass.
Not seen here is an alternative .Add() that add StoredFN to B.click
For instance, this click event works with my code
Private Sub ButtonStrip1_Click(sender As Object, e As EventArgs) Handles ButtonStrip1.Click
msgbox("You clicked " & sender.text & ".")
End Sub
What I was suggesting was something like this:
Public Class ButtonStrip
Inherits Panel
Public Event ButtonClick As EventHandler(Of ButtonClickEventArgs)
Protected Overridable Sub OnButtonClick(e As ButtonClickEventArgs)
RaiseEvent ButtonClick(Me, e)
End Sub
Private Sub Buttons_Click(sender As Object, e As EventArgs)
OnButtonClick(New ButtonClickEventArgs(DirectCast(sender, Button)))
End Sub
Public Sub Add(ParamArray values As String())
Dim btn As New Button
AddHandler btn.Click, AddressOf Buttons_Click
'...
End Sub
End Class
Public Class ButtonClickEventArgs
Inherits EventArgs
Public ReadOnly Property Button As Button
Public Sub New(button As Button)
Me.Button = button
End Sub
End Class
Now there's no need to pass event handlers around. When a Button is clicked, the ButtonStrip handles that event and then raises its own ButtonClick event. Neither the ButtonStrip nor the Buttons have to care about any methods that handle that event as it will be handled the same way any other event is. In a form, you'd then handle the ButtonClick event of the ButtonStrip and get the Button that was clicked from e.Button. You could also add an Index property if you wanted to know the position of the Button rather than the Button itself.

vb.net Subclassing ComboBox - trying to create a SelectedIndexChanging event that can be cancelled

I'm currently trying to implement the second response from this thread How can I handle ComboBox selected index changing? in vb (the response that suggests subclassing ComboBox to introduce new SelectedIndexChangingEvent). The event handler
Private Sub MyComboBox1_SelectedIndexChanging(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles MyComboBox1.SelectedIndexChanging
MsgBox("Changing")
End Sub
never gets hit. I'm thinking it has something to do with the way I'm initializing the selectedIndexChanging (lowercase first letter) variable. Any thoughts?
Imports System.ComponentModel
Public Class MyComboBox
Inherits ComboBox
Public Event SelectedIndexChanging as CancelEventHandler
Public LastAcceptedSelectedIndex As Integer
Public Sub New()
LastAcceptedSelectedIndex = -1
End Sub
Protected Sub OnSelectedIndexChanging(e As CancelEventArgs)
Dim selectedIndexChanging as CancelEventHandler = SelectedIndexChanging
If Not SelectedIndexChanging Is Nothing Then
selectedIndexChanging(Me, e)
End If
End Sub
Protected Overrides Sub OnSelectedIndexChanged(e As EventArgs)
If LastAcceptedSelectedIndex <> SelectedIndex Then
Dim cancelEventArgs = New CancelEventArgs
OnSelectedIndexChanging(cancelEventArgs)
If Not cancelEventArgs.Cancel Then
LastAcceptedSelectedIndex = SelectedIndex
MyBase.OnSelectedIndexChanged(e)
Else
SelectedIndex = LastAcceptedSelectedIndex
End If
End If
End Sub
End Class
VB handles event declaration a bit different than C#. The VB RaiseEvent keyword effectively generates code you attempted to translate for the `OnSelectedIndexChanging' method.
The correct VB implementation would be:
Protected Sub OnSelectedIndexChanging(e As CancelEventArgs)
RaiseEvent SelectedIndexChanging(Me, e)
End Sub
You could follow the original pattern, by using the hidden variable VB creates that is the real CancelEventHandler variable. These hidden variables follow the naming pattern of eventNameEvent. So the real CancelEventHandler variable is named: SelectedIndexChangingEvent.
Protected Sub OnSelectedIndexChanging(e As CancelEventArgs)
Dim selectedIndexChanging As CancelEventHandler = Me.SelectedIndexChangingEvent
If Not selectedIndexChanging Is Nothing Then
selectedIndexChanging(Me, e)
End If
End Sub

Hiding up/down buttons on NumericUpDown control

I am trying to subclass NumericUpDown in several ways to get better functionality and appearance.
Since NUD is construct of two controls I would like to hide up/down buttons in cases where property "Increment" is set to 0.
This code is in subclass:
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
Controls(0).Hide()
End Sub
... and it work OK.
But in that function I cannot check a value of Increment property like this:
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
If Me.Increment = 0 Then
Controls(0).Hide()
End if
End Sub
In scope of this function Me is not reachable.
I am also try with using local variables but can't find which event is fired before OnTextBoxResize to read value of Increment property.
What to do in such case to get desired functionality?
This seems to work fairly well. It Shadows the Increment property in order to set the visibility of the spinner controls when the Increment value is being changed. There is an underlying private method the base control calls called PositionControls which you cannot stop — that method could create some flicker, but on my test, it didn't.
Public Class MyNumBox
Inherits NumericUpDown
Shadows Property Increment As Decimal
Get
Return MyBase.Increment
End Get
Set(value As Decimal)
MyBase.Increment = value
OnTextBoxResize(Me, EventArgs.Empty)
End Set
End Property
Protected Overrides Sub OnHandleCreated(e As EventArgs)
MyBase.OnHandleCreated(e)
OnTextBoxResize(Me, EventArgs.Empty)
End Sub
Protected Overrides Sub OnTextBoxResize(source As Object, e As EventArgs)
If Me.IsHandleCreated Then
Me.Height = Me.PreferredHeight
Me.Controls(0).Visible = (MyBase.Increment > 0)
Dim borderWidth As Integer = 0
If Me.BorderStyle > BorderStyle.None Then
borderWidth = SystemInformation.Border3DSize.Width
End If
Dim textWidth As Integer
If Me.Increment = 0 Then
textWidth = Me.ClientSize.Width - (borderWidth * 2)
Else
textWidth = Me.ClientSize.Width - Me.Controls(0).Width - (borderWidth * 2)
End If
If Me.UpDownAlign = LeftRightAlignment.Left Then
If Me.Increment = 0 Then
Me.Controls(1).SetBounds(borderWidth, borderWidth, _
textWidth, Me.Controls(1).Height)
Else
Me.Controls(1).SetBounds(borderWidth + Me.Controls(0).Width, _
Me.Controls(1).Top, textWidth, Me.Controls(1).Height)
End If
Else
Me.Controls(1).SetBounds(borderWidth, Me.Controls(1).Top, _
textWidth, Me.Controls(1).Height)
End If
Me.Refresh()
End If
End Sub
End Class
In the OnTextBoxResize override, I am re-positioning the controls into their proper place, and this version does account for the UpDownAlign property.
If you can, read this thread over at EE where I answered a similar question. It resizes the edit portion so that the control is redrawn correctly when the buttons have been hidden and the control is resized. *Otherwise the portion of the control where the buttons used to be leaves ghosts behind.
One solution to your specific problem is to wait for the VisibleChanged() event and check the Increment() property from there. Here is a conversion of my previous answer with some minor changes:
Public Class NoArrowNumericUpDown
Inherits NumericUpDown
Private itb As InnerTextBox = Nothing
Protected Overrides Sub OnVisibleChanged(e As System.EventArgs)
If Me.Visible Then
If Me.Increment = 0 AndAlso IsNothing(itb) Then
Dim ctl As Control = Me.Controls(0) ' get the spinners
Me.Controls.Remove(ctl) ' remove the spinners
ctl = Me.Controls(0) ' get the edit control
itb = New InnerTextBox(Me, ctl)
End If
End If
MyBase.OnVisibleChanged(e)
End Sub
Public Class InnerTextBox
Inherits NativeWindow
Private parentControl As Control = Nothing
Const WM_WINDOWPOSCHANGING As Integer = &H46
Public Sub New(parentControl As Control, InnerTextBox As Control)
Me.parentControl = parentControl
Me.AssignHandle(InnerTextBox.Handle)
End Sub
Protected Overrides Sub WndProc(ByRef m As Message)
Select Case m.Msg
Case WM_WINDOWPOSCHANGING
Dim wp As WindowPos = CType(System.Runtime.InteropServices.Marshal.PtrToStructure(m.LParam, GetType(WindowPos)), WindowPos)
If Me.parentControl IsNot Nothing Then
wp.cx = Me.parentControl.ClientSize.Width - 2 * wp.x
wp.cy = Me.parentControl.ClientSize.Height
System.Runtime.InteropServices.Marshal.StructureToPtr(wp, m.LParam, True)
End If
Exit Select
End Select
MyBase.WndProc(m)
End Sub
Public Structure WindowPos
Public hwnd As IntPtr
Public hwndInsertAfter As IntPtr
Public x As Integer
Public y As Integer
Public cx As Integer
Public cy As Integer
Public flags As UInteger
End Structure
End Class
End Class
EDIT: You could just enclose your code in a Try/Catch block?
Public Class NoArrowNumericUpDown
Inherits NumericUpDown
Protected Overrides Sub OnTextBoxResize(ByVal source As Object, ByVal e As System.EventArgs)
Try
If Me.Increment = 0 Then
Controls(0).Hide()
End If
Catch ex As Exception
End Try
End Sub
End Class

AddressOf with parameter

One way or another I need to link groupID (and one other integer) to the button I am dynamically adding.. any ideas?
What I can do;
AddHandler mybutton.Click, AddressOf PrintMessage
Private Sub PrintMessage(ByVal sender As System.Object, ByVal e As System.EventArgs)
MessageBox.Show("Dynamic event happened!")
End Sub
What I can't do, but want to;
AddHandler mybutton.Click, AddressOf PrintMessage(groupID)
Private Sub PrintMessage(ByVal groupID as Integer)
MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub
There is no way to do this with AddressOf itself. What you're looking for is a lambda expression.
AddHandler myButton.Click, Function(sender, e) PrintMessage(groupId)
Private Sub PrintMessage(ByVal groupID as Integer)
MessageBox.Show("Dynamic event happened!" & groupID .tostring)
End Sub
You can create your own button class and add anything you want to it
Public Class MyButton
Inherits Button
Private _groupID As Integer
Public Property GroupID() As Integer
Get
Return _groupID
End Get
Set(ByVal value As Integer)
_groupID = value
End Set
End Property
Private _anotherInteger As Integer
Public Property AnotherInteger() As Integer
Get
Return _anotherInteger
End Get
Set(ByVal value As Integer)
_anotherInteger = value
End Set
End Property
End Class
Since VB 2010 you can simply write
Public Class MyButton
Inherits Button
Public Property GroupID As Integer
Public Property AnotherInteger As Integer
End Class
You can access the button by casting the sender
Private Sub PrintMessage(ByVal sender As Object, ByVal e As EventArgs)
Dim btn = DirectCast(sender, MyButton)
MessageBox.Show( _
String.Format("GroupID = {0}, AnotherInteger = {1}", _
btn.GroupID, btn.AnotherInteger))
End Sub
These new properties can even be set in the properties window (under Misc).
The controls defined in the current project automatically appear in the toolbox.
Use the Tag property of the button.
Button1.Tag = someObject
AddressOf gets the address of a method, and thus you cannot pass parameters to it.
You can use delegate which very clear for your code follow as:
Define a delegate
Public Delegate Sub ControlClickDelegate(ByVal sender As Object, ByVal e As EventArgs)
Custom button class
Public Class CustomButton
Inherits System.Windows.Forms.Button
#Region "property delegate"
Private controlClickDelegate As ControlClickDelegate
Public Property ClickHandlerDelegate As ControlClickDelegate
Get
Return controlClickDelegate
End Get
Set(ByVal Value As ControlClickDelegate)
controlClickDelegate = Value
End Set
End Property
#End Region
Public Sub RegisterEventHandler()
AddHandler Me.Click, AddressOf OnClicking
End Sub
Private Sub OnClicking(ByVal sender As Object, e As System.EventArgs)
If (Me.controlClickDelegate IsNot Nothing) Then
Me.controlClickDelegate(sender, e)
End If
End Sub
End Class
MainForm
Public Class MainForm
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.CusButton1.ClickHandlerDelegate = AddressOf Me.btnClick
Me.CusButton1.RegisterEventHandler()
End Sub
Private Sub btnClick(ByVal sender As Object, ByVal e As EventArgs)
Me.TextBox1.Text = "Hello world"
End Sub
End Class
The below worked for me:
Dim bStart = New Button With {.Text = "START"}
AddHandler bStart.Click, Function(sender, e) TriggerProcess(any Long value)
Private Function TriggerProcess(ByVal paramName As Long) As Boolean
' any processing logic
Return True
End Function
My solution:
AddHandler menuItemYear.Items(i).MouseUp, Sub() menu_year(2019)
Private Sub menu_year(ByVal intYear As Integer)
'do something
End Sub
There are few ways to do that depending of the complexity and number of parameters required.
1. Use Tag for adding a complex structure
2. Inherit the the Button class and add the values as class members then populate them before using it. That gives you a lot more flexibility.
If you are using web version
3. You cannot add it to Tag, but for simple values assign it to index use .Attributes.Add("name"). This gets added to the HTML tags and not the Server side. You can then use the index to access a server side structure for complex systems.
4. Use sessions to store values and store the session reference to Name attribute as described above (#3).
No problem ;-)
For example:
Private ComboActionsOnValueChanged As New Dictionary(Of ComboBox, EventHandler)
'somewhere in function
dim del = Sub(theSender, eventArgs)
MsgBox(CType(theSender, ComboBox).Name & " test")
End Sub
ComboActionsOnValueChanged.Add(myCombo, del)
'somewhere else
Dim delTest = ComboActionsOnValueChanged(myCombo)
RemoveHandler myCombo.SelectedValueChanged, delTest
myCombo.DataSource = someDataSource
AddHandler myCombo.SelectedValueChanged, delTest
as we expect, event won't fire after DataSource change in this place

Avoid duplicated event handlers?

How do I avoid an event from being handled twice (if is the same handler?)
Module Module1
Sub Main()
Dim item As New Item
AddHandler item.TitleChanged, AddressOf Item_TitleChanged
AddHandler item.TitleChanged, AddressOf Item_TitleChanged
item.Title = "asdf"
Stop
End Sub
Private Sub Item_TitleChanged(sender As Object, e As EventArgs)
Console.WriteLine("Title changed!")
End Sub
End Module
Public Class Item
Private m_Title As String
Public Property Title() As String
Get
Return m_Title
End Get
Set(ByVal value As String)
m_Title = value
RaiseEvent TitleChanged(Me, EventArgs.Empty)
End Set
End Property
Public Event TitleChanged As EventHandler
End Class
Output:
Title changed!
Title changed!
Desired output:
Title changed!
I want the event manager to detect that this event is already handled by this handler and so it shouldn't rehandle (or readd) it.
You can also just always call RemoveHandler before AddHandler. I have found this practical in specific scenarios.
Wrapping the event handler list in a HashSet will make sure the handlers are not duplicate references, the following snippet replacement for the question's Item class will work under the above sample (it won't re-add the handler if it's already in the HashSet):
Public Class Item
Private m_Title As String
Public Property Title() As String
Get
Return m_Title
End Get
Set(ByVal value As String)
m_Title = value
RaiseEvent TitleChanged(Me, EventArgs.Empty)
End Set
End Property
Private handlers As New HashSet(Of EventHandler)
Public Custom Event TitleChanged As EventHandler
AddHandler(value As EventHandler)
handlers.Add(value)
End AddHandler
RemoveHandler(value As EventHandler)
handlers.Remove(value)
End RemoveHandler
RaiseEvent(sender As Object, e As System.EventArgs)
For Each handler In handlers.Where(Function(h) h IsNot Nothing)
handler(sender, e)
Next
End RaiseEvent
End Event
End Class