Use variable as Control ID in .Net Application

E.g. You have a a lot of checkbox, which you have used similar name for it. E.g. (CheckBox1, CheckBox2, CheckBox3).
Last week my friend has asked if it is possible to convert a string to an Object. My first though is yes. It is quite simple and straight forward.
Instead of using
string t = “”;
What you need to do is to assign a string as:
String t = “value”;
Actually, that is not what he wants. HaHa. What he want to do is convert a string which is retrieved from a DataTable into a TextBox. Because my brain was on Holiday last few days. I think it is not simple to convert a string into an Obejct (Control). And also, I do not think you can do Array of Object like what you can do in VB6. E.g. You can Name a object with a bracket. Cannot remember it is [] or (). E.g. CheckBoxSample()..etc.
My brain seems to work again today. I have worked out the solution to deal with it.
In this example I am going to use DataTable, which has 2 columns “ControlID” and “CheckBoxValue”
I’ve Add a GroupBox named “gb_Container”, and create 10 CheckBoxes.
That all use the default name. E.g. CheckBox1 to CheckBox10.
Also I have create a Button to trigger the event.
The solution seems quite simple.
1) If you have read my previous post. You already know I prefer to use GroupBox or Panel as a container to group the controls together. It makes it easier to manage (From my point of view)
2) Drag and name the controls into the Group Box. (Make sure the name of the Control is the same as the one on the Database or Array.)
3) After you retreive the value (Control ID) variable which stored on the DataTable.
What you need to do is to convert it to string.
4) Then you can create a CheckBox variable, and use the Control Properties from the Group Box to retrieve the Control from the variable. After that you assign this control into the CheckBox Variable that you just created.
// CheckBox cb = (CheckBox) gb_Container.Control[“”]
5) To ensure the control does exist. You may want to use an If statement to check if it is null.
6) If it is not null, which mean it has assigned the control to the variable. Then you can just do whatever you like to do. E.g.
cb.Checked = (bool) tbl.Rows[RowID][CheckedValue]
Here it is the sample code in VB.NET
‘Step 1 retrive DataTable
For i As Integer = 0 To tbl.Rows.Count – 1
‘Assign valur from DataTable field into string
Dim cbControlID As String = tbl.Rows(i)(“ControlID”)
‘Assign the control to check box
Dim cbObject As CheckBox = DirectCast(groupBox1.Controls(cbControlID), CheckBox)
‘Check if the checkBox exist from the groupBox1.Controls Statement
If cbObject IsNot Nothing Then
‘Assign the value into the CheckBox
cbObject.Checked = CBool(tbl.Rows(i)(“CheckBoxValue”))
End If
Next
Here it is the sample code in C#
//Step 1 retrive DataTable
for (int i = 0; i<tbl.Rows.Count; i++){
//Assign valur from DataTable field into string
string cbControlID = tbl.Rows[i][“ControlID”];
//Assign the control to check box
CheckBox cbObject = (CheckBox) groupBox1.Controls[cbControlID ];
//Check if the checkBox exist from the groupBox1.Controls Statement
if (cbObject != null)
{
//Assign the value into the CheckBox
cbObject.Checked = (bool) tbl.Rows[i][“CheckBoxValue”];
}
}
I knew the wording of this tricks and tips is crap, Hope you can still understand and find this post useful.

Leave a Reply