I wondering of anyone can help. I have a homework and a questien in it i dont understand what i need to look for to figuer out how to find it.
This is what i need to look for
Convert the contents of the price TextBox to a double and
validate the converted value so it is >= 0.0.
Call the GetDouble method from the InputUtility class,
with min value 0 and max value some big number.
Use a const declaration for the max.value
this is what im going to get in to my if before im going to tell its true
can enter the code two
private bool ReadAndValidatePrice(out double price)
{
if
{
price = txtPrice.Text;
return true;
}
else
{
MessageBox.Show("Enter Numbers Only", "Invalid Character", MessageBoxButtons.OK, MessageBoxIcon.Error);
txtPrice.Focus();
return false;
What i fink im going to do is if it is 0.0 or bigger it going to type it out. im i going to do like 0.0 > 999999
This is the best I can do with the little information you are presenting. You don't have to declare a double and save the converted data to it, but it makes it easier to read.
double converted;
converted = Convert.ToDouble(priceTB.text)
// Check if the converted data and if is less than 0, return out of the method
if(converted >= 0.0)
return;
Related
Hey guys I wanted to make a richtextbox that only supports numbers and cant go above, 500 for example.
how would I go by doing that? thanks
I would use the keydown event to check if the pressed key is one of the keys you allow. With numbers it is pretty simple, maybe add ',' and '.' or other characters of your choice.
I'm not sure about the specifics but you can add something like
myRichTextBox.OnTextChanged() {
int number = 0;
bool checkInt = Int32.TryParse(myRichTextBox.Text, out number); //this checks if the value is int and stores as true or false, it stores the integer value in variable "number"
if ( checkInt = true && number > 500 ) //check if value in textbox is integer
{
myRichTextBox.Text = number.ToString();
}
else
{
DialogBox.Show("Please Enter Numbers Only");
myRichTextBox.Text = "";
}
}
You probably have to read the Int32.TryParse usage but brushing up this code should do what you want.
You can also put this code in a button onclick method to check that the value in textbox is integer before using the text.
Recently I've been told to change one validation towards a database to a new database in a C# application. Instead of the old database I would be using the new one.
The problem is that I can't run the app. I need to publish it so the guys that uses it give me their feedback.
So I decided to share this part of the code that I added.
Not changed, but added. This is something new and a potential thing that can go wrong.
Hopefully someone with more experience will tell me how much my code sucks or if it looks OK.
Thing is... the old database has an int value in a column and the new one has a nvarchar(5).
So I made this to convert the old one in a new one.
string ConvertIDToReg(string kairosID)
{
double n;
if (!Double.TryParse(Convert.ToString(kairosID),
System.Globalization.NumberStyles.Any,
System.Globalization.NumberFormatInfo.InvariantInfo, out n))
{
return "0";
}
char[] regID = kairosID.ToCharArray();
if (regID.Length > 4)
{
return "0";
}
if (reg.Length == 3)
{
regID[4] = regID[3];
regID[3] = regID[2];
regID[2] = regID[1];
regID[1] = regID[0];
regID[0] = "0";
}
return regID.ToString();
}
This is how it should work:
The old ID is something like "1234" but the new one is a 5 char max ID with a 0 in the beginning like "01234" (if the 5th number is not occupied). So basically I want to be able to put a 0 in the beginning if there isn't a 5th number.
If the number exceeds the 5 digits I want to return a 0 as the whole string (this will be handled later)
If the number is not a number (i.e "123ABC") return a 0 all the same.
Should this compile or even work?
What about efficiency? This will run several times. Any help to make this faster will do.
No, this won't compile. You misspelled your second regID (forgot the 'ID'), and are assigning a string to a char at regID[0] = "0";
Change that and it will compile, then blow up when you run it, when regID.Length= 3, because you're trying to access index 3 and 4, which it clearly will not have.
This should do what you're wanting:
string ConvertIDToReg(string kairosID)
{
if (kairosID.Length > 4 || kairosID.Any(c => !char.IsDigit(c)))
return "0";
return kairosID.PadLeft(5, '0');
}
if it's longer than 4 characters or if any character is not a digit, return a zero as a string ("0"). Else return the ID padded to 5 digits. About as simple as i can make it I think. No need to parse it as an int even.
This code should work:
private string ConvertIntToChar(string numValue)
{
int result = 0;
if (int.TryParse(numValue, out result) == false || numValue.Length > 4)
{
return "0";
}
return numValue.PadLeft(5, '0');
}
If value is not of 4 characters then it will add number of "0" required to make the string length equal to 5.
If the string is always some form of an integer i would approach the problem as such. and you wanted to keep every thing else the same. the "D5" tells the ToString method to 0 pad the string so that it is always 5 digits but retains the same numerical value.
string ConvertIDToReg(string kairosID)
{
int id;
if (!Int32.TryParse(kairosID, out id) || id > 9999)
{
return "0";
}
return id.ToString("D5");
}
I am trying to create a form that will convert Celsius to Fahrenheit and vice versa, but I am running into an error with the Conversion buttons code. The problem I am getting is that the variable convertedTemperature is left empty, so that should validation fail the message will be displayed via the ouput label(lblConvertedTemperature). But because of this being left blank I get the use of unassigned variable error. My question is how can I reword the processing section to not cause this error.
Double inputTemperature;// the variable that will store txtTemperatureInput for calculation
Double convertedTemperature;// the variable that will store the converted temperature
/*********************************
* INPUT *
* *******************************/
// when the user inputs a value and clicks calculate, the input must first be validated
if (Double.TryParse(txtTemperatureInput.Text, out inputTemperature) == false)
{
lblConvertedTemperature.Text = "Temperature must be a numeric value.";// message displayed in
//output label telling user their input was not accepted because it was not numeric
txtTemperatureInput.Focus();// sets the focus back onto the temperature textbox for a new entry
}
else
{
/*******************************
* PROCESSING *
*******************************/
if (optConvertToCelsius.Checked == true)// if the convert to celsius radio button is selected
// this statement will run
{
convertedTemperature = (inputTemperature - 32)*5 / 9;// the formula for converting
//Fahrenheit to Celsius
}
else if (optConvertToFahrenheit.Checked == true)// convert to fahrenheit was selected,
//meaning the convert to fahrenheit radio button was selected, so this statement will run
{
convertedTemperature = (inputTemperature * 9) / 5 + 32; ;// the formula for converting Celsius to
//Fahrenheit
}//end concatonated if
}//end if
/******************************
*OUTPUT *
******************************/
lblConvertedTemperature.Text = Math.Round(convertedTemperature, 4).ToString();// sets the converted temperature
// label to the value of convertedTemperature
}
}
}
My question is how can I reword the processing section to not cause this error.
Well, there are two situations that the compiler is concerned about:
If the input is invalid, you're already changing lblConvertedTemperature.Text anyway... so just return after you've done that. (You then don't need an else clause, which means you'll have less nesting - always nice for readability.)
Neither optConvertToCelsius nor optConvertToFahrenheit is checked. That's a situation the compiler considers, because you've got two separate conditions that you're checking. You may know it's never going to happen, of course.
For the latter, I'd suggest just removing the second condition:
if (optConvertToCelsius.Checked)
{
...
}
else
{
...
}
Note that I've removed the == true from the first condition just as a matter of style.
Now the compiler will know that if you get to this point, one of those two blocks will be executed. As both blocks assign to convertedTemperature, the variable will be definitely assigned at the end of that code.
EDIT: Just to clarify, if you have:
if (someCondition)
{
...
}
else if (someOtherCondition)
{
...
}
the compiler will not assume that one of those blocks will be executed, regardless of what the conditions are. Even if you know that exactly one condition will be true, the compiler just follows its relatively-simple rules.
You're scoping looks off. Simply move
/******************************
*OUTPUT *
******************************/
lblConvertedTemperature.Text = Math.Round(convertedTemperature, 4).ToString();// sets the converted temperature
// label to the value of convertedTemperature
To the "processing" scope.
Hi I have a numericupdown box and I am trying to load the value of it each time the form loads.
I have tried the following with no joy
string striko1value = clsData.GetStriko1GasReading();
decimal striko1 = decimal.Parse(striko1value);
Striko1Numeric.Value = Convert.ToDecimal(striko1);
I have also tried this
Striko1Numeric.Value = (striko1);
Anyone have any ideas about how I would do this or where I could read up on this.
Supposing that your clsData.GetStriko1GasReading() effectively returns a string that can be interpreted as a decimal value, then you probably have a value that is outside the Minimum or Maximum value allowed to the NumericUpdown control
string striko1value = clsData.GetStriko1GasReading();
decimal striko1;
if(decimal.TryParse(striko1value, out striko1))
{
if(striko1 > Striko1Numeric.Maximum || striko1 < Striko1Numeric.Minimum)
MessageBox.Show("Value not allowed");
else
Striko1Numeric.Value = striko1;
}
else
MessageBox.Show("Not a valid decimal number");
got it guys the Maximum value that i allowed was less than the value that was at striko1.
Thaks for the help
I am building this C# windows application. On a certain Form, I am using maskedtextbox to enter upto 3 numerical digits, then I convert it into int as I have to send all these data to database with an insert query, but the problem is that these maskedtextboxes give error when left empty:
int scd_user_comm = Convert.ToInt32(maskedTextBox1.Text);
and the error is :
Input string was not in a correct format.
The corresponding field in database allows null, so if left empty, it must not give an error. Can anyone help please?
One side question:: Can I use a textbox or maskedtextbox to ensure that user only enters numeric value b/w 0 to 100? Thanks in advance.
You could try the Int32.TryParse() method
int scd_user_comm;
if(Int32.TryParse(maskedTextBox1.Text, out scd_user_comm) == false)
scd_user_comm = 0;
this will give you all the flexiblity required to work with your integer var and your maschedTextBox-
To force your maskTextBox to accept only numeric values set
maskTextBox1.Mask = "999";
maskTextBox1.TextMaskFormat = MaskFormat.ExludePromptAndLiterals;
However this is not enough to ensure the enterd number is between 0 and 100.
To get this result you need to use the Validating event and reject the input if is outside your limits
private void maskedTextBox1_Validating(object sender, CancelEventArgs e)
{
int num;
if (Int32.TryParse(maskedTextBox1.Text, out num) == true)
{
if (num < 0 || num > 100)
{
MessageBox.Show("Insert number between 0 and 100");
e.Cancel = true;
}
}
}
You could use the TryParse instead:
int scd_user_comm;
if(!Int32.TryParse(maskedTextBox1.Text, out scd_user_comm))
{
// Do something to notify the user that the text isn't a number.
}