Solved Textbox Command Button

Posted on 2004-11-24
Last Modified: 2012-08-14
Windows form
I hve a windows form with 2 TextBoxes and 2 Cancel buttons. As a textboxes value can be cancelled .

I populate the data from the dataset (well i dont bind it as its not ncesssary)
in both the textboxes.  User can click cancel to cancel a value in textbox  where I update the database saying that line has been cancelled.
How would we know that if cancel button pressed corresponds to textbox1 or textbox2

Textbox1              cancelButton
Textbox2              cancelButton
Textbox3              cancelButton

Can a simple example be shown ? thanks in advance
Question by:sainavya1215
    LVL 19

    Accepted Solution

    You surely will have different names for the three Cancel Buttons
    Say ButtonCancel_1, ButtonCancel_2 and so on

    Solution 1:

    Handle the Click events of the Cancel Buttons separately

    so ButtonCancel_1 Click means textBox1 value was cancelled
     ButtonCancel_2 Click means textBox2 value was cancelled

    Solution 2:

    use a common Event handler for Click of all such cancel Buttons
    Then use

    CType(sender, Button).Name to identify the name of the Cancel Button that was clicked


    Author Comment

    yep....Undestood solution 1 ..........could it be more explained on solution 2.... Thanks for that
    LVL 14

    Expert Comment

    When an event handler is fired, it is passed an Object called "sender". This is the object that triggered the event.

    If you know what type of object it is, you can use the CTYPE function to cast the generic Object into the object that triggered the event.

    Solution 2 provided by arif_egbal simply says this:

    Private Sub ButtonClicks(sender As Object, e as EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        Select Case CType(sender, Button).Name     ' This determines WHAT button the user clicked.
            Case "Button1"

            Case "Button2"

            Case "Button3"

    End Sub

    This technique allows you to reduce code by using the same event handler to handle multiple events from objects on your form.

    Hope that helps.


    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Article by: Kraeven
    Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
    Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
    It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    754 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now