Command Array Click Event in VB 2008

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • supun24
    New Member
    • Aug 2006
    • 11

    Command Array Click Event in VB 2008

    Hi All, I have created Command Array like follows.
    Code:
    Public Class Form1
        Dim NewButton(25) As Button
        
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim loc As Integer
            loc = 50
            For i = 1 To 25
                NewButton(i) = New Button()
                NewButton(i).Text = "Hello " & i
                Me.Controls.Add(NewButton(i))
                NewButton(i).Location = New Point(10, loc)
                NewButton(i).Name = "NewButton" & i
                loc = loc + 20
                       Next
    
        End Sub
    So.. I want to get each and every command button name when I press One of command button.

    Ex:
    If I press NewButton8 then I need msgbox with it's name, text (All of command button properties if possible). Button Name is very important

    Thank You very much,
    Regards,
    Supun Silva
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    So what is your question exactly?
    How do you make a message box?
    How do you get the name of the button you clicked?
    You seem to already have a handle on .Name property since you are assigning when you make the button. Just read it.

    Comment

    • supun24
      New Member
      • Aug 2006
      • 11

      #3
      Re::

      Please sorry........ I don't know what is the code I should write to get Button Name in msgbox and where put it???
      Please help me...

      Regards,
      Supun Silva

      Comment

      • tlhintoq
        Recognized Expert Specialist
        • Mar 2008
        • 3532

        #4
        Take the .Name property of the button and put it into the .Text property of the MessageBox


        Oh - wait - When you are making your buttons you are not assigning any handler to them. So they do nothing when you click them.

        Make a handler to receive the .Click event of the buttons.

        In your loop assign that handler to every button.
        NewButton(i).Cl ick += new handler (intellisense will help you out here)
        All the buttons go tot he same handler so you will need to get the

        ((button)sender ).Name property

        Comment

        • supun24
          New Member
          • Aug 2006
          • 11

          #5
          Didn't get you

          Hi... Please could you kindly send me the coding including my code. I didn't understand your short code. Please forgive me for that..

          Regards,
          Supun Silva

          Comment

          • tlhintoq
            Recognized Expert Specialist
            • Mar 2008
            • 3532

            #6
            Have you ever created a button programmaticall y and assigned a handler to it?

            Comment

            • supun24
              New Member
              • Aug 2006
              • 11

              #7
              Hi..

              No.. I didn't do the program to create button dynamically

              Comment

              • tlhintoq
                Recognized Expert Specialist
                • Mar 2008
                • 3532

                #8
                May I suggest that you start a little smaller then?
                Instead of trying to create an entire array of buttons programatically start with just one.

                Learn how to make one control (button) programmaticall y. Assign event handlers. Get properties etc.

                Then when you are comfortable with how to do one, THEN try it with bunch of them.

                We all have to learn to walk before we can run.

                Comment

                • supun24
                  New Member
                  • Aug 2006
                  • 11

                  #9
                  hii

                  Hi.. Thank you buddy!!! Cool.. I really apologize about my knowledge. Could you kindly tell me how to create button in run-time(Dynamicall y) and add handler in vb 2008.

                  Thank you,
                  Supun Silva

                  Comment

                  • tlhintoq
                    Recognized Expert Specialist
                    • Mar 2008
                    • 3532

                    #10
                    The MSDN is a great resource to start with for such things. I'm more a C# guy than VB - but it is still a .NET language so most everything is the same; only different.

                    MSDN on creating controls programmaticall y

                    MSDN for the button control (adding event handler for click)

                    Comment

                    Working...