?
Solved

Adding a custom event to a form.

Posted on 2005-04-13
10
Medium Priority
?
237 Views
Last Modified: 2010-04-23
I am new to creating events in VB.Net.  I had added a property to Form1,  Public Property ValidCustomer as Boolean.  I would like an event to be raised when ever this property is changed.  I am currently using a Checkbox that is not visible and using the checked value property for the value and the checkchanged event to accomplish what I need.  I would however like to replicate this functionality without using a control.  Any assistance would be appreciated.  

0
Comment
Question by:CodeManiac
  • 5
  • 3
  • 2
10 Comments
 
LVL 32

Assisted Solution

by:Erick37
Erick37 earned 1000 total points
ID: 13773229
Whenever the property gets changed do your processing in the Set portion of the Property - do you really need an event?

    Private m_ValidCustomer As Boolean

    Public Property ValidCustomer() As Boolean
        Get
            Return m_ValidCustomer
        End Get
        Set(ByVal Value As Boolean)
            m_ValidCustomer = Value
            '##Do your processing here##
        End Set
    End Property

0
 
LVL 1

Author Comment

by:CodeManiac
ID: 13774083
Yes, I really belive that I need the ValidCustomer_Changed event. I will be setting the value to False quite a bit.

I have a textbox that can recieve its value by differnt means.  First of all I am writing a Pocket PC application.  The textbox is txtCustomerNumber.  This can be populated by Entering Text using the SIP, a barcode reader, pasted or the value can come from Search Form.  I am disabling the Pasteing ability. I have other controls namely buttons that are to be disabled until ValidCustomer is true.  I am using the barcode reader's listchanged event to validate the Customer Number is valid. If Reader.text is a valid customer then txtCustomerNumber.text = reader.text ValidCustomer = true.  If the user uses the SIP to enter in the value then the TextChanged Events sets ValidCustomer to False. When the user clicks the set button the txtCustomerNumber.text is valided.  If txtCustomerNumber.text is a vailid customer then ValidCustomer = True, Buttons enabled.   If the value comes from a search form the the value is already validated so then the txtCustomerNumber.text  is set from the search form.  I can't use the textChanged event to validate the customer number because each time I enter a letter using the SIP this event fires.  Also, If I have customer 10 and also a customer 1000  as soon as I type in 10 it would validate the customer as true which could cause user errors in the end.

The more I think about validating in the Set procedure it may just work.

  Private m_ValidCustomer As Boolean

    Public Property ValidCustomer() As Boolean
        Get
            Return m_ValidCustomer
        End Get
        Set(ByVal Value As Boolean)
            If Value = Not m_VaildCustomer
                  if Value = False
                      'Disable Buttons
                  Else
                      'Enable Buttons
                  End If
            End if
            m_ValidCustomer = Value
         
        End Set
    End Property

However,  what I really would like to know is how to write an custom event procedure as I will need this in future.



0
 
LVL 12

Accepted Solution

by:
S-Twilley earned 1000 total points
ID: 13774186
extending Erick's code...

    Private m_ValidCustomer As Boolean

    Public Property ValidCustomer() As Boolean
        Get
            Return m_ValidCustomer
        End Get
        Set(ByVal Value As Boolean)
            If m_ValidCustomer <> Value Then
                m_ValidCustomer = Value
                RaiseEvent ValidCustomerChanged(Value)
            End If
            '##Do your processing here##
        End Set
    End Property

    Private Event ValidCustomerChanged(ByVal NewValue As Boolean)

    Private Sub ValidCustomerChanged_Handler(ByVal NewValue As Boolean)

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        AddHandler ValidCustomerChanged, AddressOf ValidCustomerChanged_Handler
    End Sub

    Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Try
            RemoveHandler ValidCustomerChanged, AddressOf ValidCustomerChanged_Handler
        Catch ex As Exception

        End Try
    End Sub

==============

Don't give me full credit for this, since I did just extend Erick's code...    im not too sure why you need an event in this case, but since you'll be able to see how it works and then use it for a more valid case later, I've put the code here
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Expert Comment

by:S-Twilley
ID: 13774207
I should have stated... but at the time i thought it obvious...

    Private Sub ValidCustomerChanged_Handler(ByVal NewValue As Boolean)

    End Sub

you put your code for handling the event inside this sub...

and to invoke the event...

RaiseEvent ValidCustomerChanged(Value)   ' where value is the new value for "ValidCustomer"
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13774516
I still don't see the point of raising an event within your own form.  You may as well just call a subroutine from the Property Set method to disable your buttons, etc.  But if you do use the event, please accept the example provided by S-Twilley.
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13774563
As I said... in THIS case i dont see a point of using the event... and my eyes are a bit tired to decipher all those different cases of things chaning O:)  ... .  but I put the code up more as a lesson for events, maybe can use it when you actually need it (although I dont think now is one of those times)
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13774566
and I don't mind sharing the points Erick... I mean, half of my code was yours anyway
0
 
LVL 32

Expert Comment

by:Erick37
ID: 13774592
Well 99% of "My" code was generated automatically by VS.NET!
:)
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13774631
still, you contributed, and i used it in my answer, only fair to split ... that and im trying to make sure people know that they should give credit where it's due....  i've been awarded more credit than i should have sometimes, and then none at all others... swings n roundabouts i know, but anyway. Still think points should be split (assuming the problem is solved)
0
 
LVL 1

Author Comment

by:CodeManiac
ID: 13775263
Thanks to the both of you.  I quess I was making the problem more complicated than it needed to be but I am glad for the Event code both of you provided.  I am learning VB.Net on my own and tend over analyize how something should work.    Using the set property to accomplish what I wanted worked great.  Thanks all for the great input.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…

850 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