• Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Programming Visual Basic Help Needed !!

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
I am using Microsoft Visual Studio 2005 and using the basic settings.

I need to know how I can only input the numbers 1 to 10 in a form of textbox. If I can't achieve this by a textbox then could you come up with a solution please?


What I need:

1 . I need the user to be able to only input the numbers 1 to 10.
2. If the user inputs anything other than the numbers then a msgbox will appear stating to only use the numbers.

P.S Step by step guide in how to achieve this please ^_^

Many Thanks


GT08
 
Joined
Jun 30, 2008
Messages
1,145 (0.20/day)
Location
England
System Name Wasleys PC
Processor Intel i5 2400 3.10GHz
Motherboard Asus P8z68-V
Cooling AC Freezer Pro
Memory Kingston 4GB (2x2GB) DDR3 Hyperx Memory
Video Card(s) HIS ATi 6850 1GB DDR3
Storage Seagate ST3500320AS 500GB Hard Drive SATA II 7200rpm *32MB Cache*
Case Antec 900 with mods
Audio Device(s) On Board
Power Supply OCZ Stealth Xtream 500W
Software Windows 7
I could find out by tommorow.

I'm currently learning "Visual Basics" at college and I have the lesson tommorow :).

I know you have to include ">" and "<". (I think)
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
You could use a NumericUpDown control. It allows you to set the min and max values, and the increment (in your case 1). It also allows the user to set the value manually, like a text box.
 
Joined
May 20, 2004
Messages
10,487 (1.45/day)
You could use the changed method and use

Integer.TryParse(yourtextbox.text, 1)

Returns true or false, ie if it's integer or not. If it is integer you can check if it's between 0 and 10 using < and >.
 
Joined
Dec 6, 2005
Messages
10,881 (1.63/day)
Location
Manchester, NH
System Name Senile
Processor I7-4790K@4.8 GHz 24/7
Motherboard MSI Z97-G45 Gaming
Cooling Be Quiet Pure Rock Air
Memory 16GB 4x4 G.Skill CAS9 2133 Sniper
Video Card(s) GIGABYTE Vega 64
Storage Samsung EVO 500GB / 8 Different WDs / QNAP TS-253 8GB NAS with 2x10Tb WD Blue
Display(s) 34" LG 34CB88-P 21:9 Curved UltraWide QHD (3440*1440) *FREE_SYNC*
Case Rosewill
Audio Device(s) Onboard + HD HDMI
Power Supply Corsair HX750
Mouse Logitech G5
Keyboard Corsair Strafe RGB & G610 Orion Red
Software Win 10
^ What he said.

Textbox1.OnEvent.LostFocus (sorry about the syntax)
- Do the integer validation
- If number <1 or >10, msgbox "Number must be an integer between 1 and 10"
Textbox1.GetFocus
 

Kreij

Senior Monkey Moderator
Joined
Feb 6, 2007
Messages
13,817 (2.21/day)
Location
Cheeseland (Wisconsin, USA)
While the Focus event works, I usually use the TextChanged event to check for valid characters as the user is typing them. That way you can do error checking on the fly and let the user know that they are inputting invalid values immediately when they type them.

There is more program overhead, but if it not a resourse hungry app it works fine.

Just my 2 cents.
 
Joined
Oct 23, 2008
Messages
37 (0.01/day)
Location
Sweden
System Name NoxBox
Processor AMD 1600X
Motherboard MSI B350 Tomahawk
Memory 16GB 3200mhz
Video Card(s) 1070Ti
Storage Samsung 512gb M2, WD Black 2TB
Power Supply Corsair 650w
Software W10 x64
Try Delphi, it's easier (for me).. ;D
But, i think its like Sasqui / Dan says. Use their way, i can't VB, but it's pretty much like Delphi. ^^
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I am using Microsoft Visual Studio 2005 and using the basic settings.

I need to know how I can only input the numbers 1 to 10 in a form of textbox. If I can't achieve this by a textbox then could you come up with a solution please?


What I need:

1 . I need the user to be able to only input the numbers 1 to 10.
2. If the user inputs anything other than the numbers then a msgbox will appear stating to only use the numbers.

P.S Step by step guide in how to achieve this please ^_^

Many Thanks


GT08
There's lots of ways to do this...

1. You could also use a MaskedTextBox but it relies more on digits than values (e.g. ## rather than a range 1-10). A MaskedTextBox would work great for 0-9, for instance.

2. NumericUpDown, as mentioned.

3. A TrackBar with read-only textbox displaying current value. Limit the range on the TrackBar to 1-10.

4. Plain old TextBox with value verificaton on TextChanged event, as mentioned.

5. TextBox + button where the button validates the input in the TextBox on click. I usually change the background color of the form control to pink to signify an error.


You can get creative as to how to actually display the error using labels next to the input form or you could even use tooltips that pop up when the mouse is hovering over it.


VB makes programmers lazy so I'll stress one thing VB doesn't: you have to cast. If you decide any of the methods above that involve TextBoxes, the value they store is of type string. In order to convert to string to byte (byte can represent a value of 0-255), use something like this:

Dim value As Byte = Convert.ToByte(TextBox1.Text)

Once you have a numeric expression of the value, you can test it for validity by conditionals. For example:

If (value >= 1) && (value <= 10) Then
' value is good
Else
' value is bad
End If

What you do in the if statement depends on which route you take to display the message.
 
Last edited:

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
Thank you for all of your replies. I have decided to opt for this following code but it does not seem to work at all. This is a little project of mine which I would like to perfect.

I have decided to use lbltotal2.Text = String.Empty. The total clears but then it encounters an error which says " Conversion from string "" to type 'Double' is not valid. "

Can you fellow programmers see any mistakes in this Visual Basic coding? Also how can I stop the error from coming up as these String.Empty applies to all 14 labels.

The code below is my VB code:


-----------------------------------------------------------------------------------------

Public Class Form1
Dim CheeseTomato, HamPineapple, Vegetarian, MeatFeast, Seafood As Decimal
Dim Cola, Lemonade, FizzyOrange As Decimal
Dim ExtraCheese, Pepperoni, Onions, Peppers As Decimal
Dim TotalCostPizza, TotalCostPizza2, TotalCostPizza3, TotalCostPizza4, TotalCostPizza5, TotalPizzas As Decimal
Dim TotalCostDrinks, TotalCostDrinks2, TotalCostDrinks3, TotalCostDrinks4, TotalDrinks, TotalBase, TotalBase2 As Decimal
Dim TotalCostToppings, TotalCostToppings2, TotalCostToppings3, TotalCostToppings4, TotalCostToppings5, TotalToppings As Decimal
Dim TotalCost As Decimal
Dim Order As Decimal

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub btnorder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnorder.Click
CheeseTomato = 3.5
HamPineapple = 4.2
Vegetarian = 5.2
MeatFeast = 5.8
Seafood = 5.6
Cola = 0.9
Lemonade = 0.8
FizzyOrange = 0.9
ExtraCheese = 0.5
Pepperoni = 0.5
Onions = 0.5
Peppers = 0.5
TotalCost = 0

If chkboxcat.Checked = True Then
TotalCostPizza = Val(3.5) * txtbox1.Text
End If



If chkboxhap.Checked = True Then
TotalCostPizza2 = Val(4.2) * txtbox2.Text


If chkboxVeg.Checked = True Then
TotalCostPizza3 = Val(5.2) * txtbox3.Text


If chkboxmfeast.Checked = True Then
TotalCostPizza4 = Val(5.8) * txtbox4.Text


If chkboxseafood.Checked = True Then
TotalCostPizza5 = Val(5.6) * txtbox5.Text
End If
End If
End If
End If

TotalPizzas = TotalCostPizza + TotalCostPizza2 + TotalCostPizza3 + TotalCostPizza4 + TotalCostPizza5
lbltotal.Text = Format(TotalPizzas, "ÂŁ#,##0.00")

If chkboxCola.Checked = True Then
TotalCostDrinks = Val(0.9) * txtbox6.Text

If chkboxlemonade.Checked = True Then
TotalCostDrinks2 = Val(0.8) * txtbox7.Text


If chkboxorange.Checked = True Then
TotalCostDrinks3 = Val(0.9) * txtbox8.Text

If chkboxnone.Checked = True Then
TotalCostDrinks4 = 0
End If
End If
End If
End If

TotalDrinks = TotalCostDrinks + TotalCostDrinks2 + TotalCostDrinks3 + TotalCostDrinks4
lbltotal2.Text = Format(TotalDrinks, "ÂŁ#,##0.00")
If chkboxtac.Checked = True Then
TotalBase = Val(0.0) * txtbox9.Text
If chkboxtrad.Checked = True Then
TotalBase2 = Val(0.0) * txtbox10.Text
End If
End If
TotalBase = TotalBase + TotalBase2

If chkboxcheese.Checked = True Then
TotalCostToppings = Val(0.5) * txtbox11.Text


If chkboxpproni.Checked = True Then
TotalCostToppings2 = Val(0.5) * txtbox12.Text


If chkboxonions.Checked = True Then
TotalCostToppings3 = Val(0.5) * txtbox13.Text


If chkboxpeppers.Checked = True Then
TotalCostToppings4 = Val(0.5) * txtbox14.Text


If chkboxnone.Checked = True Then
TotalCostToppings5 = 0
End If
End If
End If
End If
End If
TotalToppings = TotalCostToppings + TotalCostToppings2 + TotalCostToppings3 + TotalCostToppings4 + TotalCostToppings5
lbltotal3.Text = Format(TotalToppings, "ÂŁ#,##0.00")

TotalCost = TotalPizzas + TotalDrinks + TotalToppings
lblbill.Text = Format(TotalCost, "ÂŁ#,##0.00")
If txtbox1.Text > 10 Then
lbltotal.Text = String.Empty
lbltotal.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox2.Text > 10 Then
lbltotal.Text = String.Empty
lbltotal.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox3.Text > 10 Then
lbltotal.Text = String.Empty
lbltotal.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox4.Text > 10 Then
lbltotal.Text = String.Empty
lbltotal.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox5.Text > 10 Then
lbltotal.Text = String.Empty
lbltotal.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox6.Text > 10 Then
lbltotal2.Text = String.Empty
lbltotal2.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox7.Text > 10 Then
lbltotal2.Text = String.Empty
lbltotal2.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox8.Text > 10 Then
lbltotal2.Text = String.Empty
lbltotal2.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox9.Text > 10 Then
lbltotal4.Text = String.Empty
lbltotal4.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox10.Text > 10 Then
lbltotal.Text = String.Empty
lbltotal.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox11.Text > 10 Then
lbltotal3.Text = String.Empty
lbltotal3.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox12.Text > 10 Then
lbltotal3.Text = String.Empty
lbltotal3.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox13.Text > 10 Then
lbltotal3.Text = String.Empty
lbltotal3.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")

If txtbox14.Text > 10 Then
lbltotal3.Text = String.Empty
lbltotal3.Text = 0.0
lblbill.Text = String.Empty
lblbill.Text = 0.0
MsgBox("Must enter numbers between 1 to 10")
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End If
End Sub

Private Sub btnclose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnclose.Click
Me.Close()
End Sub
End Class
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I have decided to use lbltotal2.Text = String.Empty. The total clears but then it encounters an error which says " Conversion from string "" to type 'Double' is not valid. "

Instead of...
Code:
lbltotal2.Text = String.Empty
...do this...
Code:
lbltotal2.Text = ""


If that doesn't help, could you zip up the project folder (delete the "bin" and "obj" folders first) and attach it to your post?
 
Last edited:

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
Instead of...
Code:
lbltotal2.Text = String.Empty
...do this...
Code:
lbltotal2.Text = ""


If that doesn't help, could you zip up the project folder (delete the "bin" and "obj" folders first) and attach it to your post?


Doesn't seem to work. Here is my project I have uploaded with the "bin" and "obj" deleted.
 

Attachments

  • Food-Tech - Latest Version (Pizza).zip
    14.9 KB · Views: 427

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
What steps does it take to trigger the error?


Edit: Ah, it errors when all fields are empty on: "If txtbox1.Text > 10 Then"

This is because the conversion algorithms don't know what to make of a null value. The simple solution is to do...

Code:
If txtbox1.Text.Length > 0 Then
 ' Proceed as normal
Else
 ' There's nothing there to process
End If


And let me give you a tip to cut down on the repetitiveness of your code. You can write a sub like this to perform all those checks:

Code:
Private Function Validate(ByRef obj As TextBox) As Double
  If (obj.Text.Length > 0) Then
    Dim value As Double = Convert.ToDouble(obj.Text)
    If (value.Text  > 10) Then
      obj.BackColor = Color.Pink
      MsgBox("Value must be between 1 and 10.")
    Else
      obj.BackColor = Color.White
      Return value
    End If
  End If
End Function

Then to validate a control, you'd just have to do the following:
Code:
Validate(txtbox1)

You can play with that concept to make it how you like it. It cuts way down on the repetition. ;)
 
Last edited:

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
What steps does it take to trigger the error?


Edit: Ah, it errors when all fields are empty on: "If txtbox1.Text > 10 Then"

This is because the conversion algorithms don't know what to make of a null value. The simple solution is to do...

Code:
If txtbox1.Text.Length > 0 Then
 ' Proceed as normal
Else
 ' There's nothing there to process
End If

I'm afraid that this program I am making is not that simple.
Let me try to explain what I am trying to achieve.

It is a pizza restaurant. I want to make a software which can allow the waiters/waitresses to take orders from the table using a small handheld computer.

In the software I must allow the waiters to record the table number (1 to 25), and order up to a maximum of 10 of any item on the menu. A textbox should be provided for any additional requests.

As you can see I have already met my criteria of 'Table Numbers'.

I am trying to design it so that any order over 10 can not be processed through thus resulting the totals back to 0 along with a message box displaying the message "You can only order up to a maximum of 10 of any item". The total should only be reset back to 0 only on the particular textbox (if more than 10). The other textbox with numbers between 1 to 10 should carry on its calculation thus keeping its total price.

If the criteria is met between 1 to 10 then the total will appear.

Within my program I have made about 3 or 4 "lbltotal" along with "lblbill" which would be my grand total. "lbltotal" is like the subtotal or you can call it a breakdown.


Any other details you may want to hear more about, just tell me.


I hope to hear from you soon.

GT08
 

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
And let me give you a tip to cut down on the repetitiveness of your code. You can write a sub like this to perform all those checks:

Code:
Private Function Validate(ByRef obj As TextBox) As Double
  If (obj.Text.Length > 0) Then
    Dim value As Double = Convert.ToDouble(obj.Text)
    If (value.Text  > 10) Then
      obj.BackColor = Color.Pink
      MsgBox("Value must be between 1 and 10.")
    Else
      obj.BackColor = Color.White
      Return value
    End If
  End If
End Function

Then to validate a control, you'd just have to do the following:
Code:
Validate(txtbox1)

You can play with that concept to make it how you like it. It cuts way down on the repetition. ;)


Where would I put this code? Would it be in my form load or my txtbox1 or in my button (process order)?

As it says " 'Return' Statement in a sub or a set cannot return a value"
 

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
Damn, I realised I have to make my program count how many pizzas has been ordered and how many drinks ordered.


It will be something like this:

Number Of Pizzas Ordered: _____

Number Of Drinks Ordered: _____


I was intending to use "Val(txtbox1) + Val(txtbox2)" and so on but it comes up with an error.

I want to display the quantity of how many pizzas ordered.

How am I to achieve this?
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Where would I put this code? Would it be in my form load or my txtbox1 or in my button (process order)?

As it says " 'Return' Statement in a sub or a set cannot return a value"
The Private Function/End Function goes inside the Class/End Class tags.

The Validate() bit goes any place that you want the validation to occur (either in a TextChanged or button Click event).


How am I to achieve this?
Dim result As Integer = Convert.ToInt32(txtbox1.Text) + Convert.ToInt32(txtbox2.Text)



What I did some time ago on code like yours (CheckBox enable/disable + associated TextBox) is I made a validate function similar to the one above and also an enable/disable sub routine.

Something like...
Code:
Private Function Validate(ByRef checkbox As CheckBox, ByRef textbox As TextBox) As Boolean
  ' return true if valid and false if invalid
End Function
I put the range of values accepted in the TextBox.Tag object as like "1,10." Then in the Function, I split the two, used the first for min and the second for max. That way, one sub could take care of the validation of almost everything.


Additionally, I added events to the checkbox to disable/enable the associated textbox. Something like...

Code:
Private Sub SetAssocEnable(ByVal sender As Object, ByVal e As EventArgs) Handles chkbox1.CheckedChanged, chkbox2.CheckedChanged, etc...
  Dim chk As CheckBox = sender ' Cast the sender to what it is
  Dim txt As String = "txt" & chk.Name.Substring(3) ' generate the textbox name from the checkbox name
  If (chk.Checked) Then
    Me.Controls(txt).Text = chk.Tag ' Copy stored value from checkbox
    Me.Controls(txt).Enabled = True ' Enable the Textbox
  Else
    chk.Tag = Me.Controls(txt).Text ' Store value to the checkbox
    Me.Controls(txt).Text = "0" ' Null its value so it doesn't conflict with the total
    Me.Controls(txt).Enabled = False ' Disable the Textbox
  End If
End Sub

Note that to use this code, the check boxes and text boxes must have a similar name. For example:
CheckBox = chkSeaFood
TextBox = txtSeaFood

The code will chop off the chk (via Substring) from the CheckBox and add txt on to it giving it the full name for the Textbox to look up.
 
Last edited:

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
Thank you for your help. Could you check over my project please? What do you think?

I know within my code I have repeated the codes but this is the only way I know to make this program work. I am still new to this. I am always up for improvements to make this code a lot shorter whilst doing the same job.

I am still figuring out how I can count how many pizzas and drinks (quantity) has been ordered corresponding to the orders.
 

Attachments

  • Project - Latest.zip
    14.5 KB · Views: 418

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
Thank you for your help. Could you check over my project please? What do you think?
Considering this is for a PDA, I would make the form as small as possible. To do that, I would put a TableLayoutPanel in all of those group boxes then add the controls to the tables. That allows you to keep it neat and also auto-size controls. I would also try to make the entire form fit to a table panel for the same reason (autosize).


I know within my code I have repeated the codes but this is the only way I know to make this program work. I am still new to this. I am always up for improvements to make this code a lot shorter whilst doing the same job.
See above post. Doing that would remove a lot of the repetitiveness of your code.


I am still figuring out how I can count how many pizzas and drinks (quantity) has been ordered corresponding to the orders.
You need a variable in the background and += it. For instance...

Dim PizzaCount As Integer = 0
Dim DrinkCount As Integer = 0

Private Sub ... ' your function that needs to modify their values
PizzaCount += pizzasordered
DrinkCount += drinksordered
End Sub


+= is the same as:

myvar = myvar + othervar
 

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
The Private Function/End Function goes inside the Class/End Class tags.

The Validate() bit goes any place that you want the validation to occur (either in a TextChanged or button Click event).



Dim result As Integer = Convert.ToInt32(txtbox1.Text) + Convert.ToInt32(txtbox2.Text)



What I did some time ago on code like yours (CheckBox enable/disable + associated TextBox) is I made a validate function similar to the one above and also an enable/disable sub routine.

Something like...
Code:
Private Function Validate(ByRef checkbox As CheckBox, ByRef textbox As TextBox) As Boolean
  ' return true if valid and false if invalid
End Function
I put the range of values accepted in the TextBox.Tag object as like "1,10." Then in the Function, I split the two, used the first for min and the second for max. That way, one sub could take care of the validation of almost everything.


Additionally, I added events to the checkbox to disable/enable the associated textbox. Something like...

Code:
Private Sub SetAssocEnable(ByVal sender As Object, ByVal e As EventArgs) Handles chkbox1.CheckedChanged, chkbox2.CheckedChanged, etc...
  Dim chk As CheckBox = sender ' Cast the sender to what it is
  Dim txt As String = "txt" & chk.Name.Substring(3) ' generate the textbox name from the checkbox name
  If (chk.Checked) Then
    Me.Controls(txt).Text = chk.Tag ' Copy stored value from checkbox
    Me.Controls(txt).Enabled = True ' Enable the Textbox
  Else
    chk.Tag = Me.Controls(txt).Text ' Store value to the checkbox
    Me.Controls(txt).Text = "0" ' Null its value so it doesn't conflict with the total
    Me.Controls(txt).Enabled = False ' Disable the Textbox
  End If
End Sub

Note that to use this code, the check boxes and text boxes must have a similar name. For example:
CheckBox = chkSeaFood
TextBox = txtSeaFood

The code will chop off the chk (via Substring) from the CheckBox and add txt on to it giving it the full name for the Textbox to look up.


This seems rather tricky to understand. I do want to understand it but could you explain more in depth as to why and how these codes work. For example what is the chkbox.tag? How do I make that work?

I should be able to understand more if you could kindly incorporate that use of code to my project. That way I could compare that and my current project and see what the differences are.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
.Tag is a property basically for random use. It is an object so the programmer can decide how to use it. It is there to inexplicably associate extra data to the control (like min/max values). Every Control has it. You define it. If you write an array to it, make sure to read it back as an array. If you make it a string, process it as a string.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I did just the Pizza one for you but a lot of the stuff I added can be reused for the other parts...

This is coded in Visual Studio 2008 by the way so you'll probably have to add the VB files to your project. I doubt it whatever version you're using can open the solution/project files.


Try to avoid using MessageBox very often. It annoys the user. In the example, I changed it to a ToolTip which displays details of the error by hovering over the errored object.

Also, there is no need for a "Close/Exit" button unless you explicitly remove the "X" in the corner for whatever reason. Instinctively, people gravitate towards the "X" anyway because that's how you close 99% of applications in Windows.


Is this for a class or a business?
 

Attachments

  • vs9 example.zip
    20.6 KB · Views: 410
Last edited:

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
I did just the Pizza one for you but a lot of the stuff I added can be reused for the other parts...

This is coded in Visual Studio 2008 by the way so you'll probably have to add the VB files to your project. I doubt it whatever version you're using can open the solution/project files.


Try to avoid using MessageBox very often. It annoys the user. In the example, I changed it to a ToolTip which displays details of the error by hovering over the errored object.

Also, there is no need for a "Close/Exit" button unless you explicitly remove the "X" in the corner for whatever reason. Instinctively, people gravitate towards the "X" anyway because that's how you close 99% of applications in Windows.


Is this for a class or a business?


Thanks for your reply. This is a class work where we are just practising programming skills by researching and using the scenarios.

Here is the program I have tried to create by using your technique.

I can't seem to apply it to all of it. How should the correct code be like?

This is quite an intriguing way to program.
 

Attachments

  • Food-Tech - Latest Version (Pizza) 2.zip
    32.1 KB · Views: 460

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
I'd get rid of the Extra Toppings -> None. The only reason why you'd need that is if the rest of them were Radio controls (where you can only select one). "None" is the equivilent to having none of those boxes checked.


Yeah, that is much better.

Some bugs...
Change Trad to Trads on this line. The controls are named Trads: Dim PizzaBase As String() = {"TAC", "Trad"}

Still trying to figure out why txtCola and txtLemonade are erroring...


Edit: I just ended up deleting those two controls and readding them. That made those two errors go away assuming you're getting them.

Before you are done, you should go through and set the order of tab stops.


I don't see any other problems besides that...

Oh, your Table Number combo box is missing?

And the Form.Text is "0.50"
 
Last edited:

GT08

New Member
Joined
Nov 11, 2008
Messages
18 (0.00/day)
I'd get rid of the Extra Toppings -> None. The only reason why you'd need that is if the rest of them were Radio controls (where you can only select one). "None" is the equivilent to having none of those boxes checked.


Yeah, that is much better.

Some bugs...
Change Trad to Trads on this line. The controls are named Trads: Dim PizzaBase As String() = {"TAC", "Trad"}

Still trying to figure out why txtCola and txtLemonade are erroring...


Edit: I just ended up deleting those two controls and readding them. That made those two errors go away assuming you're getting them.

Before you are done, you should go through and set the order of tab stops.


I don't see any other problems besides that...

Oh, your Table Number combo box is missing?

And the Form.Text is "0.50"

Thank you for the reply.

How do I get rid of this error?

"Name 'txtCola' is not declared." Line 36 Column 26
"Name 'txtLemonade' is not declared." Line 37 Column 26

ControlsValidate(txtCola) <---- Highlights the 'txtCola'
ControlsValidate(txtLemonade) <---- Highlights the 'txtLemonade'

Also what do you mean by "Edit: I just ended up deleting those two controls and readding them. That made those two errors go away assuming you're getting them."

What two controls? How do I get to it?


Lastly "Before you are done, you should go through and set the order of tab stops."

How do I set the order of tab stops? What good is this? (is it good programming?)


Thanks again for taking your time.
 

FordGT90Concept

"I go fast!1!11!1!"
Joined
Oct 13, 2008
Messages
26,259 (4.65/day)
Location
IA, USA
System Name BY-2021
Processor AMD Ryzen 7 5800X (65w eco profile)
Motherboard MSI B550 Gaming Plus
Cooling Scythe Mugen (rev 5)
Memory 2 x Kingston HyperX DDR4-3200 32 GiB
Video Card(s) AMD Radeon RX 7900 XT
Storage Samsung 980 Pro, Seagate Exos X20 TB 7200 RPM
Display(s) Nixeus NX-EDG274K (3840x2160@144 DP) + Samsung SyncMaster 906BW (1440x900@60 HDMI-DVI)
Case Coolermaster HAF 932 w/ USB 3.0 5.25" bay + USB 3.2 (A+C) 3.5" bay
Audio Device(s) Realtek ALC1150, Micca OriGen+
Power Supply Enermax Platimax 850w
Mouse Nixeus REVEL-X
Keyboard Tesoro Excalibur
Software Windows 10 Home 64-bit
Benchmark Scores Faster than the tortoise; slower than the hare.
How do I get rid of this error?

"Name 'txtCola' is not declared." Line 36 Column 26
"Name 'txtLemonade' is not declared." Line 37 Column 26

ControlsValidate(txtCola) <---- Highlights the 'txtCola'
ControlsValidate(txtLemonade) <---- Highlights the 'txtLemonade'

Also what do you mean by "Edit: I just ended up deleting those two controls and readding them. That made those two errors go away assuming you're getting them."

What two controls? How do I get to it?
1. On the form, select txtCola (it is the input form for Cola).
2. Once it is selected, hit delete.
3. Rinse and repeat for txtLemonade.
4. Now that they are both deletected, select one of the other txt##### controls like the one right below where txtLemonade used to be.
5. Right click on it and select "Copy."
6. Paste it into one of the open spaces. If it refuses to go in, simply drag and drop it in.
7. Repeat for the other one.
8. Now just rename them to what they are supposed to be: top one "txtCola" and bottom one "txtLemonade."

VS messed up some how on them with designer code.


Lastly "Before you are done, you should go through and set the order of tab stops."

How do I set the order of tab stops? What good is this? (is it good programming?)
Click on the control you want to check/change and alter TabStop property. The values start at 1 and work up.

The TabStop is the order in which the tab button proceeds through the form. For instance, if you have 3 controls with tab stop 1, 2, 3 in order while you currently have focus on 1, should you press tab, it will go to 2. If you press tab again, it will go to 3. If you press tab again, it will go back to 1.

This feature is mostly for people that will be using your program without a mouse. You can easily test it on your compiled application to see if the order is good.
 
Top