I want to design datagridview at run time I want it to fill with data and appear on the form according to comboboxtext and if combobox is empty it disappear and when I click on datagridview I want the cell to be selectedvalue in the combobox and datagriddisapear
And here the code that I tried
Private Sub ComDr_KeyPress(sender As Object, e As KeyPressEventArgs) Handles ComDr.KeyPress
Dim d As New DataGridView
Dim dt As DataTable = New DAL().selectdatatable(String.Format("select Dr_Name,Dr_No from DR where Dr_Name like '%{0}%'", ComDr.Text))
If dt.Rows.Count > 0 Then
' Try
Controls.Add(d)
d.DataSource = dt
d.BringToFront()
With d
.Name = "Dr_Name"
.Width = ComDr.Width
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill
.ColumnHeadersHeight = 30
.Height = 10 * 35
.ForeColor = Color.Blue
.BackgroundColor = Color.Beige
.RowHeadersVisible = False
.BackgroundColor = Color.BurlyWood
'.DefaultCellStyle.SelectionForeColor = Color.Yellow
' .DefaultCellStyle.SelectionForeColor = Color.Black
' .DefaultCellStyle.Font = New Font("tahoma", 13)
.Location = New System.Drawing.Point(680, ComDr.Location.Y + 30)
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
For i As Integer = 0 To d.Rows.Count - 1
Dim row As DataGridViewRow = d.Rows(i)
row.Height = 35
d.Rows(i).DefaultCellStyle.BackColor = If(i Mod 2 = 0, Color.Wheat, Color.BurlyWood)
Next
AddHandler d.CellClick, AddressOf d_cellclick
'ComDr.DisplayMember = d(0, d.SelectedRows(0).Index).Value.ToString
'ComDr.ValueMember = d(1, d.SelectedRows(0).Index).Value.ToString
'ComDr.Text = String.Empty
'Controls.Remove(d)
' ComDr.SelectedValue = d(0, d.SelectedRows(0).Index).Value.ToString
'Catch ex As Exception
'MsgBox(ex.Message)
'End Try
' Controls.Remove(d)
End If
End Sub
Related
can i add tabcontrol elements after programm started?
i use a Tabcontrol from GUI-Designer and wanna manipulate a TabPage.
i want there new dynamical checkboxes on Formular _load.
i can't see these checkboxes on my TabPages
For i = 0 To frmMain.cmbZielSpache.Items.Count - 1
Dim cBox = New CheckBox()
cBox.Name = "GEN_" & i
cBox.Location = New Point(offsetX, offsetY)
cBox.Text = frmMain.cmbZielSpache.Items(i)
If frmMain.cmbZielSpache.Items(i) = frmMain.cmbZielSpache.Text Or My.Settings.chkTranslate_normal_alleSprachen = True Then
cBox.Checked = True
End If
offsetX = offsetX + 120
Me.Controls.Add(cBox)
AddHandler cBox.CheckedChanged, AddressOf checkChangedHandler
Next i
Instead of using Me.Controls i wanna add the checkboxes after programm started dynamically.
The solution is TabControl1.TabPages(0).Controls.add()
Sub checkChangedHandler(sender As Object, e As EventArgs)
lblSprachenListeZielSprachen.Text = "Ziel Übersetzungsprache: "
For Each chkBox In TabControl1.TabPages(0).Controls.OfType(Of CheckBox)()
'Check here whether your check boxes are checked true/false
If chkBox.Checked = True Then
If Not chkBox.Name.IndexOf("GEN_") = -1 Then
lblSprachenListeZielSprachen.Text &= chkBox.Text & ","
End If
End If
Next
gblAktiveÜbersetzungssprachen = lblSprachenListeZielSprachen.Text.Replace("Ziel Übersetzungsprache: ", "")
End Sub
For i = 0 To frmMain.cmbZielSpache.Items.Count - 1
Dim cBox = New CheckBox()
cBox.Name = "GEN_" & i
cBox.Location = New Point(offsetX, offsetY)
cBox.Text = frmMain.cmbZielSpache.Items(i)
If frmMain.cmbZielSpache.Items(i) = frmMain.cmbZielSpache.Text Or My.Settings.chkTranslate_normal_alleSprachen = True Then
cBox.Checked = True
End If
offsetX = offsetX + 120
AddHandler cBox.CheckedChanged, AddressOf checkChangedHandler
Me.TabControl1.TabPages(0).Controls.Add(cBox)
Next i
I have three panel type objects (A, B, and C), which have been dynamically generated within another panel type control.
My question is, how can I exchange panel B to the position of panel A and panel A to the position of panel B? This will be triggered by a click on a ToolStripMenuItem.
What I had thought, was to go through the arrangement of panels to know who exists and from there to work them, is that correct?
For Each obj As Control In Panel1.Controls
MsgBox(obj.Name)
Next
This is the code that I use to move to right:
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
End Sub
This is the code I use to generate objects dynamically:
Private Sub TileNavItem5_ElementClick(sender As Object, e As NavElementEventArgs) Handles TileNavItem5.ElementClick
Dim pos As Int32 = cInt(TextBox38.Text)
Dim poslabel As Int16 = cInt(TextBox42.Text)
Dim posY As Int16 = 330
Dim posX As Int16 = 3
Panel1.AutoScrollPosition = New Point(0, 0)
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImage = Image.FromFile("C:\example.bmp"),
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpSection,
.Name = "Panel" & Val(TextBox37.Text)
}
AddHandler pb.Click, AddressOf myClickHandler_b
Dim labela As New Label With {
.AutoSize = True,
.Location = New Point((poslabel), 12),
.Text = "Section " & CInt(TextBox37.Text),
.ForeColor = Color.White,
.BackColor = Color.Transparent,
.Font = New Font(Me.Font, FontStyle.Bold),
.Name = "Label" & CInt(TextBox37.Text)
}
pb.Location = New Point(0, 0)
pb.Location = New Point(pos, 20)
Panel1.Controls.Add(pb)
pb.Controls.Add(labela)
For j = 1 To 4
Dim pbdoors As New Panel With
{
.Width = 114,
.Height = 98,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpUnit,
.Name = "Unit" & Val(TextBox37.Text) & j
}
AddHandler pbdoors.Click, AddressOf myClickHandler_doors
pbdoors.Location = New Point(posX, posY)
pb.Controls.Add(pbdoors)
posY = (posY - 100)
Next
Panel1.AutoScrollPosition = New Point(Panel1.HorizontalScroll.Maximum, Panel1.VerticalScroll.Maximum)
TextBox37.Text = CInt(TextBox37.Text) + 1
TextBox38.Text = Val(TextBox38.Text) + 120
End Sub
You just need to find the controls. Swapping is the easy part.
Finding controls is also easy if you use Control.Controls.Find(String, Boolean). But you must at least know the control's name.
The difficulty comes in here
' TextBox37.Text = CInt(TextBox37.Text) + 1 ' implicit conversion from int to string
TextBox37.Text = (CInt(TextBox37.Text) + 1).ToString()
where you must find the control by name and you have built some integer into the name. Can you keep track of how many times 1 is added to TextBox37.Text?
If you can, you can pass it into this function, and the swapping will be performed
Private Sub swap(index1 As Integer, index2 As Integer)
' build the panel names
Dim p1Name = $"Panel{index1}"
Dim p2Name = $"Panel{index2}"
' find the panels
Dim p1 = DirectCast(Panel1.Controls.Find(p1Name, True).FirstOrDefault(), Panel)
If p1 Is Nothing Then Throw New ArgumentException("index1")
Dim p2 = DirectCast(Panel1.Controls.Find(p2Name, True).FirstOrDefault(), Panel)
If p2 Is Nothing Then Throw New ArgumentException("index2")
' swap the panels
Dim temp = p2.Location
p2.Location = p1.Location
p1.Location = temp
End Sub
swap(1, 2) will swap panel 1 with 2. swap(4, 6) will swap the panel 4 with 6. This logic is not included in your question (i.e. how many times is TileNavItem5_ElementClick called?), so you know better how to incorporate it. I hope it works for you.
I am trying to dynamically add images to a TableLayoutPanel. The add appears to work, when I look at the contents of the control, the new items appear to have been added. The RowCount = 10, the IEnumerable has 18 items that contain the controls I have added.
My problem is, is that when the form is shown, only the first row is visible. I cannot dynamically set the Visible property to True on anything, the instruction is ignored. What am I missing?
Code:
Private Sub LoadTable()
' get the width in pixels of the columns
Dim oSizeType As SizeType = TableLayoutPanel1.ColumnStyles(0).SizeType
Dim Width As Single
Select Case oSizeType
Case SizeType.Percent, SizeType.AutoSize
Width = TableLayoutPanel1.Width / TableLayoutPanel1.ColumnCount
Case SizeType.Absolute
Width = TableLayoutPanel1.ColumnStyles(0).Width
End Select
' Fix the height of the rows
Dim Height As Single = Width
Dim oPicture As New PictureBox
Dim Cols As Integer = 1
Dim Rows As Integer = -1
Dim Cell As String = String.Empty
' loop through all the images from the folder
For i As Integer = 0 To m_FileList.Count - 1
' establish the current row/column in the table
Dim j As Integer = i + 1
'MsgBox(Fix(j / 2))
If Fix(j / 2) <> CDbl(j / 2) Then
Cols = 0
Rows += 1
' add a row if we have moved to the next row
If Rows > 0 Then TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, Height))
' this doesn't happen automatically
TableLayoutPanel1.RowCount = Rows + 1
Else
Cols = 1
End If
' this is used for the name of some controls
Cell = "R" & Rows & "C" & Cols
' now scale the image to fit into the Table Cells
Dim oPictureBox As New PictureBox
oPictureBox.Height = Height
oPictureBox.Width = Width
Dim oImage As Bitmap = New Bitmap(Bitmap.FromFile(CType(m_FileList.Item(i).Value, String)))
' set the PictureBox properties
oPictureBox.BackgroundImageLayout = ImageLayout.Stretch
oPictureBox.BorderStyle = BorderStyle.Fixed3D
' scale the image to the PictureBox size
'Dim oResized As Image = New Bitmap(Bitmap.FromFile(CType(oPictureBox.Tag, String)))
'ImageEdit.ResizeImage(oImage, oResized, picEnlargement.Width, picEnlargement.Height, False)
ScaleImage(oPictureBox, oImage)
' set the Image of the PictureBox
oPictureBox.Image = oImage
oPictureBox.Name = "PictureBox" & i
' get the path of the current file
Dim f As String = m_FileList(i).Value
'set the properties of the new controls
Dim t As String = GetTitle(f)
'oLabel.Text = t
'oLabel.AutoSize = True
'oLabel.Location = New System.Drawing.Point(30, 110)
oPicture = New PictureBox
With oPicture
SetToolTip(oPicture, f)
oPicture.Tag = f
.Image = oPictureBox.Image
.Dock = DockStyle.Fill
.Size = New System.Drawing.Size(100, 100)
.SizeMode = PictureBoxSizeMode.StretchImage
.Location = New System.Drawing.Point(2, 2)
.Cursor = Cursors.Hand
.Name = "PictureBox" & i + 1
.Visible = True
End With
'here we add the controls to a layout panel to
'manage the positioning of the controls
Dim oContainer As New Panel
With oContainer
.Dock = DockStyle.Fill
.Margin = New System.Windows.Forms.Padding(0)
.Controls.Add(oPicture)
'.Controls.Add(oLabel)
.MaximumSize = New Size(Height, Width)
.MinimumSize = New Size(Height, Width)
.Name = "Container_" & Cell
.Visible = True
End With
' add the
TableLayoutPanel1.Controls.Add(oContainer, Cols, Rows)
'TableLayoutPanel1.SetRow(oContainer, Rows)
'TableLayoutPanel1.SetColumn(oContainer, Cols)
TableLayoutPanel1.Controls.Item(i).Name = "Control_" & Cell
TableLayoutPanel1.Controls.Item(i).Enabled = True
TableLayoutPanel1.Controls.Item(i).Visible = True
'here we add a handler for the picture boxs click event
AddHandler oPicture.Click, AddressOf oPictureClickEvent
Next
TableLayoutPanel1.Visible = True
For i As Integer = 0 To TableLayoutPanel1.Controls.Count - 1
TableLayoutPanel1.Controls(i).Enabled = True
TableLayoutPanel1.Controls(i).Visible = True
Next
End Sub
What was missing was setting the TableLayoutPanel Height. After adding the controls, you need to adjust the Height of the table to a multiple of the height of the rows * (number of rows - 1), that's the basic requirement, but needs a tweak to stop the last row being stretched...
Private Sub LoadTable()
Dim oPictures As Control() = {}
'Dim oTableLayoutPanel As New TableLayoutPanel
' get the width in pixels of the columns
Dim oSizeType As SizeType = TableLayoutPanel1.ColumnStyles(0).SizeType
Dim Width As Single
Select Case oSizeType
Case SizeType.Percent, SizeType.AutoSize
Width = TableLayoutPanel1.Width / TableLayoutPanel1.ColumnCount
Case SizeType.Absolute
Width = TableLayoutPanel1.ColumnStyles(0).Width
End Select
' Fix the height of the rows
Dim Height As Single = Width
Dim oPicture As New PictureBox
Dim Cols As Integer = 1
Dim Rows As Integer = -1
Dim Cell As String = String.Empty
TableLayoutPanel1.RowCount = Fix(m_FileList.Count / 2)
' loop through all the images from the folder
For i As Integer = 0 To m_FileList.Count - 1
' establish the current row/column in the table
Dim j As Integer = i + 1
'MsgBox(Fix(j / 2))
If Fix(j / 2) <> CDbl(j / 2) Then
Cols = 0
Rows += 1
' add a row if we have moved to the next row
TableLayoutPanel1.RowStyles.Add(New RowStyle(SizeType.Absolute, Height))
' this doesn't happen automatically
TableLayoutPanel1.RowCount = Rows + 1
Else
' this line should never be needed but just in case
If Rows < 0 Then Rows = 0
Cols = 1
End If
' this is used for the name of some controls
Cell = "R" & Rows & "C" & Cols
'--------------------------------------------
' set up the PictureBox
'--------------------------------------------
Dim oPictureBox As New PictureBox
' get the path of the current file
Dim f As String = m_FileList(i).Value
'set the properties of the new controls
Dim t As String = GetTitle(f)
' create the PictureBox
With oPictureBox
.Height = Height
.Width = Width
' load the image from the current file path
Dim oImage As Bitmap = New Bitmap(Bitmap.FromFile(CType(m_FileList.Item(i).Value, String)))
' set the PictureBox properties
.BackgroundImageLayout = ImageLayout.Stretch
.BorderStyle = BorderStyle.Fixed3D
' scale the image to the PictureBox size
ScaleImage(oPictureBox, oImage)
' set the Image of the PictureBox
.Image = oImage
.Name = "PictureBox" & i + 1
.Dock = DockStyle.Fill
.Size = New System.Drawing.Size(Height, Width)
.SizeMode = PictureBoxSizeMode.StretchImage
.Location = New System.Drawing.Point(0, 0)
.Cursor = Cursors.Hand
.Visible = True
.Enabled = True
' set the Tag to the filepath of the image
.Tag = f
' set the ToolTip for the PictureBox
' This becomes the "Title" from the File MetaData
SetToolTip(oPictureBox, f)
'here we add a handler for the PictureBox click event
AddHandler oPictureBox.Click, AddressOf PictureClickEvent
End With
oPictures.Add(oPictureBox)
Next
' now add the picturebox to the next cell addres
With TableLayoutPanel1
' add the
.Controls.AddRange(oPictures)
'.Controls.Add(oPictureBox, Cols, Rows)
'.SetRow(oPictureBox, Rows)
'.SetColumn(oPictureBox, Cols)
'.Controls.Item(i).Name = "Control_" & Cell
'.Controls.Item(i).Enabled = True
'.Controls.Item(i).Visible = True
End With
TableLayoutPanel1.Visible = True
For i As Integer = 0 To TableLayoutPanel1.Controls.Count - 1
TableLayoutPanel1.Controls(i).Enabled = True
TableLayoutPanel1.Controls(i).Visible = True
Next
' adjust the height of the table to a multiple of the number of new rows * the Height of each row
TableLayoutPanel1.Height = (TableLayoutPanel1.RowCount - 1) * Height
' set the TableLayoutPanel properties
TableLayoutPanel1.Dock = DockStyle.Top
TableLayoutPanel1.AutoSize = False
TableLayoutPanel1.Visible = True
TableLayoutPanel1.Enabled = True
TableLayoutPanel1.Focus()
TableLayoutPanel1.Parent = Me.Panel1
End Sub
I'm working with a gridview on vb.net for webforms, I have to add titles to specific groups of columns in this table.. but I have not a clue of how to do it, heres an example of what i want:
"Student info", "Subject info" and "Partial Grades" ARE the titles I need to add.
Default Datagridview hasnt got an option as u wish.
You can try devexpress (It has got tones of properties in gridcontrol)
You can create your own control acting like your "TITLE LABELS"
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim DataGridView1 = New System.Windows.Forms.DataGridView()
Dim Column1 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim Column2 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim Column3 = New System.Windows.Forms.DataGridViewTextBoxColumn()
Dim TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Dim Label1 = New System.Windows.Forms.Label()
Dim Label2 = New System.Windows.Forms.Label()
'DataGridView1
DataGridView1.BorderStyle = System.Windows.Forms.BorderStyle.None
DataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
DataGridView1.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Column1, Column2, Column3})
DataGridView1.Location = New System.Drawing.Point(13, 78)
DataGridView1.Name = "DataGridView1"
DataGridView1.RowHeadersVisible = False
DataGridView1.Size = New System.Drawing.Size(397, 228)
DataGridView1.TabIndex = 0
DataGridView1.Name = "dgv"
AddHandler DataGridView1.ColumnWidthChanged, AddressOf DataGridView1_ColumnWidthChanged
'
'Column1
'
Column1.HeaderText = "Column1"
Column1.Name = "Column1"
'
'Column2
'
Column2.HeaderText = "Column2"
Column2.Name = "Column2"
'
'Column3
'
Column3.AutoSizeMode = System.Windows.Forms.DataGridViewAutoSizeColumnMode.Fill
Column3.HeaderText = "Column3"
Column3.Name = "Column3"
'
'TableLayoutPanel1
'
TableLayoutPanel1.ColumnCount = 2
TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 53.98936!))
TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 195.0!))
TableLayoutPanel1.Controls.Add(Label2, 1, 0)
TableLayoutPanel1.Controls.Add(Label1, 0, 0)
TableLayoutPanel1.Location = New System.Drawing.Point(12, 51)
TableLayoutPanel1.Name = "TableLayoutPanel1"
TableLayoutPanel1.RowCount = 1
TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
TableLayoutPanel1.Size = New System.Drawing.Size(398, 29)
TableLayoutPanel1.TabIndex = 1
TableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.[Single]
TableLayoutPanel1.Name = "title"
'
'Label1
'
Label1.Anchor = System.Windows.Forms.AnchorStyles.None
Label1.AutoSize = True
Label1.Location = New System.Drawing.Point(82, 8)
Label1.Name = "Label1"
Label1.Size = New System.Drawing.Size(39, 13)
Label1.TabIndex = 0
Label1.Text = "Label1"
'
'Label2
'
Label2.Anchor = System.Windows.Forms.AnchorStyles.None
Label2.AutoSize = True
Label2.Location = New System.Drawing.Point(281, 8)
Label2.Name = "Label2"
Label2.Size = New System.Drawing.Size(39, 13)
Label2.TabIndex = 1
Label2.Text = "Label2"
'
'Form1
'
Me.Controls.Add(TableLayoutPanel1)
Me.Controls.Add(DataGridView1)
End Sub
Private Sub DataGridView1_ColumnWidthChanged(sender As Object, e As DataGridViewColumnEventArgs)
Dim title As TableLayoutPanel = CType(Me.Controls("title"), TableLayoutPanel)
Dim dgv As DataGridView = CType(Me.Controls("dgv"), DataGridView)
title.ColumnStyles(0).Width = dgv.Columns(0).Width + dgv.Columns(1).Width
title.ColumnStyles(1).Width = dgv.Columns(2).Width
End Sub
End Class
I am trying to figure out the functions for an application that you order donuts and coffee through. There is no box to input quantities. The type of doughnuts are selected using radio buttons grouped in combo boxes as is the type of coffee. The user ordering does not input any information. That application calculates according to the selected radio buttons. There is also a 3% sales tax.
The donuts are as follows:
Glazed are $0.65, Sugar $0.65, Chocolate are $0.85, and Filled are $1.00.
The coffee costs:
Regular is $1.00 and cappuccino is $2.50.
How do I write a function for each one? I would think I would just write one function that calculates the donut, coffee and sales tax. I am not sure what I am supposed to include if there is only one possible choice for each case. I thought that I could just write a bunch of constants since nothing changed and do an If statement but the assignment is asking for functions.
This is what I have so far for the doughnuts.
Private Function GetDonutPrice(ByVal strDonut As String) As Double
Dim dblDonutPrice As Double
'Calculates and returns the price of donuts
If strDonut = "G" Or strDonut = "S" Then
dblDonutPrice = 0.65
ElseIf strDonut = "C" Then
dblDonutPrice = 0.85
Else
strDonut = "F"
dblDonutPrice = 1.0
End If
Return dblDonutPrice
End Function
And then for the btnCalc_Click I coded this:
Private Sub btnCalc_Click(sender As Object, e As EventArgs)
'Displays donut and cappucino subtotal, taxes and price
Dim strDonutPrice As String
Dim dblDonutPrice As Double
If radGlazed.Checked Then
strDonutPrice = "G"
ElseIf radSugar.Checked Then
strDonutPrice = "S"
ElseIf radChoc.Checked Then
strDonutPrice = "C"
ElseIf radFilled.Checked strDonutPrice = "F"
End If
' get the donut price '
dblDonutPrice = GetDonutPrice(strDonutPrice)
End Sub
And I get an error in dblDonutPrice = GetDonutPrice(strDonutPrice)
It sounds like you need a function that receives the type and quantity of doughnuts and does the math and returns the cost. And the same for the coffee. So you will have three functions total. The coffee, the doughnuts, and the main one that take the input of what is order and how many, calls the other functions, adds it up, and then does the sales tax
Something like this maybe.
Private Sub cmdAddItUp_Click(sender As System.Object, e As System.EventArgs) Handles cmdAddItUp.Click
Dim iQuantity1 as integer
Dim dDoughnutCost as double
'Get the data to send to doughnuts
iQuantity1 = val(TextBox1.Text)
iQuantity2 = val(TextBox2.Text)
etc.
'Get the doughnuts cost
dDoughnutCost = Doughtnuts(iQuantity1, iQuantity2, iQuantity3)
'Do the same for the coffee
dCoffeeCost = Coffees(iQuantity1, iQuantity2, iQuantity3)
'Add it up
dSubTotal = dDoughnutCost + dCoffeeCost
'Calculate tax
dTotal = Tax(dSubTotal)
'Now you have the total, do something with it. Display it maybe!
End Sub
Private Function Doughtnuts(iQuantity1 As Integer, iQuantity2 As Integer, iQuantity3 As Integer) As Double
'Do the math
End Function
Private Function Coffees(iQuantity1 As Integer, iQuantity2 As Integer, iQuantity3 As Integer) As Double
'Do the math
End Function
Private Function Tax(dSubtotal As Double) As Double
'Calculate tax
End Function
Hope that helps get you started. Good luck with your schooling.
I made some assumptions on your UI. Rather than placing the items as radio buttons in a ComboBox, I placed all of the doughnuts and coffees into a respective GroupBox. The concepts of this answer will still apply, however you'll just have to be mindful that this is not directly drop-in and go.
Here's the way I laid out the UI:
I have placed the RadioButtons for the items inside a FlowLayoutPanel inside of a GroupBox. I also placed labels into a TableLayoutPanel to provide the total amounts.
The most important part is how I named the RadioButton controls for the items being listed. In the code behind file I used the names of the RadioButton controls to check which Item was being modified. I could have populated the RadioButton controls at run time to ensure that everything lined up, but that was more work and it's not difficult to do if you use GetType(MyEnum).GetNames() in the Me.Load event of the form to create new RadioButtons with the correct name, text, and adding the proper event handlers to the radio buttons.
If you'd like to see how this works, add a new form to your Project and call it DoughnutsAndCoffee. Here is the designer file and the Code behind:
Code Behind:
Public Class DoughnutsAndCoffee
Private SelectedItems As New List(Of Items)
Public Event ItemsChanged()
Public Sub UpdateTotal() Handles Me.ItemsChanged
Dim subTotal As Decimal = 0D
For Each item As Items In SelectedItems
subTotal += Prices.GetPrice(item)
Next
Dim TaxTotal As Decimal = subTotal * Prices.Tax_Rate
Dim total As Decimal = subTotal + TaxTotal
Me.SubTotal_Label.Invoke(Sub() Me.SubTotal_Label.Text = subTotal.ToString("C2"))
Me.Tax_Label.Invoke(Sub() Me.Tax_Label.Text = TaxTotal.ToString("C2"))
Me.Total_Label.Invoke(Sub() Me.Total_Label.Text = total.ToString("C2"))
End Sub
Private Sub RadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles Cappuccino_RadioButton.CheckedChanged, Chocolate_RadioButton.CheckedChanged, Filled_RadioButton.CheckedChanged, Glazed_RadioButton.CheckedChanged, Regular_RadioButton.CheckedChanged, Sugar_RadioButton.CheckedChanged
Dim senderRB As RadioButton = DirectCast(sender, RadioButton)
Dim selectedItem As String = (From item As String In GetType(Items).GetEnumNames Where senderRB.Name.Contains(item) Select item).FirstOrDefault
If selectedItem <> String.Empty Then
Dim Item As Items = DirectCast([Enum].Parse(GetType(Items), selectedItem), Items)
If senderRB.Checked Then
Me.SelectedItems.Add(Item)
RaiseEvent ItemsChanged()
Else
If Me.SelectedItems.Contains(Item) Then
Me.SelectedItems.Remove(Item)
RaiseEvent ItemsChanged()
End If
End If
End If
End Sub
Private Sub DoughnutsAndCoffee_Shown(sender As Object, e As EventArgs) Handles Me.Shown
RaiseEvent ItemsChanged()
End Sub
End Class
Public Structure Prices
Public Const Tax_Rate As Decimal = 0.03D
Public Const Glazed As Decimal = 0.65D
Public Const Sugar As Decimal = 0.65D
Public Const Chocolate As Decimal = 0.85D
Public Const Filled As Decimal = 1D
Public Const Regular As Decimal = 1D
Public Const Cappuccino As Decimal = 2.5D
Public Shared Function GetPrice(item As Items) As Decimal
Dim itemStr As String = [Enum].GetName(GetType(Items), item)
Return GetType(Prices).GetField(itemStr).GetValue(Nothing)
End Function
End Structure
Public Enum Items
Glazed
Sugar
Chocolate
Filled
Regular
Cappuccino
End Enum
Designer File:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class DoughnutsAndCoffee
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Glazed_RadioButton = New System.Windows.Forms.RadioButton()
Me.Sugar_RadioButton = New System.Windows.Forms.RadioButton()
Me.Chocolate_RadioButton = New System.Windows.Forms.RadioButton()
Me.Filled_RadioButton = New System.Windows.Forms.RadioButton()
Me.Regular_RadioButton = New System.Windows.Forms.RadioButton()
Me.Cappuccino_RadioButton = New System.Windows.Forms.RadioButton()
Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
Me.Total_Label = New System.Windows.Forms.Label()
Me.Label5 = New System.Windows.Forms.Label()
Me.Tax_Label = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.SubTotal_Label = New System.Windows.Forms.Label()
Me.Label1 = New System.Windows.Forms.Label()
Me.FlowLayoutPanel2 = New System.Windows.Forms.FlowLayoutPanel()
Me.FlowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
Me.Coffee_GroupBox = New System.Windows.Forms.GroupBox()
Me.Doughnuts_GroupBox = New System.Windows.Forms.GroupBox()
Me.TableLayoutPanel1.SuspendLayout()
Me.FlowLayoutPanel2.SuspendLayout()
Me.FlowLayoutPanel1.SuspendLayout()
Me.Coffee_GroupBox.SuspendLayout()
Me.Doughnuts_GroupBox.SuspendLayout()
Me.SuspendLayout()
'
'Glazed_RadioButton
'
Me.Glazed_RadioButton.AutoSize = True
Me.Glazed_RadioButton.Location = New System.Drawing.Point(3, 3)
Me.Glazed_RadioButton.Name = "Glazed_RadioButton"
Me.Glazed_RadioButton.Size = New System.Drawing.Size(58, 17)
Me.Glazed_RadioButton.TabIndex = 9
Me.Glazed_RadioButton.Text = "Glazed"
Me.Glazed_RadioButton.UseVisualStyleBackColor = True
'
'Sugar_RadioButton
'
Me.Sugar_RadioButton.AutoSize = True
Me.Sugar_RadioButton.Location = New System.Drawing.Point(3, 26)
Me.Sugar_RadioButton.Name = "Sugar_RadioButton"
Me.Sugar_RadioButton.Size = New System.Drawing.Size(53, 17)
Me.Sugar_RadioButton.TabIndex = 10
Me.Sugar_RadioButton.Text = "Sugar"
Me.Sugar_RadioButton.UseVisualStyleBackColor = True
'
'Chocolate_RadioButton
'
Me.Chocolate_RadioButton.AutoSize = True
Me.Chocolate_RadioButton.Location = New System.Drawing.Point(3, 49)
Me.Chocolate_RadioButton.Name = "Chocolate_RadioButton"
Me.Chocolate_RadioButton.Size = New System.Drawing.Size(73, 17)
Me.Chocolate_RadioButton.TabIndex = 11
Me.Chocolate_RadioButton.Text = "Chocolate"
Me.Chocolate_RadioButton.UseVisualStyleBackColor = True
'
'Filled_RadioButton
'
Me.Filled_RadioButton.AutoSize = True
Me.Filled_RadioButton.Location = New System.Drawing.Point(3, 72)
Me.Filled_RadioButton.Name = "Filled_RadioButton"
Me.Filled_RadioButton.Size = New System.Drawing.Size(49, 17)
Me.Filled_RadioButton.TabIndex = 12
Me.Filled_RadioButton.Text = "Filled"
Me.Filled_RadioButton.UseVisualStyleBackColor = True
'
'Regular_RadioButton
'
Me.Regular_RadioButton.AutoSize = True
Me.Regular_RadioButton.Location = New System.Drawing.Point(3, 3)
Me.Regular_RadioButton.Name = "Regular_RadioButton"
Me.Regular_RadioButton.Size = New System.Drawing.Size(62, 17)
Me.Regular_RadioButton.TabIndex = 13
Me.Regular_RadioButton.Text = "Regular"
Me.Regular_RadioButton.UseVisualStyleBackColor = True
'
'Cappuccino_RadioButton
'
Me.Cappuccino_RadioButton.AutoSize = True
Me.Cappuccino_RadioButton.Location = New System.Drawing.Point(3, 26)
Me.Cappuccino_RadioButton.Name = "Cappuccino_RadioButton"
Me.Cappuccino_RadioButton.Size = New System.Drawing.Size(82, 17)
Me.Cappuccino_RadioButton.TabIndex = 14
Me.Cappuccino_RadioButton.Text = "Cappuccino"
Me.Cappuccino_RadioButton.UseVisualStyleBackColor = True
'
'TableLayoutPanel1
'
Me.TableLayoutPanel1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TableLayoutPanel1.AutoSize = True
Me.TableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
Me.TableLayoutPanel1.ColumnCount = 2
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
Me.TableLayoutPanel1.Controls.Add(Me.Total_Label, 1, 2)
Me.TableLayoutPanel1.Controls.Add(Me.Label5, 0, 2)
Me.TableLayoutPanel1.Controls.Add(Me.Tax_Label, 1, 1)
Me.TableLayoutPanel1.Controls.Add(Me.Label3, 0, 1)
Me.TableLayoutPanel1.Controls.Add(Me.SubTotal_Label, 1, 0)
Me.TableLayoutPanel1.Controls.Add(Me.Label1, 0, 0)
Me.TableLayoutPanel1.Location = New System.Drawing.Point(67, 133)
Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
Me.TableLayoutPanel1.RowCount = 3
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
Me.TableLayoutPanel1.Size = New System.Drawing.Size(140, 57)
Me.TableLayoutPanel1.TabIndex = 9
'
'Total_Label
'
Me.Total_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Total_Label.AutoSize = True
Me.Total_Label.Location = New System.Drawing.Point(80, 41)
Me.Total_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Total_Label.Name = "Total_Label"
Me.Total_Label.Size = New System.Drawing.Size(39, 13)
Me.Total_Label.TabIndex = 5
Me.Total_Label.Text = "Label6"
'
'Label5
'
Me.Label5.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Label5.AutoSize = True
Me.Label5.Location = New System.Drawing.Point(10, 41)
Me.Label5.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Label5.Name = "Label5"
Me.Label5.Size = New System.Drawing.Size(31, 13)
Me.Label5.TabIndex = 4
Me.Label5.Text = "Total"
'
'Tax_Label
'
Me.Tax_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Tax_Label.AutoSize = True
Me.Tax_Label.Location = New System.Drawing.Point(80, 22)
Me.Tax_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Tax_Label.Name = "Tax_Label"
Me.Tax_Label.Size = New System.Drawing.Size(39, 13)
Me.Tax_Label.TabIndex = 3
Me.Tax_Label.Text = "Label4"
'
'Label3
'
Me.Label3.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(10, 22)
Me.Label3.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(25, 13)
Me.Label3.TabIndex = 2
Me.Label3.Text = "Tax"
'
'SubTotal_Label
'
Me.SubTotal_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.SubTotal_Label.AutoSize = True
Me.SubTotal_Label.Location = New System.Drawing.Point(80, 3)
Me.SubTotal_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.SubTotal_Label.Name = "SubTotal_Label"
Me.SubTotal_Label.Size = New System.Drawing.Size(39, 13)
Me.SubTotal_Label.TabIndex = 1
Me.SubTotal_Label.Text = "Label2"
'
'Label1
'
Me.Label1.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(10, 3)
Me.Label1.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(50, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "SubTotal"%0