Assign values to button

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fanoftvb
    New Member
    • Mar 2007
    • 29

    #1

    Assign values to button

    hi, is it possible to assign values to button? I will not need to enter any values and when i click on the button, the values on the datagrid and database will automatically change. Is there anyway to do it? Thanx in advance.
  • SammyB
    Recognized Expert Contributor
    • Mar 2007
    • 807

    #2
    Originally posted by fanoftvb
    hi, is it possible to assign values to button? I will not need to enter any values and when i click on the button, the values on the datagrid and database will automatically change. Is there anyway to do it? Thanx in advance.
    You can use the Tag property of any control to store anything. You can add the Tag object at design or run time:
    Code:
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click
    	Dim b As Button = sender
    	MsgBox("Hi from " & b.Tag)
    End Sub
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    	Button1.Tag = 19.98
    	Button2.Tag = 42
    End Sub
    Note -- if you are writing in C#, you have to do casting. VB lets you be sloppy. HTH

    Comment

    • fanoftvb
      New Member
      • Mar 2007
      • 29

      #3
      Thanx 4 the reply, I try it out and these errors occur

      'Tag' is not a member of 'System.Web.UI. WebControls.But ton'.
      Name 'Button1' is not declared.

      pls help...thanx

      Comment

      • SammyB
        Recognized Expert Contributor
        • Mar 2007
        • 807

        #4
        Originally posted by fanoftvb
        Thanx 4 the reply, I try it out and these errors occur

        'Tag' is not a member of 'System.Web.UI. WebControls.But ton'.
        Name 'Button1' is not declared.

        pls help...thanx
        Sorry, I was building a Window's App. To keep the Web controls lightweight, they do not have a Tag property, but it is easy to add one by extending the WebControls.But ton class. Here is an example for a TreeNode. The code for an extended Button class would be identical.

        Comment

        • fanoftvb
          New Member
          • Mar 2007
          • 29

          #5
          Hi, can u tell me how to do it, i don't quite understand caz i'm quite new to vb...

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #6
            Originally posted by fanoftvb
            Hi, can u tell me how to do it, i don't quite understand caz i'm quite new to vb...
            I better let an ASP guru do that: I've never created a web form.

            Comment

            • AricC
              Recognized Expert Top Contributor
              • Oct 2006
              • 1885

              #7
              Originally posted by fanoftvb
              hi, is it possible to assign values to button? I will not need to enter any values and when i click on the button, the values on the datagrid and database will automatically change. Is there anyway to do it? Thanx in advance.
              Why would you assign values to a button? Are you trying to change the text dynamically? What have you tried thus far?

              Aric

              Comment

              • fanoftvb
                New Member
                • Mar 2007
                • 29

                #8
                Originally posted by AricC
                Why would you assign values to a button? Are you trying to change the text dynamically? What have you tried thus far?

                Aric

                Hi, i was thinking that if i click on the button the data in my database will change. I will not need to enter any information, the data will click automatically when i click on the button..is there a way to do it?

                Comment

                • SammyB
                  Recognized Expert Contributor
                  • Mar 2007
                  • 807

                  #9
                  Originally posted by fanoftvb
                  Hi, i was thinking that if i click on the button the data in my database will change. I will not need to enter any information, the data will click automatically when i click on the button..is there a way to do it?
                  You coud do something simple like put the number in the button name, Btn1, Btn10, Btn42, etc.
                  Code:
                  	Private Sub Btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn1.Click, Btn10.Click, Btn42.Click
                  		Dim b As Button = sender
                  		Dim s As String = Strings.Right(b.Name, Len(b.Name) - 3)
                  		Dim i As Integer = Integer.Parse(s)
                  		MsgBox("Hi from " & i)
                  	End Sub

                  Comment

                  Working...