I want to do a program in C# - WFA, in which while a button is pressed - the label 1 value is increased by 10 since it reaches 100, then in decreases to 0 and again is increased til 100
like: 0 10 20 30...80 90 100 90 80...20 10 0 10...
i tried this:
private static int i = 0;
protected void button1_MouseClick(object sender, MouseEventArgs e)
{
if (i < 100)
i = i + 10;
else
i = i - 10;
this.label1.Text = i.ToString();
}
but it just decrease from 100 to 90 and the increases back to 100 and goes like this
You need to save two state information about the current value outside the button1_MouseClick() method. One is the value i, which you already do. The other is the information if you are going up or down. You can save this in a simple bool value like:
public static bool goingUp=true;
Then you can use these two fields i and goingUp and decide what you want to do:
if (goingUp)
{
i += 10;
if (i >= 100)
{
goingUp = false;
}
}
else
{
i -= 10;
if (i <= 0)
{
goingUp = true;
}
}
this.label1.Text=i.ToString();
an easy way would be this one
_indexValue = (_indexValue + 1) % 100;
and when it hits 100 then it restart to 0
Related
I have a datagirdview that shows time and speed information from gps. I wanted datagridview shows data for each 10 times of speed and time shows elapsed time. I mean;
speed time
10 0,31
20 0,75
30 0,95
.. ..
However I faced some problems such as I can't always get value as clear as I thought. For example if I want to see speed and time in datagridview from 20 to 60 as speed. It shows;
speed time
10 0,31
20 0,75
40 0,95
60 1.25
It missed some values as you see above(values for 30 and 50 are absent). To solve this I want to get nearest value(the next data) to my conditions.
Let me show you my data and what I wanted;
this is data
speed time
10 0,31
13 0,35
16 0,40
19 0,45
22 0,48
25 0,51
28 0,54
31 0,58
34 0,62
.. ..
this is what I want to see
speed time
10 0,31
22 0,48
31 0,62
Is there any function to to this?
EDIT
I collected data in listboxes(listboxvelocity and listboxtime), label1 shows speed, label2 shows elapsed time, and I get data from listboxes to datagridview. By the way I collect data by using timer, here is my code,
int p;
Stopwatch watch=new Stopwatch();
private void timer2_Tick(object sender, EventArgs e)
{
listBoxTime.Items.Add(label2.Text);
listBoxVelocity.Items.Add(label1.Text);
p = dataGridView1.Rows.Add();
watch.Start();
var time = watch.Elapsed.TotalSeconds;
if (Math.Round(Convert.ToDecimal(label1.Text),0)%10==0 ||
Convert.ToDecimal(label1.Text)==0)
{
for (int i = 1; i < listBoxTime.Items.Count; i++)
{
dataGridView1.Rows[p].Cells[0].Value =listBoxVelocity.Items[i].ToString();
dataGridView1.Rows[p].Cells[1].Value = listBoxTime.Items[i].ToString();
}
}
EDIT2
I have two buttons to create artificial speed to simulate code.When I start to increase value of the label1(as speed) it catches the true value but it doesn't save it in datagridview1. Datagirdview1 always shows the lastest value which is the closest more than the last decade in its all rows. And here is the final code.
private void timer2_Tick(object sender, EventArgs e)
{
watch.Start();
var time = watch.Elapsed.TotalSeconds;
label2.Text = String.Format("{0:00.00}", time);
List<DataPoint> liste = new List<DataPoint>();
liste.Add(new DataPoint(Convert.ToDouble(label1.Text), Convert.ToDouble(label2.Text)));
dataGridView1.DataSource = liste;
List<DataPoint> newlist = new List<DataPoint>();
for (int q = 10; q < liste.Max(t => t.Speed); q += 10)
{
newlist.AddRange(liste.Where(t => t.Speed % q == liste.Select(v => v.Speed % q).Min()).ToList());
}
dataGridView1.DataSource = null;
dataGridView1.DataSource = newlist;
}
private void button1_Click(object sender, EventArgs e)
{
x += 3;
aquaGauge1.Value =Convert.ToSingle(x);
label1.Text = x.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
x -= 3;
aquaGauge1.Value = Convert.ToSingle(x);
label1.Text = x.ToString();
}
Say I have a list: 1 5 6 10
When I click on next and the List item showed is already 10.. I want to bring the user back to 1 and same in the other way when I click previous and the List item showed is already 1.. I want to bring the user back to 10.. Here is what I tried but I always get that the "i (contor) can't be negative or greater than List.Count" :
public int i = 0;
private void nextbutton_Click(object sender, EventArgs e)
{
++i;
if (sweaterclicked)
{
if(SweatersList.Count != 1)
if (i >= SweatersList.Count - 1)
{
i = 0;
pictureBox1.Image = SweatersList[i];
}
else pictureBox1.Image = SweatersList[i];
}
}
private void previousbutton_Click(object sender, EventArgs e)
{
--i;
if (sweaterclicked)
{
if (SweatersList.Count != 1)
{
if (i < 0)
{
i = SweatersList.Count;
pictureBox1.Image = SweatersList[i];
}
else pictureBox1.Image = SweatersList[i];
}
}
}
Array or list indexes in C# start at zero, so the largest in-range index is one less than the count of items in the collection. Therefore, SweatersList.Count, hence i, is out of range here:
i = SweatersList.Count;
pictureBox1.Image = SweatersList[i];
So when you want to start over at the last valid index, you need to subtract one from SweatersList.Count:
i = SweatersList.Count - 1;
I don't know why you're incrementing and decrementing outside the if (sweaterclicked) blocks, but I presume you do.
In the top of form1 i have:
private int seconds;
private int minutes;
private int hours;
In the constructor:
seconds = 0;
minutes = 20;
hours = 0;
label9.Visible = false;
label9.Text = "00:00:00";
Then the timer 3 tick event. I used a breakpoint on the timer3 tick event i see that the minutes and seconds are coundting backwards but label 9 is not updating.
Maybe the string.Format on label9 is not right ?
private void timer3_Tick(object sender, EventArgs e)
{
// Verify if the time didn't pass.
if ((minutes == 0) && (hours == 0) && (seconds == 0))
{
// If the time is over, clear all settings and fields.
// Also, show the message, notifying that the time is over.
timer3.Enabled = false;
label9.Visible = true;
label9.Text = "00:00:00";
}
else
{
// Else continue counting.
if (seconds < 1)
{
seconds = 59;
if (minutes == 0)
{
minutes = 59;
if (hours != 0)
hours -= 1;
}
else
{
minutes -= 1;
}
}
else
seconds -= 1;
// Display the current values of hours, minutes and seconds in
// the corresponding fields.
label7.Visible = true;
label9.Visible = true;
label9.Text = string.Format("{00} : {00} : {00}", hours.ToString(), minutes.ToString(),seconds.ToString());
}
}
All i see in the end on label 9 is: 0:0:0 thats it and i dont see 20 minutes counting backward.
You need to fix your string formatting, that's the culprit:
label9.Text = string.Format("{0:00} : {1:00} : {2:00}", hours, minutes, seconds);
Also - consider using some other mechanism (e.g. Stopwatch class) for calculating elapsed time; you can't guarantee that the code as written will execute exactly every 1000 ms.
Below is my current code:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int[] trialArray = new int[10];
public int trialCounter = -1;
private void button1_Click(object sender, EventArgs e)
{
bool button1Click = true;
if (button1Click == true)
{
ITIpanel.Visible = true;
for (int i = 0; i < trialArray.Length; i++) { trialArray[i] = -1; } // Set default value through array
int counter = 0;
Random rnd = new Random();
while (counter < 10 / 2)
{ // Red trials, fill half array
int index = rnd.Next(0, 10 - 1);
if (trialArray[index] == -1) { trialArray[index] = 1; ++counter; } //if unchanged value, change it
}
while (counter < 10)
{
int index = rnd.Next(0, 10);
if (trialArray[index] == -1) { trialArray[index] = 2; ++counter; }
}
}
}
private void ITIpanel_Paint(object sender, PaintEventArgs e)
{
if (ITIpanel.Visible == true)
{
trialCounter += 1;
timer1.Enabled = true;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
ITIpanel.Visible = false;
timer1.Enabled = false;
if (trialArray[trialCounter] == 1) { redstimPanel.Visible = true; }
else { bluestimPanel.Visible = true;}
if (trialCounter == 9) { Application.Exit(); }
}
public int counter = 0;
public event EventHandler Clicked5TimesEvent;
private void OnClicked5TimesEvent()
{ if (Clicked5TimesEvent != null) { Clicked5TimesEvent(this, EventArgs.Empty); } }
private void bluestimPanel_MouseDown(object sender, EventArgs e)
{
//FR requirement
counter++; if (counter % 5 == 0) { redstimPanel.Visible = false; ITIpanel.Visible = true; }
}
private void redstimPanel_MouseDown(object sender, EventArgs e)
{
//FR requirement
counter++; if (counter % 5 == 0) { redstimPanel.Visible = false; ITIpanel.Visible = true; }
}
}
}
As you can see, I am attempting to make a global array with 10 items. On the button click the 10 items are supposed to be altered such that half contain the value 1 and the other half contain the value 2.
Then, on the timer tick, depending on the value in the trialCounter, which determines the part of the array to be accessed, it should display either the redstimPanel or the bluestimPanel.
Therefore, if the 'trialCounter' is equal to 8, and 8 in the TrialArray is equal 1, the 'redstimPanel' should become Visible. Alternatively, if 8 in the 'TrialArray' is equal to 2, the 'bluestimPanel' should become Visible.
This, however, is not working as I would like it to. Thus, there are clearly some issues with my code. Do you all have any suggestions?
You never reset counter, or have the second loop (the one setting the 2s) be the full array.
There is also an error with the random number, rnd.Next(a,b) a - lower bound (inclusive), b - upper bound (exclusive). So it should be rnd.Next(0,10); so you have a chance of populating the last array position.
while (counter < 10 / 2) { // Red trials, fill half array
int index = rnd.Next(0, 10);
if (trialArray[index] == -1) { trialArray[index] = 1; ++counter; } //if unchanged value, change it
}
//Counter on the first loop here is already 5 (it exited the previous loop)
//So allow it to get to 10, and populate the FULL array.
while (counter < 10) {
int index = rnd.Next(0, 10);
if (trialArray[index] == -1) { trialArray[index] = 2; ++counter; }
}
Allow me to give you some tips and some explanations regarding your code:
First of all, you probably wanted that local button1Click variable to know later on whether the button has been clicked or not. For that to work, you should place it outside that function, otherwise it's never going to be used, and will be true with every button click, something like this:
bool button1Click = false;
private void button1_Click(object sender, EventArgs e)
{
if (!button1Click)
{
When you have a condition, you want the code to decide, whether an expression is true or false you may omit the part "== true" because it doesn't add anything new.
You have two whiles. Your idea was to run the counter until 5, with the first piece of code, and then from 5 to 10 the second piece of code. Now let me try to explain what is actually going on. The counter will go on until 5 filling 1s at random indices. Then at 5, the expression in the while will become false and it breaks out from the loop. Since the second while has the very same expression, it simply avoids it and goes on. One of the many solutions would be to have an if in the loop like this:
while (counter < 10)
{
if (counter<5)
{
// fill red
}
else
{
// fill blue
}
}
The way you fill up the values in your array. Have you thought about what's going to happen when the same index will be generated several times? It means it'll overwrite the previous value while certain index will remain -1.
This is the numeric changed event code with timer2 wich didnt solve hte problem the function im calling is DoThresholdCheck()
The problem is that in this function im creating each time im changing the numeric value a temp list each time im moving the numeric value change it the list is been created from the start. The problem is that if im using a big file in my program the list is containing sometimes 16500 indexs and its taking time to loop over the list so i guess when im changing the numeric value its taking time to loop over the list. If im using smaller video file for example the list containing 4000 indexs so there is no problems. I tried to use Timer2 maybe i could wait 0.5 seconds between each time the numeric value is changed but still dosent work good.
When im changing the numeric value while the program is running on a big video file its taking the values to be changed like 1-2 seconds ! thats a lot of time.
Any way to solve it ? Maybe somehow to read the list loop over the list faster even if the list is big ?
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
Options_DB.Set_numericUpDownValue(numericUpDown1.Value);
if (isNumericChanged == true)
{
isNumericChanged = false;
myTrackPanelss1.trackBar1.Scroll -= new EventHandler(trackBar1_Scroll);
DoThresholdCheck();
counter = 0;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
counter++;
if (counter > 1)
{
isNumericChanged = true;
//timer2.Stop();
}
}
This is the DoThresholdChecks() function code:
private void DoThresholdCheck()
{
List<string> fts;
//const string D6 = "000{0}.bmp";
if (Directory.Exists(subDirectoryName))
{
if (!File.Exists(subDirectoryName + "\\" + averagesListTextFile + ".txt"))
{
return;
}
else
{
bool trackbarTrueFalse = false ;
fts = new List<string>();
int counter = 0;
double thershold = (double)numericUpDown1.Value;
double max_min_threshold = (thershold / 100) * (max - min) + min;
//label13.Text = max_min_threshold.ToString();
_fi = new DirectoryInfo(subDirectoryName).GetFiles("*.bmp");
for (int i = 0; i < myNumbers.Count; i++)
{
if (myNumbers[i] >= max_min_threshold)
{
//f.Add(i);
string t = i.ToString("D6") + ".bmp";
if (File.Exists(subDirectoryName + "\\" + t))
{
counter++;
button1.Enabled = false;
myTrackPanelss1.trackBar1.Enabled = true;
trackbarTrueFalse = true;
label9.Visible = true;
// myTrackPanelss1.trackBar1.Scroll += new EventHandler(trackBar1_Scroll);
//myTrackPanelss1.trackBar1.Minimum = 0;
// myTrackPanelss1.trackBar1.Maximum = f.Count;
// myTrackPanelss1.trackBar1.Value = f.Count;
// myFiles = new Bitmap(myTrackPanelss1.trackBar1.Value);
}
else
{
label9.Visible = false;
trackbarTrueFalse = false;
button1.Enabled = true;
myTrackPanelss1.trackBar1.Enabled = false;
myTrackPanelss1.trackBar1.Value = 0;
pictureBox1.Image = Properties.Resources.Weather_Michmoret;
label5.Visible = true;
secondPass = true;
break;
}
//fts.Add(string.Format(D6, myNumbers[i]));
}
}
//myTrackPanelss1.trackBar1.Maximum = _fi.Length - 1;
if (myTrackPanelss1.trackBar1.Maximum > 0)
{
if (trackbarTrueFalse == false)
{
myTrackPanelss1.trackBar1.Value = 0;
}
else
{
myTrackPanelss1.trackBar1.Maximum = counter;
myTrackPanelss1.trackBar1.Value = 0;
SetPicture(0);
myTrackPanelss1.trackBar1.Scroll += new EventHandler(trackBar1_Scroll);
}
//checkBox2.Enabled = true;
}
if (_fi.Length >= 0)
{
label15.Text = _fi.Length.ToString();
label15.Visible = true;
}
}
}
else
{
button1.Enabled = true;
}
}
try to cache results from DoThresholdCheck method
You can't magically get around the time the processing takes, if the processing is really necessary. You've got a couple of avenues to explore:
1) Minimise the processing being done - is all of it necessary? Can you cache any of it and only recompute a small amount each time the value changes?
2) Do the processing less often - do you have to recompute every single time?
3) Do the processing on another thread - then at least your UI remains responsive, and you can deliver the results to the UI thread when the background task is complete. However, this is going to be a relatively complicated variant of this pattern as you're going to need to be able to abort and restart if the value changes again while you're still processing the previous one.