Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am trying to have a text box validate if the entry is a number between 1 and 100.
Example:
if (textBox.Text is equal to numbers between 1 and 100)
{
do this;
}
else
{
do this;
}
This is form validation for a trackbar used for jpeg compression and can only have numeric values between 1 and 100. How do I do this?
String text = TextBox.Text;
try{
long value = long.parse(text.trim());
if(value > 0 && value < 101){
//do something here
}
else{
//Do something else
}
}
catch(Exception e){
Messagebox.Show("Please check you input and try again");
}
First you need to convert input from textbox from string to integer
string textBoxvalue = textBox.Text;
int textBoxIntValue = int.TryParse(textBoxvalue)
then you need to check values for condition you need
if(textBoxIntValue > 0 && textBoxIntValue <= 100)
{
//do THIS
}
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
The below code is giving me error "Cannot assign to 'writeline' because it is a 'method group;" at every Console.Writeline statement. I am trying to find out why but I could not find anything to point me in right direction. Any help appreciated.
{
class FizzBuzz
{
static void Main(string[] args)
{
for (int i = 1; i <= 100; i++)
if ((i % 3 == 0) && (i % 5 == 0))
{
Console.WriteLine = "Fizzbuzz" ;
}
else if (i % 3 == 0)
{
Console.WriteLine = "Fizz";
}
else
(i % 5 == 0)
{
Console.WriteLine = "Buzz";
}
System.Console.ReadLine();
}
}
}
You need to set the string in parenthesis:
Console.WriteLine("This string goes to console.");
You tried to assign the method with a value, that is not possible. That works only with properties and fields.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Assume I let user input number : 1298743257884834...(long as user need)
Let program tell how many digit are they ?
then give result by SUM them = ?
By using LINQ
string input = // get input from console, textbox, ...
int inputLength = input.Length; // get number of digits, although you don't need it here
int sum = input.Sum(c => int.Parse(c.ToString())); // summarize
Hint: use the BigInteger structure found in System.Numerics.
First of all you can know the number of digits in this way:
string digits = TextBox1.Text;
long digitsnumber = digits.Length;
Then to sum each number, you need to loop in your string like an array of char, in this way, and cast the car value to an integer with the GetNumericValue method of System.Char:
int sum = 0;
foreach (var c in digits)
{
if (Char.IsNumber(c))
{
sum += (int)Char.GetNumericValue(c);
}
}
Sum will be your result.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a button (let's call it button1), a textbox (let's call it textbox1) and another textbox (let's call it textbox2). - Windows Form Application in C#
So i've made it that you need to write how long you want the characters to be in textbox2 (e.x 9) when button1 is clicked to generate random 9 characters (like D0yZk#!eA - depending how many characters you've set it to in textbox2) to textbox1. It can generate from 1 to 99 characters, that's just an example.
So my question is how to make in C# when button1 is clicked, the result from textbox1 to go to a text file in a new line which to be included in the program in which i can scroll in to see what i have generated? Like a textbox (but that textbox's content to be a textfile that all generated things will be in a new line) that show's all or the latest generated things in a new line each that is only readable.
Add a ListBox to your form and call it listBox1. Then add a click event to button1, and do something like:
private void button1_Click(object sender, EventArgs e) {
int length;
// We use TryParse to make sure it is a number, otherwise
// we tell the user and return;.
if (!int.TryParse(textBox2.Text, out length)) {
MessageBox.Show("Not a number!");
return;
}
// Make a random string
string myString = RandomString(length);
// Show it in the text box
textBox1.Text = myString;
// ... and in the listbox
listBox1.Items.Add(myString);
}
The function for a random string could look like this:
string RandomString(int length) {
string options = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#%&/()=?";
StringBuilder str = new StringBuilder(length);
Random random = new Random();
// Add the characters one by one
for (int i = 0; i < length; i++) {
// Get a random index in our character options string.
int randomIndex = random.Next(options.Length);
// Add the random character
str.Append(options[randomIndex]);
}
// Make a string of the StringBuilder and return it.
return str.ToString();
}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
The problem, i am facing in this code is that first iteration of loop is good but in second iteration when i press y program gives error.
Error = Input string was not in a correct format.
Line of Error = my = int.Parse(Console.ReadLine());
static void Main(string[] args)
{
int a, my;
char again = 'y';
while ((again == 'y' || again=='Y'))
{
Console.Write("Enter the value for your number = ");
my = int.Parse(Console.ReadLine());
Random b = new Random();
a = b.Next(1, 6);
if (a == my)
{
Console.WriteLine("Congratulations");
}
else
{
Console.WriteLine("you Lost");
Console.WriteLine("My no is {0}.", a);
}
Console.Write("Again? Then press 'y' or 'Y' = ");
again = (char)Console.Read();
}
Console.ReadLine();
}
Change
again = (char)Console.Read();
to
again = (char)Console.ReadLine().First();
Console.Read Reads the next character from the standard input stream. To continue you press a key ('y' in this case) and press 'enter'. Which means you input 2 characters and read 1. Which one is read by next Console.Readline. So, you never get the actual string in this line
my = int.Parse(Console.ReadLine());
Rather you get the character you did not read previously. And this one can not be parsed to integer.
If you got Input string was not in a correct format in a Int32.Parse, try replace it for a Int32.TryParse
Int32.TryParse(Console.ReadLine(), out my);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions must demonstrate a minimal understanding of the problem being solved. Tell us what you've tried to do, why it didn't work, and how it should work. See also: Stack Overflow question checklist
Improve this question
I have a DataTable. What I want to do is change the value of all rows of the Colum "X" of the DataTable.
For example:
if row value is "TRUE" then change it into "Yes"
else change it into "No"
A simple loop:
foreach(DataRow row in table.Rows)
{
string oldX = row.Field<String>("X");
string newX = "TRUE".Equals(oldX, StringComparison.OrdinalIgnoreCase) ? "Yes" : "No";
row.SetField("X", newX);
}
StringComparison.OrdinalIgnoreCase enables case insensitive comparison, if you don't want "Equals" to be "Yes" simply use the == operator.
maybe you could try this
int columnNumber = 5; //Put your column X number here
for(int i = 0; i < yourDataTable.Rows.Count; i++)
{
if (yourDataTable.Rows[i][columnNumber].ToString() == "TRUE")
{ yourDataTable.Rows[i][columnNumber] = "Yes"; }
else
{ yourDataTable.Rows[i][columnNumber] = "No"; }
}
Hope this helps...