The problem is that the numericupdown doesn't change types of speed for fast and slow. Basically it would speed up whether the value is positive or negative and if I take it to back to zero it will crash. I am now trying to make an if loop inside of the numericupdown for TickCounter.
Here it is:
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Text = "AAAAAAA AAAAAAA #########";
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
TickCounterLabel.Text = "The timer has started";
tPeriodic.Enabled = true;
}
else
{
TickCounterLabel.Text = "The timer has ended";
tPeriodic.Enabled = false;
}
}
private void TickCounter_ValueChanged(object sender, EventArgs e)
{
**TickCounter.Value = TickCount;
if (TickCount >= 0)
tPeriodic.Interval = 1000 / Convert.ToInt32(TickCounter.Value * TickCounter.Value);
else if (TickCount <= 0)
tPeriodic.Interval = 1000 * Convert.ToInt32(TickCounter.Value * TickCounter.Value);
else if (TickCount == 0)
tPeriodic.Interval = Convert.ToInt32(TickCounter.Value * TickCounter.Value);**
}
private void tPeriodic_Tick(object sender, EventArgs e)
{
tickCount += 1;
lTickCount.Text = tickCount.ToString();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
public int tickCount { get; set; }
public int X { get; set; }
private void TickCounterLabel_Click(object sender, EventArgs e)
{
}
public int TickCount { get; set; }
}
}
Should I use another type of loop or am I just writing this wrong? When I try to change the value now it just crashes and the doesn't change at all.
Does anyone know how this can be accomplished? Also, should else if (TickCount == 0) be different if I don't want to have any change at all?
I guess it is because of divide by zero.
In the code of "TickCounter_ValueChanged", for TickCount=0, it just gets into the first "if" block and will cause the exception.
You need to use "if-else" conditions in a correct way.
private void TickCounter_ValueChanged(object sender, EventArgs e)
{
if (TickCounter.Value > 0)
tPeriodic.Interval = 1000 / Convert.ToInt32(TickCounter.Value * TickCounter.Value);
else if (TickCounter.Value < 0)
tPeriodic.Interval = 1000 * Convert.ToInt32(TickCounter.Value * TickCounter.Value);
else
tPeriodic.Interval = 1000;
}
Please let me know whether it solves your problem.
Related
I have problem, which consists in aesthetic sense, correctly - There is textBox to which i apply true condition of UseSystemPasswordChar.. It's work! But i get bold points. Try to change font size - decreases textbox's field. Below is the code (although why is it here?). Can anyone help, thank you in advance)
public partial class frmRegistr : Form
{
public frmRegistr()
{
InitializeComponent();
}
int counter = 0;
int a = 0;
string b;
private void frmRegistr_Load(object sender, EventArgs e)
{
b = label1.Text;
a = b.Length;
label1.Text = "";
timer1.Start();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
private void timer1_Tick(object sender, EventArgs e)
{
if (counter < a)
{
counter++;
label1.Text = b.Substring(0, counter);
}
else
{
timer1.Stop();
}
}
private void label4_Click(object sender, EventArgs e)
{
timer3.Start();
}
private void label4_MouseHover(object sender, EventArgs e)
{
//if this.MouseLeave
label4.BackColor = Color.FromArgb(((int)(((byte)(154)))), ((int)(((byte)(181)))), ((int)(((byte)(101)))));
}
private void timer2_Tick(object sender, EventArgs e)
{
if (Opacity == 1)
{
timer2.Stop();
}
Opacity += .2;
}
private void timer3_Tick(object sender, EventArgs e)
{
if (Opacity <= 0)
{
this.Close();
}
Opacity -= .2;
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
textBox2.UseSystemPasswordChar = true;
}
}
}
If you want to define your own password character, use property TextBox.PasswordChar. If you want this in a certain font, use Control.Font
As you only have to do this once, do this in the constructor:
public MyForm : Form
{
InitializeComponents(),
this.textBox1.PasswordChar = '_';
this.textBox11.Font = new Font(...)
};
You can also decide to do this using the visual studio designer.
You can setup this in VisualStudio designer, but this is code:
textBox1.PasswordChar = '*';
//* = password character
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int A = 0;
private void timer1_Tick(object sender, EventArgs e)
{
A++;
if (A == 30)
{
A--;
}
textBox1.Text = A.ToString();
}
}
}
Just store the direction you're going and flip it when you reach the limits:
int A = 0;
int direction = 1;
private void timer1_Tick(object sender, EventArgs e)
{
A += direction;
if (A == 30 || A == 0)
{
direction = -direction;
}
textBox1.Text = A.ToString();
}
I am making a program that makes a user select the type of cookie they want with a constant price and the quantity they want; wither a 1/2 dozen , 1 dozen, 2 dozen, and 3 dozen. I am having a tough time applying my listBox which is named Quantity and its values to my if statements for each cookie pressed. My whole if statement is red stating it cannot be applied to operands of type "double" and "void".
public partial class Form1 : Form
{
private const double CHOCOLATE_CHIP = 8.98;
private const double OATMEAL = 6.98;
private const double VANILLA_WAFER = 6.48;
private double price;
public Form1()
{
InitializeComponent();
this.listBox1.Items.AddRange(new object[] { 0.5, 1.0, 2.0, 3.0 });
}
private void close_Click(object sender, EventArgs e)
{
this.Close();
}
private void clear_Click(object sender, EventArgs e)
{
accumulated.Clear();
}
private void buy_Click(object sender, EventArgs e)
{
}
private void chocolateChip_CheckedChanged(object sender, EventArgs e)
{
if(chocolateChip.Checked == true)
{
if (listBox1.SelectedItem != null)
price = CHOCOLATE_CHIP * ((double)listBox1.SelectedItem * 0.50);
accumulated.Text(price.ToString));
}
}
}
}
Even though I really don't think you understand the best use of a ListBox. Here is some code, using your techniques, that will get you what you want. I would highly recommend understanding all of the various controls you use or have access to so that your program is as robust as possible. Hopefully now you can use this to understand how to gain access to the ListBox.SelectedItem and use it in your calculations.
public partial class Form1 : Form
{
double total = 0.0;
double COOKIE_PRICE = 2.24;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.listBox1.Items.AddRange(new object[] { 0.5, 1.0, 2.0, 3.0 });
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
if (listBox1.SelectedItem != null)
{
// (* 12.0) Because the value selected is number of dozen.
total = COOKIE_PRICE * ((double)listBox1.SelectedItem * 12.0);
MessageBox.Show(total.ToString());
}
else
{
MessageBox.Show("Nothing was selected in the ListBox!");
}
}
}
}
I have a function that displays numbers using a while loop but I want to stop execution of a while loop at random variable value using c# by clicking a button.
For Example:
private void FrqSent()
{
int i = 1;
while (i <= 5)
{
i = i + 1;
}
}
Here is a quick example on how you can use a Backgroundworker to accomplish your task:
public partial class Form1 : Form
{
private int i = 1;
public Form1()
{
InitializeComponent();
}
private void FrqSent()
{
while (i <= 500000000)
{
if (backgroundWorker1.CancellationPending)
{
break;
}
else
{
i = i + 1;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
private void button2_Click(object sender, EventArgs e)
{
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
FrqSent();
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show(i.ToString());
}
}
Just create a new Windows-Forms Project and add a backgroundworker object aswell as 2 buttons. You have to set the DoWork, RunWorkerCompleted and Click events manually.
Edit: Do not forget to set the BackgroundWorker`s WorkerSupportsCancellation property to true.
not very elegant but simple
public partial class Form1 : Form
{
private bool _stop;
public Form1()
{
InitializeComponent();
}
private void buttonStart_Click(object sender, EventArgs e)
{
_stop = false;
FrqSent();
}
private void buttonStop_Click(object sender, EventArgs e)
{
_stop = true;
}
private void FrqSent()
{
int i = 1;
while (i <= 5000000 && !_stop)
{
i = i + 1;
Application.DoEvents();
}
}
}
In my application I need a progress bar to display the progress of a plant growing. This would be the code:
private static Timer farmProgress;
internal void initFarmProgTimer( int step, int max = 100 )
{
farmProgress = new Timer();
farmProgress.Tick += new EventHandler(farmProgress_Tick);
farmProgress.Interval = step; // in miliseconds
farmProgress.Start();
}
private void farmProgress_Tick(object sender, EventArgs e)
{
if (increment >= 100)
{
// wait till user get plant
}
else
{
increment++;
plantProgressBar.Value = increment;
}
}
Here the call of the initFarmProgTimer function:
public static System.Threading.Timer growTimer;
public static void InitGrowTimer(int time, string name)
{
growTimer = new System.Threading.Timer(growTimer_Finished, null, time, Timeout.Infinite);
plantActive = true;
Menu menu = new Menu();
menu.initFarmProgTimer(time / 100);
}
Note that the class where this function is called from is NOT a form but the class where the function is defined IS a form.
Does anybody know what my error is?
edit
here is the call for the InitGrowTimer function
switch ( index )
{
case 0:
currentPlant = wheat.name;
plantQ = printPlantDatas("wheat");
if (plantQ == true)
{
InitGrowTimer(wheat.time, wheat.name);
wheat.planted++;
}
break;
}
You are setting the value to the progress bar which will only set the progress to the current value. You have to increment it rather. I have added the pluss (+) sign for you
private void farmProgress_Tick(object sender, EventArgs e)
{
if (increment >= 100)
{
// wait till user get plant
}
else
{
increment++;
plantProgressBar.Value += increment;
}
}
It is not clear to me what is on a Form and what isn't but assuming this is on the Form:
private void farmProgress_Tick(object sender, EventArgs e)
{
if (increment >= 100)
{
// wait till user get plant
}
else
{
increment++;
plantProgressBar.Value = increment;
}
}
Change it to this so that you update the control from the UI thread:
private void farmProgress_Tick(object sender, EventArgs e)
{
if (increment >= 100)
{
// wait till user get plant
}
else
{
increment++;
this.Invoke(new Action(() => {
plantProgressBar.Value = increment;
}));
}
}
Update
My answer is wrong, I didn't expect this, but I created a Forms application and this worked fine to increment the progressbar:
public partial class Form1 : Form
{
private Timer farmProgress;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
farmProgress = new Timer();
farmProgress.Tick += farmProgress_Tick;
farmProgress.Interval = 1000; // in miliseconds
farmProgress.Start();
}
void farmProgress_Tick(object sender, EventArgs e)
{
progressBar1.Value++;
}
}