How to move textbox events to other file - VB6 to VB.NET? - vb.net

I am reworking old application built in VB6, with VB.NET and Visual Studio 2005.
It has one large form with like 50-60 text boxes, each with 2-3 event handlers, click, validating, keydown, enter etc...
The person who built this application didn't pay attention to keep good structure, so now the code file for the form is almost 2900 lines of code.
It has 3-4 important functions, other functions are just event handlers.
The problem is, each of those event handlers are defined as private and refer to objects/textboxes which exist only in the form code file.
What would be the right approach to move those event handlers to separate files?
Here is a sample code which I failed to move to separate file since I am noob VB.NET programmer:
Imports System.Windows.Forms
Public Class Pol
Private cboPol As MaskedTextBox = New MaskedTextBox()
Private Sub cboPol_Enter(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) 'Handles cboPol.Enter
' TODO ova beshe zakomentirano. Da se proveri
'meGodrag.SelStart = 0
' meGodrag.SelLength = 4
'grstar = meGodrag.Text
End Sub
Private Sub cboPol_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs) 'Handles cboPol.KeyDown
Dim KeyCode As Short = eventArgs.KeyCode
Dim Shift As Short = eventArgs.KeyData \ &H10000
If KeyCode = Constants.LEFT_ARROW_KEY Then
'meNasmes.Focus()
End If
End Sub
Private Sub cboPol_KeyPress(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyPressEventArgs) 'Handles cboPol.KeyPress
Dim KeyAscii As Short = Asc(eventArgs.KeyChar)
'If KeyAscii = Constants.ENTER_KEY Then
System.Windows.Forms.SendKeys.Send(Constants.TAB_KEY)
'End If
eventArgs.KeyChar = Chr(KeyAscii)
If KeyAscii = 0 Then
eventArgs.Handled = True
End If
End Sub
End Class

The simplest approach to divide your mega-class in small parts is through the use of the concept of Partial class
Just create a new class file and name it as your big form class but add the keyword Partial before both classes. Now you can move all your event handlers to the second file freeing your main form from this stuff
See MSDN on How to split a class into partial classes
On a more structural solution you can check if it is possibile to create an unique event handler for the same event and force your controls to use the same event handler.
For example, if the action on the KeyDown event is the same for all controls then you can write a single event handler and tell your controls to use it
Private Sub allControls_KeyDown(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.KeyEventArgs)
Handles cboPol.KeyDown, cboXXXX.KeyDown, cboYYYY.KeyDown etc.....
Or, if you find this approach too 'verbose', just use the Winforms designer to select the same event handler for all controls

If the sample code you posted is indicative, it is possible a lot of the code is just for navigating the form.
You might be able to remove most of it by setting your tab order correctly.
For special navigation keys (e.g. left arrow key does something special, or you want the ENTER key to move to the next control like the TAB key), try setting Form.KeyPreview = True and having form-wide KeyUp/KeyDown/KeyPress routines.

Related

event detection between different controls vb

I have a class A which contains 2 user controls declared as
Friend WithEvents CustInfo1 As WindowsApplication1.CustInfo
Friend WithEvents ServiceLocation1 As WindowsApplication1.ServiceLocation
Both have textBoxes. If value of textBoxA in custInfo1 changes then how can I make value of textBoxB in SeviceLocation1 also change
I will be most thankful if anyone can help me.
Thanks
You need to do the following:
Inside the CustInfo user control, you need to write code inside the textBoxA Changed event that raises an event from the CustInfo user control (e.g. TextBoxChanged event). RaiseEvent statement
Inside the ServiceLocation user control, create a public property getter and setter for whatever your textBoxB.Text is
On the form that contains both user controls, create code in the new CustInfo TextBoxChanged event and set the new property on the ServiceLocation1 user control.
All controls (also custom controls) have the property Controls, through which you can access the (sub) controls of that control. Now you can retrieve your textbox by calling the .Item(key) method on it. Then you can assign a event handler to it in your form or class.
Dim key As String = "textBoxA" 'Or simply the name of that TextBox in your CustInfo
Dim textboxA As TextBox = CustInfo1.Controls.Item(key)
AddHandler textBoxA.TextChanged, AddressOf mytextchangedhandler
Where that mytextchangedhandler handles the TextChanged event for that TextBox.
Personally I don't like this method too much, as you are relying on knowing either the name of the TextBox or the index in the Controls list of your usercontrol.
I would definitely go for the option to create your own event on your usercontrol. It is quite easy to do even! Below how to do it. In the code behind of your usercontrol, you'll have to add an event declaration:
Event MyTextBoxChanged(sender As Object, e As EventArgs)
Now we'll have to raise it, we do this by implementing the TextChanged event of the TextBoxA in your usercontrol (as you explained you wanted to do):
Private Sub TextBoxA_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxA.TextChanged
RaiseEvent MyTextBoxChanged(Me, EventArgs.Empty)
End Sub
Now we can simply implement this event (MyTextBoxChanged) in your Form as follows:
Private Sub CustInfo1_MyTextBoxChanged(sender As System.Object, e As System.EventArgs) Handles CustInfo1.MyTextBoxChanged
' Do something
End Sub
Obviously we still need to get the updated text, now we can create our own EventArgs that will give us the new (and/or old value) as you will want to have. We simply can inherit the System.EventArgs class and add some properties (for example a property OldText that holds the old text value and a property NewText that holds the new text value):
Public Class MyEventArgs
Inherits System.EventArgs
Private _OldText As String
Public ReadOnly Property OldText() As String
Get
Return _OldText
End Get
End Property
Private _NewText As String
Public ReadOnly Property NewText() As String
Get
Return _NewText
End Get
End Property
Public Sub New(oldText As String, newText As String)
_OldText = oldText
_NewText = newText
End Sub
End Class
Now we have to change the event definition and raising to use the MyEventArgs:
Event MyTextBoxChanged(sender As Object, e As MyEventArgs)
Private Sub TextBoxA_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBoxA.TextChanged
RaiseEvent MyTextBoxChanged(Me, New MyEventArgs(TextBoxA.Text))
End Sub
And also change the implementation in your Form:
Private Sub CustInfo1_MyTextBoxChanged(sender As System.Object, e As MyEventArgs) Handles CustInfo1.MyTextBoxChanged
MessageBox.Show(e.Text)
End Sub
More information about events can be found on our favorite spot MSDN.

Running event x from event y

(Newbie VB.NET question)
Here's the specific code behind my simple winforms that I don't fully understand:
Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click
'do something
End Sub
Private Sub MainForm_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Enter
okButton_Click(Me, e) '<=== argumanets must be wrong
End Sub
What I'm trying to achieve:
If the user hits Enter when they have the winforms active then I'd like the Click event handler of the okButton to fire.
Obviously from the above my understanding of the arguments I need to supply to the event called okButton_Click is lacking; what are the correct arguments and why?
I think you might use the AcceptButton property of the form. Just set it to the desired button and it should do the trick.
Note that there is also a CancelButton property.
Answering your event-question:
The sender argument marks the sender of the event. Mostly, this is the Me instance of the class. In my opinion, Me seems to be absolutely correct.
The e argument contains the EventArgs of the specific event. If you're not using this argument in your function body, the content of this variable doesn't matter. You could use Nothing or just route the EventArgs (that's what you've done).
Refering to your comment:
EventArgs is a base class for event-specific data. For example, if you're subscribing to a mouse event, ewill be a MouseEventArgs. The MouseEventArg class offers you the mouse buttons that have been pressed and the coordinates of the pointer when the event was fired.
In your case, the events only have EventArgs which provide only basic information about the event. There does not seem to be special information about it.
Note: If you want to combine multiple events into one callback, you can make e of type EventArgs because every e should inherit from EventArgs following the Microsoft guidelines. Therefore, you can combine a Button-Click with a Mouse-Move into one callback because the signature of the delegates match.
A nicer way than just passing Nothing to the target Sub, is to combine two callbacks into one. You can do this in VB.NET using multiple Handles like this:
Private Sub SomeSub(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles okButton.Click, MyBase.Enter
'this is getting called on a okButton.Click and MyBase.Enter
End Sub
(Scroll to the right to see the Handles)
Note that you don't need a second Sub which calls the first one. Everything is in one Sub.
Try this instead:
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
If (keyData And Keys.KeyCode) = Keys.Enter Then
okButton.PerformClick()
Return True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
You might need to exclude some controls in the check if you want to use enter key with other controls, ie:
If (keyData And Keys.KeyCode) = Keys.Enter AndAlso Not Textbox1.Focused Then

How does one override the OnShown Event of a form when creating a form programmatically?

I am trying to get a sound to play when a form is first shown (rather like a standard message box does for want of a better example). If using a standard form added through the designer I would generally do this by overriding the standard onshown event and then go on to call MyBase.OnShown(e)
The problem I've hit now is that the form is being created programmatically (Dim myForm as new Form etc) and as such I seem not to be able to use AddHandler to override this event. I don't doubt that I'm doing this in entirely the wrong way, but I'd appreciate any advice that can be offered. I would prefer advice from the perspective of VB.net, but I can just about muddle through in C#.
Form.OnShown is not an event. Rather, it is a method of the Form class which raises the form's Shown event. Here's the MSDN article that explains the OnShown method:
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.onshown.aspx
When you are making a derived class by using the form designer, you can override the OnShown method, but when you are simply accessing a form through its public interface, you need to use the Shown event instead. You can add an event handler for that event like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f As Form1 = New Form1()
AddHandler f.Shown, AddressOf f_Shown
f.Show()
End Sub
Private Sub f_Shown(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Since the form doesn't exist in code, you would actually have to call the event then.
Try writing out the showing code first:
Public Sub form_Showing(ByVal sender As Object, e As EventArgs)
// play sound
End Sub
then when you create your form, you add the handler of the event:
Dim f As New Form
AddHandler f.Shown, AddressOf form_Showing
f.Show()

Select the content of textbox when it receives focus

I have found a similar question to mine in Making a WinForms TextBox behave like your browser's address bar
Now i am trying to modify or make it some more different by making it general. I want to apply same action to all the textboxes in form without write code for each one... how many i dun know. As soon as i add a textbox in my form it should behave with similar action of selecting.
So wondering how to do it?
The following code inherits from TextBox and implements the code you mentioned in Making a WinForms TextBox behave like your browser's address bar.
Once you've added the MyTextBox class to your project you can do a global search for System.Windows.Forms.Text and replace with MyTextBox.
The advantage of using this class is you can't forget to wire all the events for every textbox. Also if you decide on another tweak for all textboxes you have one place to add the feature.
Imports System
Imports System.Windows.Forms
Public Class MyTextBox
Inherits TextBox
Private alreadyFocused As Boolean
Protected Overrides Sub OnLeave(ByVal e As EventArgs)
MyBase.OnLeave(e)
Me.alreadyFocused = False
End Sub
Protected Overrides Sub OnGotFocus(ByVal e As EventArgs)
MyBase.OnGotFocus(e)
' Select all text only if the mouse isn't down.
' This makes tabbing to the textbox give focus.
If MouseButtons = MouseButtons.None Then
Me.SelectAll()
Me.alreadyFocused = True
End If
End Sub
Protected Overrides Sub OnMouseUp(ByVal mevent As MouseEventArgs)
MyBase.OnMouseUp(mevent)
' Web browsers like Google Chrome select the text on mouse up.
' They only do it if the textbox isn't already focused,
' and if the user hasn't selected all text.
If Not Me.alreadyFocused AndAlso Me.SelectionLength = 0 Then
Me.alreadyFocused = True
Me.SelectAll()
End If
End Sub
End Class
Assuming you're going to use the accepted solution from the question you link to, all you'd need to do would be that whenever you create a new textbox, you use AddHandler to add the same 3 eventhandlers to each new textbox.
Then you need to change the event handlers to instead of referencing the textbox as this.textBox1 they'll reference it as CType(sender, TextBox) which means that they'll use the textbox that generated the event.
Edit: I'll add that line of code here as well since it's easier to read then
Private Sub TextBox_GotFocus (ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus, TextBox2.GotFocus, TextBox3.GotFocus
We use this custom textbox control:
Public Class TextBoxX
Inherits System.Windows.Forms.TextBox
Private Sub TextBoxX_MouseUp(sender As Object, e As MouseEventArgs) Handles Me.MouseUp
SelectAll()
End Sub
end class
You can see the full project of our TextBox (on steroids) on GitHub https://github.com/logico-dev/TextBoxX

vb.net winform 2008 datagrid doubleclick event not firing

i have a databound datagrid in vb.net 2008. This is a program where i use the double click event multipal times.. but for some reason on a new form it's not working anymore. In code behind i selected the datagrid and double click event. when i double click on teh grid in debug mode, it never fires off the event(i put a breakpoint in). I've tried several other events and none of them are firing.
Here is what the event look like
Private Sub dgWho2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgWho2.DoubleClick
End Sub
and here is what it looks like for one that is working on another form
Private Sub dgQueryStats_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles dgQueryStats.DoubleClick
For Each O As asr.QueryStat In Os
If O.RowID = CInt(Me.dgQueryStats.Tag.ToString) Then
Me.lblQuery.Text = O.Text
End If
Next
Me.cmdCopyQuery.Visible = True
End Sub
in comparing the properties of the two datagrid, other than the name and the binding, they look the same as well. wondering if anyone has an idea.
thanks
shannon
Err... found my problem.. i had a datagrid.enabled=false stuck down in some code...
Sorry about that
shannon