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'm quite new to C# and I'm trying to change the height of a rectangle to the value of the number in my textbox when i press a button.
So when I hardcode it I get
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = 150;
}
this is without the texbox and worked fine.
I thought I had to do this if I use a textbox:
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = txt2010.Text;
}
But this doesn't work. Can somebody help me with this?
Height is an integer, but Text is a string. This isn't safe, in case the string can't parse to an integer, but it will work for your simple example.
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = int.Parse(txt2010.Text);
}
To be really safe you would use TryParse.
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
int height;
if(int.TryParse(txt2010.Text,out height))
{
rct2010.Height = height;
}
else
{
rct2010.Height = 150;
}
}
convert it to an int
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
rct2010.Height = int.Parse(txt2010.Text);
}
or you can go a step further
private void btnGrafiek_Click(object sender, RoutedEventArgs e)
{
int i = 0;
if(int.TryParse(txt2010.Text, out i)
rct2010.Height = i;
else
MessageBox.Show("That's not a number");
}
Presumably the type of Height is int, and so assigning a value of type string won't work and you'll get a compile-time error stating as such (feel free to clarify); you'll need to convert the type, such as:
int height = 0;
if (int.Parse(txt2010.Text, out height)) {
rct2010.Height = height;
}
The TryParse (as opposed to the otherwise suggested Parse) will ensure your application doesn't encounter an exception if the value cannot be parsed (i.e. it's bad, unexpected input). But then, on the other hand, this means your application apparently does nothing with the input (because it doesn't), so you may wish to use an else case to inform the user.
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have some code below which is a URL Parser and the if statement doesn't work instead of executing the code when it is true it executes all the code in all the if statements.
public static class var
{ //Global variables
public static string stre = System.Uri.UnescapeDataString(Environment.CommandLine);
public static string[] str = stre.Remove(stre.Length - 1).Split(new string[] { "cmd:" }, StringSplitOptions.None);
}
public CMD()
{ //I suppose this class runs first so all my is statements are here
AClsMsgBox.Show("Parsing...", "CMD Parser", 1000); //msgbox
if (var.stre.Length > 5) { InitializeComponent();} //This executes even if I type "cmd:" which should trigger the statement below. (Always executes) This should run when typing "cmd:echo a command"
else if (var.stre.Length == 4) { Process.Start("cmd.exe"); } //This should run when typing "cmd:"(Never execute)
else { AClsMsgBox.Show("This is a URL Parser use it by typing 'cmd:<command>' in the URL adress bar field of a browsers.","CMD Parser",60000); } //Always executes
}
private void Form1_Load(object sender, EventArgs e)
{
string stra = var.str[1].Replace("&", "&&");
label1.Text += stra;
}
private void button1_Click(object sender, EventArgs e)
{
Process.Start("cmd.exe", " /k " + var.str[1]);
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
private void label1_Click(object sender, EventArgs e) { }
}
This is C# GUI so I am running InitializeComponent() only if I need to display the rest of buttons and stuff. (I think it should stop the GUI)
And there are no errors! But appear after building the code.
On running debugger:
System.IndexOutOfRangeException
HResult=0x80131508
Message=Index was outside the bounds of the array.
Source=CMD Parser GUI
StackTrace:
at CMD_Parser_GUI.CMD.Form1_Load(Object sender, EventArgs e) in D:\bat\CMD Parser GUI\Form1.cs:line 61
line 61 is "string stra = var.str[1].Replace("&", "&&");"
I am making a tool in C# using Windows Forms with .NET Framework 4.7.1.0 which does math calculations for game Minecraft making building and calculating process easier and faster.
I made a button "Calculate Field" with in-code name CalculateFieldButton which is supposed to calculate a field of a rectangle based on two Coordinates inputs, which I made using TextBox components inside separate GroupBox component.
App look screenshot:
//Code activated after clicking on a button.
private void CalculateFieldButton_Click(object sender, EventArgs e)
{
try
{
intCoordinatesX = Convert.ToInt32(CoordinatesX);
intCoordinatesZ = Convert.ToInt32(CoordinatesZ);
intCoordinatesX2 = Convert.ToInt32(CoordinatesX2);
intCoordinatesZ2 = Convert.ToInt32(CoordinatesZ2);
NetherErrorLabel.Visible = false;
field = (intCoordinatesX - intCoordinatesX2) * (intCoordinatesZ - intCoordinatesZ2);
FieldLabel.Text = "Field: " + Convert.ToString(field);
} catch (FormatException)
{
//shows error text when the format of the numbers is wrong.
NetherErrorLabel.Visible = true;
}
}
For example:
CoordinatesX = 16
CoordinatesZ = 16
CoordinatesX2 = 8
CoordinatesZ2 = 8
Putting everything into formula gives me:
(16 - 8) * (16 - 8)
which should be equal to 64, but the output is 128 instead. (I know about negative values, but I'll do them once I'll fix this problem.)
After running program in debug mode and going through each line I've noticed, that CoordinatesZ2 string is null even though it contains a value, and using function Convert.ToInt32(); with argument null returns 0, and then it leads to the wrong result. The rest of the values are parsed correctly. Here are the screenshots of the Debug session when it converts CoordinatesZ2 to integer and a section of code which takes values from TextBoxes:
Debug: Click here Second link
private void InputBoxX_TextChanged(object sender, EventArgs e) => CoordinatesX = InputBoxX.Text;
private void InputBoxY_TextChanged(object sender, EventArgs e) => CoordinatesY = InputBoxY.Text;
private void InputBoxZ_TextChanged(object sender, EventArgs e) => CoordinatesZ = InputBoxZ.Text;
private void InputBoxX2_TextChanged(object sender, EventArgs e) => CoordinatesX2 = InputBoxX2.Text;
private void InputBoxY2_TextChanged(object sender, EventArgs e) => CoordinatesY2 = InputBoxY2.Text;
private void InputBoxZ2_TextChanged(object sender, EventArgs e) => CoordinatesZ2 = InputBoxZ2.Text;
I honestly don't know why this occurs and I never had similar problem like this never before. I checked properties of TextBoxes, syntax and I can't work out how to fix this. If you need any other information which you would need to get feel free to write. Also you can give feedback. Thanks!
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I'm trying to make a kind of miner without purpose. If you press start the program generates numbers between 1-900. And example 4 has 3 point you will get 3 point if you generated 4. But my problem is making the function that the code will be shown in the textbox.
public partial class Box : Form
{
public void MineEnabled()
{
Random rnd = new Random();
int a = 0;
for (a = 10; a < 200; a++)
{
int coin = rnd.Next(1, 900);
textBox1.Text = coin;
}
}
public Box()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
Close();
}
private void button2_Click(object sender, EventArgs e)
{
MineEnabled();
}
}
It throws an error at textBox1.Text = coin;:
Cannot implicitly convert type 'int' to 'string'
Can someone give me advise what I should do?
There are two problems with your code:
You're only able to see the number that is generated when a = 199
You need to convert your coin integer to a string by textBox1.Text = coin.ToString();
I can not for the life of me figure out why an error keeps throwing when I have other programs that look basically identical that don't throw this error. Any ideas?
sum = Convert.ToInt32(textBoxAdd1.Text) + Convert.ToInt32(textBoxAdd2.Text)
Throws the error:
Input string was not in a correct format.
Sorry. Here's the code:
public partial class MainWindow : Window
{
int sum;
int sub;
public MainWindow()
{
InitializeComponent();
sum = 0;
sub = 0;
}
private void button_Click(object sender, RoutedEventArgs e)
{
textBlock.Text = "HELLO " + textBox.Text;
}
private void buttonadd_Click(object sender, RoutedEventArgs e)
{
sum = Convert.ToInt32(textBoxAdd1.Text) + Convert.ToInt32(textBoxAdd2.Text);
textBlockadddisplay.Text = Convert.ToString(sum);
}
private void buttonsub_Click(object sender, RoutedEventArgs e)
{
sub = Convert.ToInt32(textBoxsub1.Text) + Convert.ToInt32(textBoxsub2.Text);
textBlocksubdisplay.Text = Convert.ToString(sub);
}
}
}
One of your text boxes is probably a value that cannot be converted to an int. One solution would be to check them using the in.TryParse Method first
int num1, num2;
if (int.TryParse(textBoxAdd1.Text, out num1) && int.TryParse(textBoxAdd2.Text, out num2))
{
// They were assigned.
}
There could be a number answers as to why this issue is occurring.
Here's some things you could think about:
1) What values are in textBoxAdd1 and textBoxAdd2?
You could be running into an issue where the number is too big to fit in an Int32 and you may need a larger datatype?
2) Have you tried debugging through your code and putting a breakpoint on that particular line? Then you will see exactly what is being passed in and maybe there's some formatting issue with the input - maybe you might need to strip some whitespaces?
If this is the issue, you can check this post:
C# - remove whitespace from input number to have int
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
I'm trying to increment the value of an int: counter by 1 each time the stakebtn is pressed.
Here's what I've tried so far:
private void stakebtn_Click(object sender, EventArgs e)
{
int counter = 0;
counter++;
currentstakes.Text = counter.ToString();
}
However, this only works one time- the currentstakes label will not go above 1.
Is anyone able to see where I am going wrong?
Obviously, it will increase only one time, declare it outside your click,
int counter = 0;
private void stakebtn_Click(object sender, EventArgs e)
{
counter++;
currentstakes.Text = counter.ToString();
}
You are setting it to zero every time before you increment it, move the counter declaration outside the method:
int counter = 0;
private void stakebtn_Click(object sender, EventArgs e)
{
counter++;
currentstakes.Text = counter.ToString();
}