✅ Help Validating user inputs into Text Boxes
I am currently a student in a C# class we have a shipping Calculator project due and I am having problems making sure that only North, South, East, and West is input into the zone text box please help? I am also having to use a switch {case...} for determining the cost for the shipment, If i could possible get some help with that as well that would be great.
237 Replies
My class uses visual studio if you would need to have that information.
$details
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
So I don't think my code will help at this time because I think a majority of it is incorrect for what I need to do for this project is there maybe some other way I can get you the info you need. I can upload my assignment sheet or maybe get you in a voice chat? Here is the code I have anyways https://paste.mod.gg/ncpvtydszvvu/0
BlazeBin - ncpvtydszvvu
A tool for sharing your source code with the world!
so
you have a textbox
and you want to limit the user to only inputting "North", "South", "East", and "West"?
correct
VALIDATION: Validate that the package weight entered is numeric and greater than zero.
Validate that a non-blank shipping zone letter code was entered. If the user entered a shipping zone letter code other
than the valid codes mentioned below, then display an error message indicating this to the user, and do not calculate the
shipping costs.
this is the exact wording the professor used
ah
so
no
you do not want to limit the user to only inputting "North", "South", "East", and "West"
you want to limit the user to only inputting some "zone letter code" values
what are the valid "zone letter code"s
N S E W
also, that doesn't specify use of a "textbox" anywhere
don't use a textbox
unless that's specified somewhere else in the problem
INPUT: Create text boxes to accept the input of a package’s weight and
another text box to accept the input of a shipping zone letter code (assume shipping zone letter codes of N, S, E, W) with
zone rates as described below.
use a dropdown selector or radio buttons
okay
so
Validate that a non-blank shipping zone letter code was entered
If the user entered a shipping zone letter code other than the valid codes mentioned below, then display an error message indicating this to the userso, what are you having trouble with?
If I am going to be honest most everything with this course so far. I can look at code and understand what things are going to do but I am having a hard time determining where code needs to go. I understand what I am needing to do. Just do not know how to execute it.
cracks knuckles
where do you want to start?
I guess with my first bit i have to do. which is that input section you saw
the shipping zone bit?
https://gyazo.com/c993cd56f05557b2b3f306049ae334cb this is my form for reference
Gyazo
Gyazo
lovely
aha
AND you have a calculate button
that is very telling
yes making sure i can validate the letter codes
so
A) you need input controls
which you have
B) you need to DO something with them
when are we going to DO things?
when text is added into the text box?
or when the calculate button is pressed?
does the problem specify?
if not, what makes more sense to you?
it does not specify but for me it would be when the calculate button is pressed
I would agree
if you're going to HAVE a calculate button, that indicates to the user that they're responsible for making things happen
you definitely COULD have things automatically update whenever text is entered
but then what's the point of having a button at all?
so, now our job is "write code to run when the button is clicked"
exactly i think i have been doing that in line 28
assuming that method is attached to the
Click
event of the calculate button, yes
the other handlers you can get rid of
now, what do we need to DO within that method?the shipzonetb_ttextchanged and the packwght tb_text changed?
you wanted to start with the shipping zone input?
yes, we've just established you don't want to do anything when text changes
okay one sec
I am getting errors
what do they say?
rather, what do you think they're telling you?
what does that text mean?
that there was a function given to those events and the code no longer has the event trigger
roughly correct
you have removed the methods
you did not remove other code that refers to those methods
the code that actually attaches those methods to the textbox events
double-click on the error to see
ahh okay we have not got to the designer yet in class
I mean
you're using it
so, yeah, you have
you're getting a peek into what the Designer DOES for you, which is that it just generates C# code
don't edit that file
but that file didn't come from nowhere
if the Designer generated a reference to a method that doesn't exist, that's becuase you told it to
go into the Designer and remove the thing that tells it to do that
awesome okay got it
The total shipping cost amount is equal to the ‘weight cost’ plus the ‘zone cost’ and is calculated as follows:
$18.00 PER POUND ‘weight cost’ PLUS:
$27.00 ‘zone cost’ FLAT surcharge if shipped to zone N
$36.00 ‘zone cost’ FLAT surcharge if shipped to zone S
$45.00 ‘zone cost’ FLAT surcharge if shipped to zone E
$54.00 ‘zone cost’ FLAT surcharge if shipped to zone W
this is also how he wants us to price out things figured i would give you this as well since it will be relevant
I saw
what are we working on right now?
I would say lets start with the Shipping zone code and making sure that the correct zone cost is put in
great
what do we need to do with the shipping zone code?
ensure that the N S E W are put into the Shipping zone TB
what does our code need to do in order to accomplish that
read the text box for that text and if it does not read any of those zone codes then display to enter a valid code
read the text box for that textwhat's the code for this?
would that be a .Parse?
which .Parse()?
.Parse(ShipZoneTB.text)?
what are you calling
.Parse()
on?
there's a lot of different .Parse()
methods out there
which one are you talking about?ahhhh So i would start with north so maybe zoneN = ___.Parse(ShipZoneTB.Text)?
uhm
no
also no
dang
you're getting farther and farther from the actual question
read the text box for that texthow do you read text from a text box?
decimal.Parse(ShipZoneTB.Text)
I feel like that is wrong
cause that would be a number no?
it would
I mean
it's also kinda right
int?
but yeah, why are you parsing the text into a decimal?
do we WANT a decimal?
no we want a letter
Isletter.Parse?
why are you so stuck on a Parse method?
read the text box for that textwhere in here are we discussing parsing text?
I am not sure I apologize
nothing to apologize for
so, next step
if it does not read any of those zone codeswhat's the code we need for this?
I am not sure exactly.
well, the word "if" in that sentence should be your biggest clue
okay so if (ShipZoneTB.Text
you can do that, if you want
or you can use the variable, like I demonstrated above
if (shipZoneCode
since we're going to be checking the text value multiple times, probably better to use a reusable vairable
and what are we checking
shipZoneCode
for?if (shipZoneCode is "N" etc?
try it
says cannot implicitly conver type 'string' to system windows form tb
on what code?
if (shipZoneCode is "n");
what is
shipZoneCode
?ShipZoneTB?
why?
is that what we said we needed?
a reference to the entire textbox?
no just the text inside it
so, what is the code you wrote to do that?
I don't think I have written that yet?
so... do it
I apologize for taking up an hour of your time. I seem to not be able to understand what is needed. I will speak with my professor about it when I see him next. Thank you giving me your insight though it meant a lot.
I'm just chillin' working on other stuff
you're not imposing at all, that's what I'm here for
I'm good to keep going if you are
recap:
Validate that a non-blank shipping zone letter code was entered
If the user entered a shipping zone letter code other than the valid codes mentioned below, then display an error message indicating this to the userthis is what we need to do which you then distilled down to this
read the text box for that text and if it does not read any of those zone codes then display to enter a valid codethen we identified the first stop of that, and wrote code for it
read the text box for that textnow, we're working on the second step
if it does not read any of those zone codesfor which you wrote you said that was giving you an error, and then you told me you never added the first bit of code that we wrote to your actual program you need to do that
okay I am good to keep going
I have added the var shipZoneCode = ShipZoneTB.text;
this is the error code it is giving me
on what line of code?
if (shipZoneCode is "n")
and what is
shipZoneCode
?var shipZoneCode = ShipZoneTB;
so
when you said
you in fact did not do this
you have defined a variable
shipZoneCode
and given a value of ShipZoneTB
I.E.
a reference to the entire texbox
then, you are attempting to compare that TextBox
object against a string
literal
and the compiler correctly tells you that's nonsense
there is no automatic conversion between those typesso i need to define other variables?
no, you need to take the line of code that we wrote here in Discord and put it in your program
correctly
just so i am clear the var needs to be changed correct?
no
what is the difference between these two lines?
this?
the first looks at the text as the other looks at the box
gotcha
got it no error
I have added if statements for all the directions into the code
let's see
BlazeBin - zctrgqycjuwc
A tool for sharing your source code with the world!
So now i need to add the is not one of those to have a message box that says please use one of them
how do you want to display that message to the user?
would that be an else statement?
that would be one way to accomplish it
doesn't fit with what you've written so far
okay what do you recommend?
I'm going to recommend that you do what makes sense to you
what makes sense to me would be to have a message box pop up saying "Please enter a valid Zone Code"
great
let's do that
what's the code to do that?
MessageBox.Show("Please Enter A Valid Zone Code");
looks good to me
now, where do we put that?
I.E. when do we want that to run?
when the calc button is clicked
I have it right now under my if statements
when the button is clicked, sure, but ALWAYS when the button is clicked?
i see why you asked that question cause it still popped up
hmm
you only want it to run when the ship code isn't one of the 4 acceptable values, yes?
yes
so, we need to put it somewhere the only runs when that is the case
I.E. we need the program to make a decision about when to run it
hmmm I am unsure maybe after the button is clicked but after it checks for the shipZoneCode?
how do we make decisions in code?
else MessageBox.Show("Please Enter A Valid Zone Code");?
no that is not correct
but that is displaying 4 of the message boxes
indeed
that's what you told it to do
"if the code isn't n, display a message box"
"if the code isn't s, display a message box"
"if the code isn't e, display a message box"
"if the code isn't w, display a message box"
so i need to do is not one of those display the message box
uhmm
rephrase, please?
is the text is no NSEW MessageBox.Show("Please Enter A Valid Zone Code");
yeah
if the code is NONE of those
I.E. if ALL of those statements are true
"if the code isn't n AND the code isn't s AND ..."
it says the "not pattern" is not available in C# 7.3 please use language version 9.0 or greater
if (shipZoneCode is not "n"(MessageBox.Show("Please Enter A Valid Zone Code")); this is what I was trying to do
I am not sure where to put this message box
i found this "&&" I feel like this needs to be in this code
yeah, you can't use
is not
in C# 7.3
use !=
that is how you would combine conditions, to check whether all of them are true at once, definitelyokay so these would be more if statements?
or would this be an else
both are possible
if you were going to do it as a single if statement, what would that look like?
any idea?
else if (shipZoneCode != " " )
{
MessageBox.Show ("Please enter a valid zone code")
}
?
that looks like an
else if
to me
not "a single if statement"if (shipZoneCode != "n" )
{
MessageBox.Show ("Please enter a valid zone code")
}
okay, but you already have that
and it didn't work
we were talking about how to check that the code is NONE of the 4 valid values
in a single if statement
I dont know how i would do that
Hint:
&&
less-obvious hint: you already mentioned it earlier
I am guessing you mean &&
indeed
if (shipZoneCode != "n" && "s" && "e" && "w")?
{
MessageBox.Show ("Please enter a valid zone code")
}
Almost
else?
&&
is a boolean operator
It works on booleans
bool && bool
shipZoneCode != "n"
will resolve to a boolean
But "s"
is a stringAlternatively, look at the error that gives you. It tells you that you can't convert a string to a bool, yes? That's meaningful, it tells you that you're passing a string to something that expects a bool.
okay i see that error what should i do to fix it?
don't pass a string to something that expects a boolean
consider what Z said about how
&&
works
and consider what you're trying to do here
you're trying to identify when the code does not meet certain conditions
what are those conditions?when the text entered is not NSEW
how many conditions is that?
4
write the code for each one
Alternatively alternatively, pattern matching:
we already covered that
not available in C# 7.3
But chances are your teacher won't know wtf it is and will fail you
Ah oof my condolences
I am sorry I am very lost about how to code this
How do you write "shipZoneCode is not n"?
if (shipZoneCode != "n")
Just the condition
Without the if
now how do you write "shipZoneCode is not s"?
shipZoneCode != "s"
And now, how do you write a condition that both must be true?
given that
&&
combines conditions
and you have just written 2 conditionsshipZoneCode != "n" && shipZoneCode != "s" && shipZoneCode != "e" && shipZoneCode != "w"
🎉
🎉
HAHAHAHAHAHAA
how does the line end?
https://gyazo.com/9b81fe37835d1f1cec9dcdabb993a457 this is what I am seeing
Gyazo
Gyazo
I dunno, what do you want to do with that
You don't need that last &&
Unless you want to chain another condition
it wasnt actually there that was just intellisense
you have written a boolean expression that evaluates to
true
when shipZoneCode
is none of the valid values you want
why were you writing this expression in the first place?to ensure that a valid zone code was entered
why do you want to know that?
So that the correct price can be used in the cost
when did we start talking about prices and costs?
we were trying to display a message box
in particular
if you were going to do it as a single if statement, what would that look like?you were trying to write an if statement so take that expression and put it in an if statement
if (shipZoneCode != "n" && shipZoneCode != "s" && shipZoneCode != "e" && shipZoneCode != "w")
{
MessageBox.Show("Please Enter a Valid Zone Code.");
}
IT WORKSSSSSSSSSSSS
🎉
Okay so I am doing the math portion of this now should i make another help post or can we just do it in here?
Here's fine I think
BlazeBin - ijsgxvdsaonh
A tool for sharing your source code with the world!
this is what i got so far
seems like my costpp is incorrect
Right, so, what's wrong with your code?
Why do you think it's incorrect?
seems like it is multiplying 18 * 18
Only if
packWghtTB.Text
is "18"
oh okay so Cost per pound is working fine and showing the correct value in the string my zone cost is dramatically higher though showing$702
So, did you mean to do this?
Assuming user input here for
costpp
is 10
, we're looking at
i need it to take that cost per pound which is 18 * weight entered in package weight tb + the zone cost
Well that's not what you're doing, then
To start with, you set
costpp
to your packWghtTB
should i make that a var?
like i did the shipZoneCode
Well, yeah, you probably do want to store the weight in a variable
okay done
And now you can do your calculations
okay so what do i need to change about those calculations?
get rid of the ()
You can, sure
Order of operations will ensure proper order anyway
Definitely don't reassign the variables that are, from what I can see, supposed to be constant
Like,
costE
is not supposed to change, is it?no
I'd even encourage you to declare those variables as actual constant fields
this is what I am getting now
Well, yeah, constants cannot change
We already established that we don't want to change those values, right?
Same goes for
costpp
We don't want to reassign that value
Rather, you want some new variable named, say total
That total
would be costpp * weight
Then, depending on the zone selected, you would add the zone cost to it?
op i forgot to change costpp
Why so many different costs?
Or rather, so many different totals?
There will be just one total, no?
One result
it will display total cost per pound, Total zone cost , and total shipping cost capped at $100
this is what my form looks like
Yeah, it will display totals
Based on the selected zone
Not totals for each zone
So why have totals for each zone?
you are right seems wrong
okay now my zone cost is showing correctly but my cost per pound is high
this is what i have now
Yeah
So, start with
totalcost = 18 * costpp
That's it
Don't reassign to costpp
Don't make multiple costs
Just oneWhy start with
totalcost = 0
?
decimal totalcost = 18 * costpp
is enough
Then, since the total cost has to be dependent on the zone...you have this part in your code
Perfect for changing stuff depending on the zone, is it not?
so like thise
?
Yep
Then, depending on the zone, add that cost
If the zone is
n
, add costN
to totalcost
And so ondecimal in front right
Why?
You'll be reassigning to the totalcost, right?
totalcost += costN
for example
Or totalcost = totalcost + costN
if you're yet unfamiliar with +=
?
Yeah, but you don't want to always do that, do you?
Depending on the zone
If zone is
n
you want to add costN
If zone is s
you want to add costS
Angius
you have this part in your code
Quoted by
<@85903769203642368> from #Help Validating user inputs into Text Boxes (click here)
React with ❌ to remove this embed.
I am very confused now haha
Pseudocode:
ye
so it is still not showing my cost per pound correctly
How do you display it?
Well, makes sense
totalcost
is cost per pund + zone cost
Cost per pound is just costpp * weight
While the zone cost... depends on the zone
So, again, look into those ifscan i get a hint?
👍
then delete this right
Well, the last four lines from here, yes
this is what it is showing now
I will be back in a little bit I apologize
Sure
Well
I'm headed to bed now, so I'll be able to pick it up in some time
Perhaps someone else will chip in
it's over 400 msgs, maybe a recap? :/
You could replace that
textbox
by a Combobox
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.