Question: Is there a way to more efficiently or more readably write a loop that creates multiple types of controls?
Background
I've been trying to learn C# by converting some of my older VB.Net programs over. This is a method I use after the main form loads to populate a TableLayoutPanel with controls. Eventually looking something similar to this:
Is there a way to more efficiently or more readably write this kind of method? It really feels like there are more repeated lines here than truly necessary.
I apologize for the messy C# code, I'm about a week into playing with it.
C# Code
public static List<object> _labels = new List<object>();
public static List<object> _entries = new List<object>();
public static TableLayoutPanel Form_Grid = new TableLayoutPanel();
private void wl_getFormat()
{
try
{
MySqlConnection MySql = new MySqlConnection("Server=localhost;Database=" + Session.Department + ";Uid=" + Session.Username + ";Pwd=Password2016;");
MySql.Open();
MySqlCommand MySqlCmd = new MySqlCommand("SELECT * FROM wl_format", MySql);
MySqlDataReader Reader = MySqlCmd.ExecuteReader();
int i = 0;
int Height = 255;
while (Reader.Read())
{
string Name = Reader.GetString("Name"); //Get field name
string Type = Reader.GetString("Type"); //Get field type
Label L = new Label();
if (Reader.GetInt32("Required") == 1) { L.Text = Name + "*"; } else { L.Text = Name; }
L.Name = "L" + i;
L.AutoSize = true;
L.TextAlign = ContentAlignment.MiddleLeft;
L.Visible = true;
L.Font = new Font("Microsoft Sans Serif", 8);
L.Dock = DockStyle.Fill;
L.Margin = new Padding(0, 3, 0, 3);
if (!_labels.Contains(L))
{
_labels.Add(L);
L.MouseUp += new MouseEventHandler(wl_Lock);
}
Form_Grid.Controls.Add(L, 0, i + 1);
Form_Grid.RowStyles[i + 1].SizeType = SizeType.AutoSize;
if (Type == "Textbox" || Type == "Suggestbox")
{
TextBox T = new TextBox();
Height += 26;
T.Name = "T" + i;
T.Dock = DockStyle.Fill;
T.TabIndex = i + 1;
if (Type == "Suggestbox")
{
T.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
T.AutoCompleteSource = AutoCompleteSource.CustomSource;
string[] p = _options[i].Split('|');
foreach (string a in p)
{
T.AutoCompleteCustomSource.Add(a);
}
}
if (!_entries.Contains(T))
{
_entries.Add(T);
T.KeyPress += new KeyPressEventHandler(wl_hotKeys);
}
Form_Grid.Controls.Add(T, 1, i + 1);
Form_Grid.SetColumnSpan(T, 3);
}
else if (Type == "Dropbox")
{
ComboBox C = new ComboBox();
Height += 26;
C.Name = "C" + i;
C.Dock = DockStyle.Fill;
C.TabIndex = i + 1;
C.AutoCompleteMode = AutoCompleteMode.Suggest;
C.AutoCompleteSource = AutoCompleteSource.ListItems;
C.FormattingEnabled = true;
C.Sorted = true;
string[] p = _options[i].Split('|');
foreach (string a in p)
{
C.Items.Add(a);
}
if (!_entries.Contains(C))
{
_entries.Add(C);
C.SelectedValueChanged += new EventHandler(wl_Ties);
}
Form_Grid.Controls.Add(C, 1, i + 1);
Form_Grid.SetColumnSpan(C, 3);
}
else if (Type == "Numberbox")
{
NumericUpDown N = new NumericUpDown();
Height += 26;
N.Name = "N" + i;
N.Dock = DockStyle.Fill;
N.TabIndex = i + 1;
N.BorderStyle = BorderStyle.FixedSingle;
if (!_entries.Contains(N))
{
_entries.Add(N);
}
Form_Grid.Controls.Add(N, 1, i + 1);
Form_Grid.SetColumnSpan(N, 3);
}
else
{
Label E = new Label();
Height += 26;
E.Name = "E" + i;
E.Text = "No type information found";
E.AutoSize = true;
E.TextAlign = ContentAlignment.MiddleLeft;
E.Visible = true;
E.Font = new Font("Microsoft Sans Serif", 8);
E.Dock = DockStyle.Fill;
E.Margin = new Padding(0, 3, 0, 3);
}
Form_Grid.RowStyles[i + 1].SizeType = SizeType.AutoSize;
i++;
}
this.Size = new Size(280, Height);
MySql.Close();
}
catch (Exception ex) { Notify.Errors(ex.HResult, ex.ToString(), ex.Message); }
}
VB.Net Code
Public Labels, Entries As New List(Of Object)
Public Form_Grid As New TableLayoutPanel
Private Sub Worklog_GetFormat()
Try
Dim MySql As MySqlConnection = New MySqlConnection("Server=10.5.1.97;Database=" & Session.Department & ";Uid=" & Session.Username & ";Pwd=PocketUser2015;")
MySql.Open()
Dim MySqlCmd As MySqlCommand = New MySqlCommand("SELECT * FROM wl_format", MySql)
Dim Reader As MySqlDataReader = MySqlCmd.ExecuteReader
Dim i As Integer = 0
Dim LogHeight As Integer = 255
Do While Reader.Read
Dim Name As String = Reader.GetString("Name")
Dim Type As String = Reader.GetString("Type")
Dim L As New Label
With L
If CBool(Reader.GetInt32("Required")) Then .Text = Name & "*" Else .Text = Name
.Name = "L" & i
.AutoSize = True
.TextAlign = ContentAlignment.MiddleLeft
.Visible = True
.Font = New Font("Microsoft Sans Serif", 8.25)
.Dock = DockStyle.Fill
.Margin = New Padding(0, 3, 0, 3)
If Not Labels.Contains(L) Then
Labels.Add(L)
AddHandler .MouseUp, AddressOf Labels_MouseUp
End If
End With
Form_Grid.Controls.Add(L, 0, i + 1)
Form_Grid.RowStyles(i + 1).SizeType = SizeType.AutoSize
Select Case True
Case Type = "Textbox" Or Type = "Suggestbox"
Dim T As New TextBox
LogHeight += 26
With T
.Name = "T" & i
.Visible = True
.Dock = DockStyle.Fill
.TabIndex = i + 1
If Type = "Suggestbox" Then
.AutoCompleteMode = AutoCompleteMode.SuggestAppend
.AutoCompleteSource = AutoCompleteSource.CustomSource
Dim p = Split(Options(i), "|")
For o = 0 To p.Count - 1
T.AutoCompleteCustomSource.Add(p(o))
Next
End If
If Not Entries.Contains(T) Then
Entries.Add(T)
AddHandler .KeyPress, AddressOf Entries_KeyPress
End If
End With
Form_Grid.Controls.Add(T, 1, i + 1)
Form_Grid.SetColumnSpan(T, 3)
Case Type = "Dropbox"
Dim D As New ComboBox
LogHeight += 26
With D
.Name = "D" & i
.Visible = True
.Dock = DockStyle.Fill
.TabIndex = i + 1
.AutoCompleteMode = AutoCompleteMode.Suggest
.AutoCompleteSource = AutoCompleteSource.ListItems
.FormattingEnabled = True
.Sorted = True
Dim p = Split(Options(i), "|")
For o = 0 To p.Count - 1
D.Items.Add(p(o))
Next
If Not Entries.Contains(D) Then
Entries.Add(D)
AddHandler .SelectedValueChanged, AddressOf Dropbox_ValueChanged
End If
End With
Form_Grid.Controls.Add(D, 1, i + 1)
Form_Grid.SetColumnSpan(D, 3)
Case Type = "Numberbox"
Dim N As New NumericUpDown
LogHeight += 30
With N
.Name = "N" & i
.Visible = True
.Dock = DockStyle.Fill
.TabIndex = i + 1
.BorderStyle = BorderStyle.FixedSingle
If Not Entries.Contains(N) Then
Entries.Add(N)
End If
End With
Form_Grid.Controls.Add(N, 1, i + 1)
Form_Grid.SetColumnSpan(N, 3)
Case Type = "Checkbox"
Dim C As New CheckBox
LogHeight += 35
With C
.Name = "C" & i
.Visible = True
.Dock = DockStyle.Fill
.TabIndex = i + 1
.Text = L.Text
L.Visible = False
If Not Entries.Contains(C) Then
Entries.Add(C)
End If
End With
Form_Grid.Controls.Add(C, 1, i + 1)
Form_Grid.SetColumnSpan(C, 3)
End Select
Form_Grid.RowStyles(i + 1).SizeType = SizeType.AutoSize
i += 1
Loop
Me.Size = New System.Drawing.Size(280, LogHeight)
MySql.Close()
Catch ex As Exception
Notify.Errors(ex.HResult, ex.ToString, ex.Message)
End Try
End Sub