Int won't increment by 1 [closed] - c#

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();
}

Related

Calculate average when checkbox are checked C# [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Good morning everyone!
So, I have 8 textboxes and under those are checkboxes. To those textboxes I'am always receiving data from a serialport and I need to do an average from those values. But I only want to do the average from the textboxes from which I checked the checkbox under them.
How can I add those values to the average formula when selected by checking the checkbox?
If anyone could help, I would really apreciate.
Thanks,
Jonathan
I think you should write something like this :
bool[] CheckBoxList { get; set; } = new bool[8];
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBoxList[0] = checkBox1.Checked;
CalcAverage();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
CheckBoxList[1] = checkBox2.Checked;
CalcAverage();
}
in CalcAverage method , Textboxes with a true value in the list are computed.

Unable to reset time duration [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
On this code I am starting and stopping random sampling based on a fixed sampling time.
When I clicked stop sampling, timers stops correctly.
But when I start back again, the times do not start correctly.
could you please help to check what is wrong here?
public partial class MainWindow : Window
{
public float samplingTime = 10f;
double currentDateTime = 0.0;
DateTime dtEnd;
public bool running;
public MainWindow()
{
InitializeComponent();
timer1 start.code()
void timer_Tick(object sender, EventArgs e)
{
dtEnd = DateTime.Now.AddSeconds(samplingTime);
someCode();
}
}
private void startSample_Click(object sender, RoutedEventArgs e)
{
timer2 start code()
void windowtimer_Tick(object sender, EventArgs e)
{
currentDateTime = (dtEnd - DateTime.Now).TotalSeconds;
if (currentDateTime < 0.1 && running == true)
{
code();
}
}
}
private void stopSample_Click(object sender, RoutedEventArgs e)
{
timer.Stop();
windowTimer.Stop();
}
You are adding the Tick handler multiple times:
windowTimer.Tick += windowtimer_Tick;
is executed every time you start, but you don't unhook it. Probably you should set the handler elsewhere (in the designer maybe).
Or you can add:
windowTimer.Tick -= windowtimer_Tick;
to the Stop handle

Program throws an error "cannot implicitly convert int to string" [closed]

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();

how do i add +1 to a label each time a button is clicked? [closed]

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
protected void btnsubmit_Click(object sender, EventArgs e)
{
int result;
result=Convert.ToInt32(lblresult.Text);
}
this is all I have so far (just the variable)
Convert lblStart.Text value to int every time and assign it to i. Then increase i.
protected void btnsubmit_Click(object sender, EventArgs e)
{
i = Int32.Parse(lblStart.Text);
i++;
lblStart.Text = i.ToString();
}

Change height of rectangle with value in textbox [closed]

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.

Categories

Resources