So in my very small program I am generating a random percentage and based off that percentage it should display one of two panels. However, it is only ever displaying one.
Here is my code:
Random random = new Random();
private void button1_Click(object sender, EventArgs e)
{
bool button1Clicked = true;
if (button1Clicked == true) { ITIpanel.Visible = true; }
}
private void ITIpanel_Paint(object sender, PaintEventArgs e)
{
ITItimer.Enabled = true;
}
private void ITItimer_Tick(object sender, EventArgs e)
{
double rand = random.NextDouble();
if (rand <= .50) { bluestimPanel.Visible = true; }
if (rand >= .50) { redstimPanel.Visible = true; }
ITItimer.Enabled = false;
}
private void bluestimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void redstimPanel_Paint(object sender, PaintEventArgs e)
{
Trialtimer.Enabled = true;
}
private void Trialtimer_Tick(object sender, EventArgs e)
{
bluestimPanel.Visible = false;
redstimPanel.Visible = false;
Trialtimer.Enabled = false;
ITIpanel.Visible = true;
}
I think what is happening here is that in the TrialTimer_Tick method setting ITIPanel.Visible to true is not causing ITIPanel to repaint and hence ITITimer never restarts.
You can put a breakpoint in the ITITimer_Click method and see if it is ever invoked again after the first invocation.
Related
I am new to c# and I know the answer is very simple I just could not find it through searching
I created two buttons the first one generates random values
and the second one is an IF statement inside another button, but I`m getting a red line under 1value1 saying
the name does not exists in current context
private void button3_Click(object sender, EventArgs e)
{
Random b = new Random();
float value = b.Next(50, 100);
}
private void button2_Click(object sender, EventArgs e)
{
if (value < MinValue)
{
textBox18.Text = ("warning");
textBox18.ForeColor = Color.White;
textBox18.BackColor = Color.Red;
}
}
value is defined in the scope of the button3_Click and thus not accessible for the button2_Click. Put it as a variable of the class:
private int _minValue = 50;
private int _maxValue = 100;
private float _value = _maxValue;
private void button3_Click(object sender, EventArgs e)
{
Random b = new Random();
_value = b.Next(_minValue, _maxValue);
}
private void button2_Click(object sender, EventArgs e)
{
if (_value < _minValue)
{
textBox18.Text = ("warning");
textBox18.ForeColor = Color.White;
textBox18.BackColor = Color.Red;
}
}
Set property GenerateMember=True
I have 40 buttons that all do something slightly different when clicked, I would like to condense this down if I can. I also want to say, if one of the buttons is clicked, create a timestamp which can be accessed by the class.
Here is the code for 2 out of 40 of the buttons:
private void Btn1_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 5; i++)
{
CheckBox CheckBox = (this.FindName(string.Format("Check{0}", i)) as CheckBox);
if (CheckBox != null)
{
CheckBox.IsChecked = true;
}
}
}
private void BtnDisable1_Click(object sender, RoutedEventArgs e)
{
for (int i = 1; i < 5; i++)
{
CheckBox CheckBox1 = (this.FindName(string.Format("Check_D{0}", i)) as CheckBox);
if (CheckBox1 != null)
{
CheckBox1.IsChecked = false;
}
}
}
I think one way of doing it is putting it in an array and whenever one of the 40 buttons are clicked it looks in the array on what to do next? I'm not really sure, thank you!
You can make this simple using one method.
Answer is updated based on this discussion
private void DoWork(int checkboxGroup, bool enable)
{
int start = checkboxGroup * 4;
for (int i = start; i < start + 4; i++)
{
CheckBox CheckBox = this.FindName("CheckBox" + i) as CheckBox;
if (CheckBox != null)
{
CheckBox.IsChecked = enable;
}
}
}
private void Btn1_Click(object sender, RoutedEventArgs e)
{
DoWork(1 , true);
}
private void BtnDisable1_Click(object sender, RoutedEventArgs e)
{
DoWork(1 , false);
}
Because there are 40 methods like this you can use Expression bodied methods. You must have C#6 to use this feature.
private void Btn1_Click(object sender, RoutedEventArgs e) => DoWork(1 , true);
private void BtnDisable1_Click(object sender, RoutedEventArgs e) => DoWork(1 , false);
private void Btn2_Click(object sender, RoutedEventArgs e) => DoWork(2, true);
private void BtnDisable2_Click(object sender, RoutedEventArgs e) => DoWork(2, false);
// and so on
this is my first time posting, but I was wondering how can i call a timer.start() function in a function that is different from form load(). I am using openGl in some graphic application and i want timer to execute according to farmer(newstatus) condition. Thank you in advance for your help.
...
if (farmer(newstatus))
{
timer1.Start();
}
Timer Event:
private void timer1_Tick(object sender, EventArgs e)
{
if (sheepX < 0.7)
sheepX = sheepX + 0.08f;
if (boatX < 0.27)
boatX += 0.04f;
simpleOpenGlControl1.Invalidate();
}
Form Load:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Start();
}
The control which is added from Tool box to the form can by accessed any where in the code. Here is an example to call the Timer control from other methods
private void Form1_Load(object sender, EventArgs e)
{
RunMethod();
}
private void RunMethod()
{
string newstatus = "accept";
if (farmer(newstatus))
{
timer1.Start();
}
}
private bool farmer(string value)
{
bool isValid = false;
if (value == "accept")
isValid = true;
return isValid;
}
I have a text box which the user shouldn't be allowed to write spaces in.
So far I have this code:
private void SearchCriteria_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key.ToString() == "Space")
{
DelLast = SearchCriteria.Text;
NeedsToDelete = true;
}
}
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
NeedsToDelete = false;
}
}
It works, but the cursor is being placed in front of the text. Is there another way to do this?
Save the SelectionStart in your OnKeyDown event and then set it again within the OnKeyUpEvent
private void SearchCriteria_KeyDown(object sender, System.Windows.Input.KeyEventArgs e
{
if (e.Key.ToString() == "Space")
{
DelLast = SearchCriteria.Text;
NeedsToDelete = true;
_selectionStart = SearchCriteria.SelectionStart;
}
}
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
SearchCriteria.SelectionStart = _selectionStart;
NeedsToDelete = false;
}
}
try this :
private void SearchCriteria_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
if (NeedsToDelete == true)
{
SearchCriteria.Text = DelLast;
NeedsToDelete = false;
SearchCriteria.Select(SearchCriteria.Text.Length, 0);
}
}
I have two text box and i want to calculate each text box when the other changed.but my program fall into a loop.
private bool suspendEvents = false;
private void txtYourPercent_ValueChanged(object sender, EventArgs e)
{
if (suspendEvents)
return;
suspendEvents = true;
txtMyPercent.value = 100 - txtYourPercent.value;
suspendEvents = false;
}
private void txtMyPercent_ValueChanged(object sender, EventArgs e)
{
if (suspendEvents)
return;
suspendEvents = true;
txtYourPercent.value = 100 - txtMyPercent.value;
suspendEvents = false;
}
It's my way.This method is disgusting.Any better solution? ("WinForms")
If there is not another solution tell me to delete this question and use my (beep) solution.
You could assign both textboxes the same chenge event handler:
this.txtMyPercent.TextChanged += new System.EventHandler(this.txtPercent_TextChanged);
this.txtYourPercent.TextChanged += new System.EventHandler(this.txtPercent_TextChanged);
and then:
private void txtPercent_TextChanged(object sender, EventArgs e)
{
if (sender == txtMyPercent)
{
txtYourPercent.Text = (100 - int.Parse(txtMyPercent.Text)).ToString();
}
else if (sender == txtYourPercent)
{
txtMyPercent.Text = (100 - int.Parse(txtYourPercent.Text)).ToString();
}
}