Yevrah Aradnop
Yevrah Aradnop

Reputation: 21

Adding dynamic Button and changing its Property text. VB.NET

I have this problem by changing the text of added dynamically button. I want to change the button text from "Select" to "Update".

this is my code.

dim oButton as button

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

Dim dr As SqlDataReader
    Dim cn As SqlConnection = New SqlConnection("Data Source=server;Initial Catalog=testDB;Integrated Security=True;MultipleActiveResultSets=true;")
    cmd.Connection = cn
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "votecand"
    Dim stream As New MemoryStream()
    cn.Open()
    dr = cmd.ExecuteReader
    If dr.HasRows = True Then
       while dr.read
          oButton = New Button
          oButton.Enabled = True
          oButton.Font = New Font("Tahoma", 11, FontStyle.Regular)
          oButton.Location = New Point(ButtonNumber * 30, ButtonNumber * 30)
          oButton.Name = "MyButton" & ButtonNumber.ToString
          oButton.Size = New Size(134, 34)
          oButton.BackColor = Color.MediumSeaGreen
          oButton.FlatStyle = FlatStyle.Flat
          oButton.ForeColor = Color.White
          oButton.Text = "Select"
          oButton.Visible = True
          oButton.Dock = DockStyle.Bottom
          oButton.Tag = ButtonNumber
          AddHandler oButton.Click, AddressOf onButtonClick
          Me.Controls.Add(oButton)
       End while
    End if
End sub

this code is MyFunc Function

Private Sub MyFunc(ByVal ButtonNumber As Integer)

    Dim cn As SqlConnection = New SqlConnection("Data Source=server;Initial Catalog=testDB;Integrated Security=True;MultipleActiveResultSets=true;")
    Dim command As New SqlCommand

    command = New SqlCommand("select first_name,middle_name,last_name,position,gender,course,year_level,aff,student_id from tbl_cand where id=@id", cn)
    command.Parameters.AddWithValue("@id", SqlDbType.VarChar).Value = ButtonNumber.ToString

    Dim dt As New DataTable()
    Dim adapater As New SqlDataAdapter(command)
    adapater.Fill(dt)

    Dim fname As String = dt.Rows(0).ItemArray(0).ToString
    Dim mname As String = dt.Rows(0).ItemArray(1).ToString
    Dim lname As String = dt.Rows(0).ItemArray(2).ToString
    Dim gender As String = dt.Rows(0).ItemArray(3).ToString
    Dim course As String = dt.Rows(0).ItemArray(4).ToString
    Dim year As String = dt.Rows(0).ItemArray(5).ToString
    Dim aff As String = dt.Rows(0).ItemArray(6).ToString
    Dim studid As String = dt.Rows(0).ItemArray(7).ToString

    lblfname.Text = fname
    lblmname.Text = mname
    lbllname.Text = lname
    lblgender.Text = gender
    lblyear.Text = year
    lblmajor.Text = course
    lblaff.Text = aff

    connect()
    sql = "INSERT INTO tbl_castvote(can_id,fname,img,student_id)"
    sql = sql + "VALUES(@canid,@fname,@pos,@img,@voterid,@stdid)"
    cmd = New SqlCommand(sql, con)
    With cmd
        .Parameters.AddWithValue("canid", studid)
        .Parameters.AddWithValue("fname", fname)

        Dim ms As New MemoryStream()
        pb.Image.Save(ms, pb.Image.RawFormat)
        Dim data As Byte() = ms.GetBuffer()
        Dim p As New SqlParameter("@img", SqlDbType.VarBinary)
        p.Value = data
        .Parameters.Add(p)

        .Parameters.AddWithValue("stdid", studentid.Text)
    End With

    If MsgBox("You have selected" & " '" & fname & " " & mname & " " & lname & "' " & "as" & " '" & pos & "' ", vbOKCancel + vbInformation, "Voting System") = vbOK Then
        cmd.ExecuteNonQuery()
        oButton.Text = "Update" --> this line is my problem. Can't change the oButton.text "Select" to "Update"
    End If

End Sub

and this is my button click handler

Private Sub onButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim ButtonNumber As Integer
    If CType(sender, Button).Tag IsNot Nothing Then
        If Integer.TryParse(CType(sender, Button).Tag.ToString, ButtonNumber) Then
            MyFunc(ButtonNumber)
        End If
    End If
End Sub

pls help me. thank you

Upvotes: 0

Views: 1854

Answers (2)

Trevor_G
Trevor_G

Reputation: 1331

You are using one reference for multiple buttons. You have two options from the above code.

1. Save your added button references in a list

Private Buttons As List(of Button)

Before you start your loop

Buttons = New List(of Button)

in your loop

   Dim oButton = New Button
   ButtonNumber = Buttons.Count '<-- use the collection count as the number
   ...blah blah blah...
   Buttons.Add(oButton)

Then in myFunc simply use

Buttons(ButtonNumber).Text = "whatever"

2. If this is the only place you reference it, pass the sender along instead.

Private Sub MyFunc(Byref Pressed_Button as Button, ByVal ButtonNumber As Integer)
         ...blah blah blah...
         Pressed_Button.Text = "whatever"
End Sub

Private Sub onButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim PressedButton as Button = CType(sender, Button)
    Dim ButtonNumber As Integer
    If PressedButton.Tag IsNot Nothing Then
        If Integer.TryParse(PressedButton.Tag.ToString, ButtonNumber) Then
            MyFunc(PressedButton, ButtonNumber)
        End If
    End If
End Sub

There are also ways to search for the control by name, but keeping your own index is more efficient.

Upvotes: 0

LarsTech
LarsTech

Reputation: 81610

A couple of issues. You need to increment your ButtonNumber value inside your loop:

while dr.read
  ButtonNumber += 1
  oButton = New Button
  oButton.Name = "MyButton" & ButtonNumber.ToString

Then in your function, you have to find your button to update it:

cmd.ExecuteNonQuery()
Dim btn As Button = Me.Controls.OfType(Of Button).
                    Where(Function(x) x.Name = "myButton" & btnNumber.ToString).
                    FirstOrDefault()
If btn IsNot Nothing Then
  btn.Text = "Update"
End If

Change your function parameter to be unique:

Private Sub MyFunc(btnNumber As Integer)

Upvotes: 1

Related Questions