Question Change button properties on buttons created in run time

Ole

New member
Joined
Apr 11, 2013
Messages
2
Programming Experience
Beginner
Hi.
I'm working on a program where a number of buttons is being created based on information in a database. Later in my code I will like to change background color on some of the buttons to show their status. I would normally use Button1.BackColor = Color.Red, but I don't know how to call the buttons when they are created during run time. Like you can see in below code, the first button will get the name "Knap1". How do I change this buttons color to red?

        Try
            con = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ole\Documents\Visual Studio 2012\Projects\TMS\TMS-database.accdb")
            con.Open()
        Catch ex As Exception
            MsgBox("Database ikke fundet. Fejl 1")
        End Try
        sqlStr = "Select * from Medarbejdere"
        cmd = New OleDb.OleDbCommand(sqlStr, con)
        dr = cmd.ExecuteReader()
        Dim Y As Integer = 1
        Do While dr.Read()
            Dim btn = New Button()
            btn.Name = "Knap" & Y
            btn.Text = dr(3)
            btn.Location = New Point(10, Y * 30)
            Panel1.Controls.Add(btn)
            AddHandler btn.Click, AddressOf Me.ButtonClicked
            Y = Y + 1
        Loop
        con.Close()


I hope somebody know how.

Thank you in advance.

/ Ole
 
Last edited by a moderator:
Hi,

To do this, you need to access the contents of the Controls Collection of the Parent Container and then Cast that object back to the correct Type to access its Full Range of methods and properties. i.e:-

VB.NET:
Dim SelectedButton As Button = DirectCast(Panel1.Controls("myDynamicButton"), Button)
SelectedButton.BackColor = Color.Red

Hope that helps.

Cheers,

Ian

BTW, please use Code Tags on the Advanced menu if you want to post code in the future.
 
Hi,

To do this, you need to access the contents of the Controls Collection of the Parent Container and then Cast that object back to the correct Type to access its Full Range of methods and properties. i.e:-

VB.NET:
Dim SelectedButton As Button = DirectCast(Panel1.Controls("myDynamicButton"), Button)
SelectedButton.BackColor = Color.Red

Hope that helps.

Cheers,

Ian

BTW, please use Code Tags on the Advanced menu if you want to post code in the future.

What you say is correct but it's worth noting that what you get back from the Controls collection is a Control reference. If you only need access to members of the Control class, e.g. Text, Location and BackColor, then no cast is required.
 
By the way, you should NEVER connect directly to the data file in your source folder. That is supposed to be your single source of truth so you don't want to pollute it with test data. You should configure the file to be copied to the output folder whenever it changes and then connect to that copy. You should use |DataDirectory| as the folder path in your connection string so that it will work while testing and after release without change. For more info on managing local data files, follow the first link in my signature.
 
Hi jmcilhinney,

What you say is correct but it's worth noting that what you get back from the Controls collection is a Control reference. If you only need access to members of the Control class, e.g. Text, Location and BackColor, then no cast is required.

Thanks for the clarification, which I was aware of, but you are right I should have made that point clear.

Kind regards,

Ian
 
Thank you for your help IanRyder and jmcilhinney. I appreciate it.

It works fine now.

I have managed to make it work with cast, and also without, like below:

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click 'Test-Button
Dim SelectedButton As Button = Panel1.Controls("Knap1")
SelectedButton.BackColor = Color.Red
End Sub


Kind regards

Ole
 
Ole said:
I have managed to make it work with cast, and also without, like below:
You should turn on Option Strict. That code does an implicit cast from type Control (that is what the ControlCollection.Item property returns) and type Button (that is what you have declared your variable as).

You should also get used to using type inference (that is Option Infer actually) and write code like this:
Dim SelectedButton = Panel1.Controls("Knap1")
SelectedButton.BackColor = Color.Red

Compiler will here infer the type of SelectedButton variable from the assigned expression (which is type Control).

In this actual code you should not use a variable at all, there is no need for it, this is equivalent:
Panel1.Controls("Knap1").BackColor = Color.Red

While a variable can make an expression more readable by putting a label on it, including the use of that variable in later code, less code usually means less code clutter which by itself makes things easier to read.
 
Back
Top