Attempting a Loop Creating Databound Labels on Demand.

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

VB Novice Hendri

Guest
I am attemting a Loop to create Databound Labels on demand.

I'm useg Visual Studio 2013

Below I am placing all my code, The Sub called "CreateContentLabels" is where I need a lot of help.

I am sure I am not going about this quite right. Please help do this.

As you can see in the Class there is 50 Content Labels needed, mostly only a couple is used therfor I want to create only the ones nesasery. That is way the Loop to create labels on demand, when nesasiry.

Public Class Recipe

Public Property ID As Integer
Public Property RecipeName As String
Public Property Comment As String
Public Property Quantity As String
Public Property Con1 As String
Public Property Con2 As String
Public Property Con3 As String
Public Property Con4 As String
Public Property Con5 As String
Public Property Con6 As String
Public Property Con7 As String
Public Property Con8 As String
Public Property Con9 As String
Public Property Con10 As String
Public Property Con11 As String
Public Property Con12 As String
Public Property Con13 As String
Public Property Con14 As String
Public Property Con15 As String
Public Property Con16 As String
Public Property Con17 As String
Public Property Con18 As String
Public Property Con19 As String
Public Property Con20 As String
Public Property Con21 As String
Public Property Con22 As String
Public Property Con23 As String
Public Property Con24 As String
Public Property Con25 As String
Public Property Con26 As String
Public Property Con27 As String
Public Property Con28 As String
Public Property Con29 As String
Public Property Con30 As String
Public Property Con31 As String
Public Property Con32 As String
Public Property Con33 As String
Public Property Con34 As String
Public Property Con35 As String
Public Property Con36 As String
Public Property Con37 As String
Public Property Con38 As String
Public Property Con39 As String
Public Property Con40 As String
Public Property Con41 As String
Public Property Con42 As String
Public Property Con43 As String
Public Property Con44 As String
Public Property Con45 As String
Public Property Con46 As String
Public Property Con47 As String
Public Property Con48 As String
Public Property Con49 As String
Public Property Con50 As String
Public Property Notes As String
Public Property ImageSize As String
Public Property ImageLocation As String

Function ActiveContent() As String
Throw New NotImplementedException
End Function

End Class



Imports System.Data.OleDb
'Imports System.IO

Public Class ToolsForm

Dim MyParent As Form1

Dim TitleFontStyle As FontStyle = FontStyle.Bold + FontStyle.Italic
Dim TitleFont As New System.Drawing.Font("Comic Sans MS", 20, TitleFontStyle)
Dim HeadingFontStyle As FontStyle = FontStyle.Bold
Dim HeadingFont As New System.Drawing.Font("Comic Sans MS", 11, HeadingFontStyle)
Dim ContentFont As New System.Drawing.Font("Comic Sans MS", 7)

Private recipesLst As List(Of Recipe) = New List(Of Recipe)()

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

MyParent = DirectCast(Me.Owner, Form1)
'MyParent.ChildStatus = 1

Me.Location = New Point(1050, 150)
Me.Size = New Size(347, 500)

Me.Input_Label.Text = "Wat doen ons vandag?"
'& vbCrLf & "" & vbCrLf & "Plaas ons 'n Nuwe Resep?" & vbCrLf & "of" & vbCrLf & "Soek ons na 'n Bestaande Resep?"
Me.Input_Label.Font = HeadingFont

Me.New_Recipe_Button.Location = New Point(Me.New_Recipe_Button.Left, Me.Input_Label.Bottom + 5)
Me.Save_View_Button.Location = New Point(Me.Save_View_Button.Left, Me.Input_Label.Bottom + 5)

Me.New_Recipe_Button.Text = "Plaas 'n Nuwe Resep"
Me.New_Recipe_Button.Size = New Size(158, 25)
Me.New_Recipe_Button.Font = HeadingFont

Me.Save_View_Button.Text = "Soek na 'n Resep"
Me.Save_View_Button.Size = New Size(158, 25)
Me.Save_View_Button.Font = HeadingFont

End Sub

Dim ConnectionString As String = Nothing
Dim ActiveTabel As String

Private Sub CategoryComboBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CategoryComboBox.SelectedIndexChanged

TitleListBox.Items.Clear()
Select Case CategoryComboBox.Text
Case Is = "Smaaklik Tuisgemaakte Drankies"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Drankies.accdb;"
TitleListBox.Items.Add("Koue Drankies")
TitleListBox.Items.Add("Melk Drankies")
TitleListBox.Items.Add("Warm Drankies")

Case Is = "Voorgeregte & Ander Ligte Eetes"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Voorgeregte.accdb;"
'No need to display Tabel names since this DataBase has only one Tabel.
ActiveTabel = "VoorgeregteLigteEetes"
Call ShowRecipeNames()

Case Is = "Hoofgeregte"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Hoofgeregte.accdb;"
TitleListBox.Items.Add("Eiergeregte")
TitleListBox.Items.Add("Heerlike Sop Resepte")
TitleListBox.Items.Add("Pasta, Bobotie, Breyani, Tamatie en Kerie")
TitleListBox.Items.Add("Viegitariese Eetes & Groente Geregte")
TitleListBox.Items.Add("Vleis Geregte")
TitleListBox.Items.Add("Pluimvee Geregte - Hoender, Eend ens.")
TitleListBox.Items.Add("Vis & ander Seekos Geregte")

Case Is = "Watertand Nageregte & Poedings"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Nageregte.accdb;"
TitleListBox.Items.Add("Koud voorberyde Nageregte (Poedings)")
TitleListBox.Items.Add("Warm voorberyde Nageregte")

Case Is = "Kraakvars Slaai Resepte"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Slaaie.accdb;"
'No need to display Tabel names since this DataBase has only one Tabel.
ActiveTabel = "SlaaiResepte"
Call ShowRecipeNames()

Case Is = "Souse & Marinades"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Souse.accdb;"
'No need to display Tabel names since this DataBase has only one Tabel.
ActiveTabel = "SouseMarinades"
Call ShowRecipeNames()

Case Is = "Geurige Tuisgebak"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Gebak.accdb;"
TitleListBox.Items.Add("Beskuit & Mosbolietjies")
TitleListBox.Items.Add("Brood & Broodrolietjies")
TitleListBox.Items.Add("Koek, Kolwyntjies & Ander Lekerneie")
TitleListBox.Items.Add("Muffens & Scones")
TitleListBox.Items.Add("Pannekoeke, Plaatkoekies & Tortillas")
TitleListBox.Items.Add("Southappies & Peeselkossies")
TitleListBox.Items.Add("Terte & Pasteie")

Case Is = "Gebottelde & Ingelegde Lekerneie"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Gebottelde.accdb;"
TitleListBox.Items.Add("Gepeekelde Lekerneie")
TitleListBox.Items.Add("Ingelegde Vrugte & Konfyte")
TitleListBox.Items.Add("Smeere & Bladjang")

Case Is = "Wenke & Boererate vir in en om die Huis"
ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=M:\My Documents\My Resepte\Wenke.accdb;"
TitleListBox.Items.Add("Kook & Bak Wenke")
TitleListBox.Items.Add("Huishoudelike Wenke")

End Select
End Sub

Private Sub TitleListBox_SelectedIndexChanged(sender As Object, e As EventArgs) Handles TitleListBox.SelectedIndexChanged
Try
Select Case TitleListBox.SelectedItem
'Smaaklik Tuisgemaakte Drankies
Case Is = "Koue Drankies"
ActiveTabel = "KoueDrankies"
Case Is = "Melk Drankies"
ActiveTabel = "MelkDrankies"
Case Is = "Warm Drankies"
ActiveTabel = "WarmDrankies"
'Hoofgeregte
Case Is = "Eiergeregte"
ActiveTabel = "Eiergeregte"
Case Is = "Heerlike Sop Resepte"
ActiveTabel = "SopResepte"
Case Is = "Pasta, Bobotie, Breyani, Tamatie en Kerie"
ActiveTabel = "PastaBobotieBreyaniTamatieKerie"
Case Is = "Viegitariese Eetes & Groente Geregte"
ActiveTabel = "ViegitarieseEetesGroenteGeregte"
Case Is = "Vleis Geregte"
ActiveTabel = "VleisGeregte"
Case Is = "Pluimvee Geregte - Hoender, Eend ens."
ActiveTabel = "PluimveeGeregte"
Case Is = "Vis & ander Seekos Geregte"
ActiveTabel = "VisSeekosGeregte"
'Watertand Nageregte & Poedings
Case Is = "Koud voorberyde Nageregte (Poedings)"
ActiveTabel = "KoueNageregte"
Case Is = "Warm voorberyde Nageregte"
ActiveTabel = "WarmNageregte"
'Geurige Tuisgebak
Case Is = "Beskuit & Mosbolietjies"
ActiveTabel = "BeskuitMosbolietjies"
Case Is = "Brood & Broodrolietjies"
ActiveTabel = "BroodBroodrolietjies"
Case Is = "Koek, Kolwyntjies & Ander Lekerneie"
ActiveTabel = "KoekAnderLekerneie"
Case Is = "Muffens & Scones"
ActiveTabel = "MuffensScones"
Case Is = "Pannekoeke, Plaatkoekies & Tortillas"
ActiveTabel = "PannekoekePlaatkoekiesTortillas"
Case Is = "Southappies & Peeselkossies"
ActiveTabel = "SouthappiesPeeselkosies"
Case Is = "Terte & Pasteie"
ActiveTabel = "TertePasteie"
'Gebottelde & Ingelegde Lekerneie
Case Is = "Gepeekelde Lekerneie"
ActiveTabel = "GepeekeldeLekerneie"
Case Is = "Ingelegde Vrugte & Konfyte"
ActiveTabel = "IngelegdeVrugteKonfyte"
Case Is = "Smeere & Bladjang"
ActiveTabel = "SmeereBladjang"
'Wenke & Boererate vir in en om die Huis
Case Is = "Kook & Bak Wenke"
ActiveTabel = "KookBakWenke"
Case Is = "Huishoudelike Wenke"
ActiveTabel = "HuishoudelikeWenke"
Case Else

With Form1.RecipeNameLabel
.Text = result.RecipeName
.Visible = True
End With
Form1.Label1.Text = Form1.RecipeNameLabel.Size.ToString

With Form1.CommentLabel
.Font = ContentFont
.Text = result.Comment
.Location = New Point(Form1.RecipeNameLabel.Left, Form1.RecipeNameLabel.Bottom + 10)
.Visible = True
End With
X = Form1.CommentLabel.Left
Y = Form1.CommentLabel.Bottom + 10

With Form1.QuantityLabel
.Font = ContentFont
.Text = result.Quantity
.Location = New Point(Form1.RecipeNameLabel.Right - Form1.QuantityLabel.Width, Form1.CommentLabel.Bottom - Form1.QuantityLabel.Height)
.Visible = True
End With

Call CreateContentLabels()

End Select

Call ShowRecipeNames()

Catch ex As Exception
MessageBox.Show(ex.Message, "A Programing Error Ocured", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try

End Sub

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


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

Dim ContentLabel(50) As Label
Dim X As Integer
Dim Y As Integer
'Dim IngredientLabelLocationX As Integer = 42
'Dim IngredientLabelLocationY As Integer = 146
Dim IngNo As Integer = 1



Sub CreateContentLabels()

For Index As Integer = 1 To 50
Form1.Controls.Remove(ContentLabel(Index))
Next

If result.ActiveContent.ToString = "" Then
Exit Sub
Else
For Index As Integer = 1 To 50
ContentLabel(Index) = New Label With {.Location = New Point(X, Y), _
.Font = ContentFont, _
.Text = result.ActiveContent.ToString, _
.AutoSize = True, _
.MaximumSize = New Size(550, 60), _
.MinimumSize = New Size(66, 20, _
.BackColor = Color.Transparent, _
.TextAlign = ContentAlignment.MiddleLeft, _
.Image = Image.FromFile("M:\My Documents\Visual Studio 2013\Projects\Virtuele Resepte Boek in Afrikaanse\Resources\Measuring-Jug.png"), _
.ImageAlign = ContentAlignment.MiddleLeft)}
Controls.Add(ContentLabel(Index))

IngredientLabel(Index).Parent = Form1
X = ContentLabel.Left
Y = ContentLabel.Bottom + 1

Next
End If

End Sub

Sub ShowRecipeNames()

TitleListBox.Items.Clear()

Using cn As OleDbConnection = New OleDbConnection(ConnectionString)
cn.Open()
Dim cmdText As String = String.Format("SELECT ID, RecipeName, Comment, Quantity, Con1, Con2, Con3, Con4, Con5, Con6, Con7, Con8, Con9, Con10, Con11, Con12, Con13, Con14, Con15, Con16 FROM {0}", ActiveTabel)

Using cmd As OleDbCommand = New OleDbCommand(cmdText, cn)
Dim reader = cmd.ExecuteReader()

While reader.Read()
TitleListBox.Items.Add(reader("RecipeName"))
recipesLst.Add(New Recipe With {
.ID = Convert.ToInt32(reader("ID")),
.RecipeName = reader("RecipeName").ToString(),
.Comment = reader("Comment").ToString(),
.Quantity = reader("Quantity").ToString(),
.Con1 = reader("Con1").ToString(),
.Con2 = reader("Con2").ToString(),
.Con3 = reader("Con3").ToString(),
.Con4 = reader("Con4").ToString(),
.Con5 = reader("Con5").ToString(),
.Con6 = reader("Con6").ToString(),
.Con7 = reader("Con7").ToString(),
.Con8 = reader("Con8").ToString(),
.Con9 = reader("Con9").ToString(),
.Con10 = reader("Con10").ToString(),
.Con11 = reader("Con11").ToString()
})
End While
End Using
End Using
End Sub

Private Sub CategoryComboBox_DropDownClosed(sender As Object, e As EventArgs) Handles CategoryComboBox.DropDownClosed

If Me.TitleListBox.Visible = False Then
Me.TitleListBox.Visible = True
Me.TitleListBox.Location = New Point(Me.CategoryComboBox.Left, Me.CategoryComboBox.Bottom + 5)
End If

End Sub

Private Sub Save_View_Button_Click(sender As Object, e As EventArgs) Handles Save_View_Button.Click

Me.CategoryComboBox.Visible = True
Me.CategoryComboBox.Location = New Point(Me.New_Recipe_Button.Left, Me.New_Recipe_Button.Bottom + 5)

End Sub

End Class

Continue reading...
 

Similar threads

V
Replies
0
Views
110
VB Novice Hendri
V
V
Replies
0
Views
133
VB Novice Hendri
V
V
Replies
0
Views
122
VB Novice Hendri
V
V
Replies
0
Views
141
VB Novice Hendri
V
V
Replies
0
Views
99
VB Novice Hendri
V
Back
Top