techPowerUp! Forums

Go Back   techPowerUp! Forums > Software > Programming & Webmastering

Reply
 
Thread Tools
Old Nov 18, 2008, 11:15 AM   #1
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

Programming Visual Basic Help Needed !!

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
GT08 is offline  
Reply With Quote
Old Nov 18, 2008, 11:36 AM   #2
Wozzer
500 Posts
 
Join Date: Jun 2008
Location: England
Posts: 990 (0.55/day)
Thanks: 166
Thanked 76 Times in 72 Posts
Send a message via MSN to Wozzer

System Specs

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)
Wozzer is offline  
Reply With Quote
Old Nov 18, 2008, 01:17 PM   #3
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Nov 18, 2008, 01:52 PM   #4
DanTheBanjoman
Seņor Moderator
 
DanTheBanjoman's Avatar
 
Join Date: May 2004
Location: Utrecht, Utrecht, The kingdom of the Netherlands
Posts: 8,498 (2.58/day)
Thanks: 41
Thanked 1,453 Times in 1,077 Posts
Send a message via ICQ to DanTheBanjoman Send a message via MSN to DanTheBanjoman

System Specs

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 >.
DanTheBanjoman is offline  
Reply With Quote
Old Nov 18, 2008, 01:56 PM   #5
Sasqui
Eligible for custom title
 
Sasqui's Avatar
 
Join Date: Dec 2005
Location: Manchester, NH
Posts: 6,066 (2.23/day)
Thanks: 827
Thanked 913 Times in 746 Posts

System Specs

^ 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
Sasqui is offline  
Reply With Quote
Old Nov 18, 2008, 06:07 PM   #6
Kreij
Hardcore Monkey Moderator
 
Kreij's Avatar
 
Join Date: Feb 2007
Location: Cheeseland (Wisconsin, USA)
Posts: 12,126 (5.27/day)
Thanks: 591
Thanked 5,494 Times in 2,938 Posts

System Specs

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.
__________________

Cloud (noun, singular): A dynamic arrangement of multiple potential single points of failure, with a user at one end and their data at the other.


Get more tech news on a wide variety of topics at NextPowerUp
Kreij is offline  
Reply With Quote
Old Nov 18, 2008, 06:25 PM   #7
Noxman
5 Posts
 
Noxman's Avatar
 
Join Date: Oct 2008
Location: Sweden
Posts: 22 (0.01/day)
Thanks: 0
Thanked 4 Times in 4 Posts

System Specs

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. ^^
Noxman is offline  
Reply With Quote
Old Nov 18, 2008, 10:07 PM   #8
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

Quote:
Originally Posted by GT08 View Post
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.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }

Last edited by FordGT90Concept; Nov 18, 2008 at 10:14 PM.
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
Old Nov 20, 2008, 04:25 PM   #9
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

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
GT08 is offline  
Reply With Quote
Old Nov 21, 2008, 01:37 AM   #10
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

Quote:
Originally Posted by GT08 View Post
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?
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }

Last edited by FordGT90Concept; Nov 21, 2008 at 01:47 AM.
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
Old Nov 21, 2008, 08:18 AM   #11
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

Quote:
Originally Posted by FordGT90Concept View Post
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.
Attached Files
File Type: zip Food-Tech - Latest Version (Pizza).zip (14.9 KB, 226 views)
GT08 is offline  
Reply With Quote
Old Nov 21, 2008, 08:41 AM   #12
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

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.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }

Last edited by FordGT90Concept; Nov 21, 2008 at 08:56 AM.
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Nov 21, 2008, 09:03 AM   #13
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

Quote:
Originally Posted by FordGT90Concept View Post
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 is offline  
Reply With Quote
Old Nov 21, 2008, 09:08 AM   #14
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

Quote:
Originally Posted by FordGT90Concept View Post

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 is offline  
Reply With Quote
Old Nov 21, 2008, 09:35 AM   #15
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

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?
GT08 is offline  
Reply With Quote
Old Nov 21, 2008, 09:41 AM   #16
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

Quote:
Originally Posted by GT08 View Post
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).


Quote:
Originally Posted by GT08 View Post
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.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }

Last edited by FordGT90Concept; Nov 21, 2008 at 10:15 AM.
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
Old Nov 21, 2008, 10:22 AM   #17
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

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.
Attached Files
File Type: zip Project - Latest.zip (14.5 KB, 229 views)
GT08 is offline  
Reply With Quote
Old Nov 21, 2008, 11:02 AM   #18
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

Quote:
Originally Posted by GT08 View Post
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).


Quote:
Originally Posted by GT08 View Post
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.


Quote:
Originally Posted by GT08 View Post
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
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
Old Nov 21, 2008, 11:32 AM   #19
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

Quote:
Originally Posted by FordGT90Concept View Post
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.
GT08 is offline  
Reply With Quote
Old Nov 21, 2008, 12:51 PM   #20
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

.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.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
Old Nov 21, 2008, 02:06 PM   #21
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

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?
Attached Files
File Type: zip vs9 example.zip (20.6 KB, 214 views)
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }

Last edited by FordGT90Concept; Nov 21, 2008 at 02:23 PM.
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Old Nov 21, 2008, 05:48 PM   #22
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

Quote:
Originally Posted by FordGT90Concept View Post
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.
Attached Files
File Type: zip Food-Tech - Latest Version (Pizza) 2.zip (32.1 KB, 224 views)
GT08 is offline  
Reply With Quote
Old Nov 22, 2008, 12:30 AM   #23
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

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"
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }

Last edited by FordGT90Concept; Nov 22, 2008 at 12:39 AM.
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
Old Nov 23, 2008, 09:47 PM   #24
GT08
5 Posts
 
Join Date: Nov 2008
Posts: 18 (0.01/day)
Thanks: 7
Thanked 0 Times in 0 Posts

Quote:
Originally Posted by FordGT90Concept View Post
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.
GT08 is offline  
Reply With Quote
Old Nov 24, 2008, 03:06 AM   #25
FordGT90Concept
"I go fast!1!11!1!"
 
FordGT90Concept's Avatar
 
Join Date: Oct 2008
Location: IA, USA
Posts: 10,583 (6.28/day)
Thanks: 1,755
Thanked 2,599 Times in 1,962 Posts

System Specs

Quote:
Originally Posted by GT08 View Post
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.


Quote:
Originally Posted by GT08 View Post
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.
__________________
Golden Rule of Programming: Never assume.

try { SteamDownload(); }
catch (Steamception ex) { RageQuit(); }
FordGT90Concept is offline  
Crunching for Team TPU
Reply With Quote
The Following User Says Thank You to FordGT90Concept For This Useful Post:
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the older visual basic DrPepper Programming & Webmastering 28 Sep 26, 2008 11:47 PM
Visual Basic : Build a SIMPLE calculator Dark_Webster Programming & Webmastering 22 Aug 16, 2008 02:14 PM
General Programming FAQ Kreij Programming & Webmastering 2 Jun 8, 2008 07:39 PM
Visual Studio 2008, .NET 3.5 Reach Beta 2; Silverlight 1.0 Hits RC HellasVagabond General Software 0 Jul 26, 2007 08:26 PM
BF2142 Stats Site 1Strive Games 15 Nov 3, 2006 06:55 PM


All times are GMT. The time now is 01:11 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
no new posts