Solved

VB.Net 2005: click handlers

Posted on 2007-03-29
3
300 Views
Last Modified: 2013-11-26
I have two click event handler Subs in a vb.net 2005 desktop application that are exactly the same across many forms, except that they are fired by the Click events of different controls on each form, and they set the text value of a textbox that is differently named in each form.

I didn't want to use a Module.vb file, since that is not object-oriented (right?). Otherwise though, is there any way I can bring these Subs into an external place like a header file that I can include in all the forms I need them in? Right now I am cutting and pasting what seems to be a lot of code and it seems to me I should be able to abstract them into something reusable. but nothing comes to me when I try to figure out how.

'EXAMPLE
'The UpdateScore subroutine below sets a textbox value as the result of checkboxes being checked/unchecked.
'It is exactly the same in 30 different forms in this app, except for
'       1) the multiple click events that fire it: cbExpCfgDmg.Click, etc.
'and 2) the textbox whose value it sets: txtCWMCfg.Text
'-------------------------------------------------------------------
    Friend Sub UpdateScore(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbExpCfgDmg.Click, _
cbMixUXO.Click, cbExpCfgUndmg.Click, cbBulk.Click, cbK941.Click, cbCAIS.Click, cbEvCWM.Click
        If sender.checked = True Then
            If Convert.ToInt32(sender.text) > Convert.ToInt32(txtCWMCfg.Text) Then
                txtCWMCfg.Text = sender.text
            End If
        ElseIf sender.checked = False Then
            Dim cb As CheckBox
            Dim arrCbValues As New ArrayList
            For Each ctrl As Control In gb1.Controls
                If TypeOf ctrl Is CheckBox Then
                    cb = CType(ctrl, CheckBox)
                    If cb.Checked = True Then
                        arrCbValues.Add(Convert.ToInt32(cb.Text))
                    End If
                End If
            Next ctrl
            'find checked boxes
            If arrCbValues.Count > 0 Then
                'if max value in checked boxes is greater than textbox value, replace it
                If FindMax(arrCbValues) >= 0 Then
                    txtCWMCfg.Text = FindMax(arrCbValues)
                End If
            Else
                txtCWMCfg.Text = "0"
            End If
        End If
    End Sub
'-----------------------------------------------------
'END EXAMPLE
0
Comment
Question by:k3n51mm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 24

Expert Comment

by:DBAduck - Ben Miller
ID: 18821513
I think it would be much simpler to create a Class Library with a class that could be used to do the actions and then in each form that needs the click, you just handle the click and pass the Textbox to the Class library method and it does the changes.  Then you have consistency and can modify the class library (or object) and then each application that you used it in is updated without changing each Click handler.
0
 
LVL 1

Author Comment

by:k3n51mm
ID: 18826387
thanks for the response. How would I do that?
0
 
LVL 24

Accepted Solution

by:
DBAduck - Ben Miller earned 500 total points
ID: 18826800
In Visual Studio you would create a Class Library project and create a class maybe called something like GlobalClickHandler and in it put the method either as a Public Shared Function or Sub and have parameters of a TextBox that gets the value of it Set and then a parameter of the Sender (maybe it is a Button or whatever it is).

Something like:
Public Shared Function HandleClick(ByVal pButton as System.Web...Button, ByRef pTextbox as System.Web....TextBox)
   ' In this function you would do the same thing that you do but now you pass in the button and the Textbox and you set the value of the textbox, etc.

End Function

Now you have the Class GlobalClickHandler with a method of HandleClick( )

Inside your project that will use this, you would have a reference to it and the Import it with an Imports statement.

Now in the Project, your Buttons will have a Click handler and in it you will reference it this way:

GlobalClickHandler.HandleClick(button, ByRef txtCWMCfg) ' or however you call it or whatever the name is, but this would have the same code but would use and set the values of the parameter objects.

Make sense?
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

732 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question