Change If to Loop

  • Thread starter Thread starter VB Novice Hendri
  • Start date Start date
V

VB Novice Hendri

Guest

I have a Data Tabel with 50 similary named Colums, "Con1 to Con50"

Most of the time not all will be used.

I need a simple LOOP to count the ones that has data and then again a LOOP to creating for the ones that have data.

I dont understand complicated structures like Classes and Funcsions.

I have got the code to work with IF structures, verry unpractical but it works.

So please help me turn this into 2 simple LOOPs do do the same thing.

Dim recipeSelected As String = TitleListBox.SelectedItem.ToString()
Dim result = recipesLst.Where(Function(x) x.RecipeName = recipeSelected).FirstOrDefault()
'Form1.TextBox1.Text = result.ID.ToString()




'Recipe Name Label
Dim RecipeNameLabel As New Label()
With RecipeNameLabel
.AutoSize = True
.MaximumSize = New Size(370, 100)
.MinimumSize = New Size(370, 10)
.TextAlign = ContentAlignment.MiddleCenter
.Text = result.RecipeName
.BackColor = LabelColor
.Font = TitleFont
.Location = New Point(75, 50)
End With
Controls.Add(RecipeNameLabel)
RecipeNameLabel.Parent = Form1
X = RecipeNameLabel.Left
Y = RecipeNameLabel.Bottom + 10
Form1.TextBox1.Text = "Recipe Name " & RecipeNameLabel.Size.ToString

'Comment Label
Dim CommentLabel As New Label()
With CommentLabel
.AutoSize = True
.MaximumSize = New Size(300, 150)
.MinimumSize = New Size(300, 10)
.TextAlign = ContentAlignment.MiddleLeft
.Text = result.Comment
.BackColor = LabelColor
.Font = New System.Drawing.Font("Comic Sans MS", 10)
.Location = New Point(X, Y)
End With
Controls.Add(CommentLabel)
CommentLabel.Parent = Form1
X = CommentLabel.Left
Y = CommentLabel.Bottom

'Quantity Label
Dim QuantityLabel As New Label()
With QuantityLabel
.AutoSize = True
.MaximumSize = New Size(300, 150)
.MinimumSize = New Size(300, 10)
.TextAlign = ContentAlignment.BottomRight
.Text = result.Quantity
.BackColor = LabelColor
.Font = New System.Drawing.Font("Comic Sans MS", 10)
.Location = New Point(440 - QuantityLabel.Width, CommentLabel.Bottom - QuantityLabel.Height)
End With
Controls.Add(QuantityLabel)
QuantityLabel.Parent = Form1

Dim ContentCount As Integer = 0

If result.Con1 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con2 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con3 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con4 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con5 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con6 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con7 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con8 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con9 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con10 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con11 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con12 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con13 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con14 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If
If result.Con15 <> "" Then
ContentCount = ContentCount + 1
Form1.TextBox1.Text = ContentCount.ToString
End If



Dim ActiveContent As String = "Con" & ContentCount.ToString

Dim CountVar As Integer
For CountVar = 1 To ContentCount
Dim ContentLabel As New Label()
With ContentLabel
'.Name = "Content " & ContentCount.ToString
.AutoSize = True
.MaximumSize = New Size(375, 100)
.MinimumSize = New Size(66, 10)
.TextAlign = ContentAlignment.MiddleLeft
If CountVar = 1 Then
.Text = result.Con1
End If
If CountVar = 2 Then
.Text = result.Con2
End If
If CountVar = 3 Then
.Text = result.Con3
End If
If CountVar = 4 Then
.Text = result.Con4
End If
If CountVar = 5 Then
.Text = result.Con5
End If
If CountVar = 6 Then
.Text = result.Con6
End If
If CountVar = 7 Then
.Text = result.Con7
End If
If CountVar = 8 Then
.Text = result.Con8
End If
If CountVar = 9 Then
.Text = result.Con9
End If
If CountVar = 10 Then
.Text = result.Con10
End If
If CountVar = 11 Then
.Text = result.Con11
End If
If CountVar = 12 Then
.Text = result.Con12
End If
If CountVar = 13 Then
.Text = result.Con13
End If
If CountVar = 14 Then
.Text = result.Con14
End If
If CountVar = 15 Then
.Text = result.Con15
End If
.BackColor = LabelColor
If .Text.Contains(":") Then
.Font = New System.Drawing.Font("Comic Sans MS", 12, FontStyle.Bold)
.Location = New Point(X, Y + 10)
Else
.Font = New System.Drawing.Font("Comic Sans MS", 10)
.Location = New Point(X, Y)
End If
End With
Controls.Add(ContentLabel)
ContentLabel.Parent = Form1

X = ContentLabel.Left
Y = ContentLabel.Bottom
Next




Continue reading...
 

Similar threads

V
Replies
0
Views
102
VB Novice Hendri
V
V
Replies
0
Views
108
VB Novice Hendri
V
V
Replies
0
Views
116
VB Novice Hendri
V
Back
Top