check string value through specific time - c#

I want to check the string value for a specified time. I set it. During this time you will press the button to change the value of the string and achieve the condition. If you did not press the button during the specified time you will not check the condition. If the time is over, the condition will not be met . When using this code, the condition is always met :( :
string value;
private void button3_Click(object sender, EventArgs e)
{
IAsyncResult result;
Action action = () =>
{
do //loop to check value through 10s
{
return;
}
while ( value == "");
};
result = action.BeginInvoke(null, null);
if(result.AsyncWaitHandle.WaitOne(10000))
//wait 10s to check response
{
listBox2.Items.Add("good"); // if response string value != ""
}
else
{
listBox2.Items.Add("bad");
// if response string value == "" or timeout
}
}
private void button4_Click(object sender, EventArgs e)
{
value = "best"; // add value
}

This would be my approach to your problem, that do while loop, wont do any good honestly
string value;
private void button3_Click(object sender, EventArgs e)
{
WaitSecondsAndExecute(10);
}
private async void WaitSecondsAndExecute(int seconds)
{
for (int i = 0; i < seconds; i++)
{
await Task.Delay(1000);
if (value != null)
{
break;
}
}
if (value != null)
{
listBox2.Items.Add("good"); // if response string value != ""
}
else
{
listBox2.Items.Add("bad");
// if response string value == "" or timeout
}
}
private void button4_Click(object sender, EventArgs e)
{
value = "best"; // add value
}
if you need it faster its a matter of dividing the delay and multiplying the seconds for the same amount

Related

C# How to disable button when the value is zero

I'm a beginner to C# programming and I really need some help :)
I made 2 buttons that will increment or decrement the value in the text box which is initially zero.
private void bminus_Click(object sender, EventArgs e)
{
NUMBER--;
textBox2.Text = NUMBER.ToString();
}
private void bplus_Click(object sender, EventArgs e)
{
NUMBER++;
textBox2.Text = NUMBER.ToString();
}
Will there be a way that the decrement button will be disabled if the value is 0 so that there won't be negative numbers? Thank you, I'd really appreciate anyone's help! :)
private void bminus_Click(object sender, EventArgs e)
{
NUMBER--;
textBox2.Text = NUMBER.ToString();
if(NUMBER == 0){
bminus.Enabled = false;
}
}
private void bplus_Click(object sender, EventArgs e)
{
NUMBER++;
textBox2.Text = NUMBER.ToString();
bminus.Enabled = true;
}
That should do.
You can just directly insert logical state NUMBER not being 0 to your textBox2 Enabled property:
textBox2.Enabled = NUMBER != 0;
Or make a separate function from this:
private bool IsNotZero(double n)
{
return n != 0;
}
then:
textBox2.Enabled = IsNotZero(NUMBER);
All you need to do is add the following to the textBox2.TextChanged event handler and it should work with your current code.
private void textBox2_TextChanged(object sender, EventArgs e)
{
int output;
if (int.TryParse(textBox2.Text, out output))
{
bminus.Enabled = int.Parse(textBox2.Text) > 0;
}
}
NOTE: No one here has specified checking for an integer so I added int.TryParse and int.Parse for this purpose.
Button have a property named Enabled which you can set to true or false based on your logic.
Check the value using if condition. If the value is null or zero you can use button.enabled = false; code.if(value == zero || value == null) { button.enabled=false;} else {button.enabled = true;}

C# show MessageBox based on Combobox SelectedText

How do I show a messagebox based on the various SelectedText in the Combobox? It currently just returns a NULL value when running.
I need to show the specific messagebox for each Combobox Text as once I can do this then depending on the SelectedText different SQL Connections will be used and Queries run.
I've included my code below. After some research it seems that the SelectedText control will always return a null value as it loses focus. How do I get around this?
private void button2_Click(object sender, EventArgs e)
{
if(comboSelectServer.SelectedText == "SERV1")
{
MessageBox.Show("SERV1");
}
else if(comboSelectServer.SelectedText == "SERV2")
{
MessageBox.Show("SERV2");
}
else if(comboSelectServer.SelectedText == "SERV3")
{
MessageBox.Show("SERV3");
}
}
Try this.
if (comboSelectServer.Text == "SERV1")
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.Text == "SERV2")
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.Text == "SERV3")
{
MessageBox.Show("SERV3");
}
However, this is easier...
if (comboSelectServer.SelectedIndex == 0) //SERV1
{
MessageBox.Show("SERV1");
}
else if (comboSelectServer.SelectedIndex == 1) //SERV2
{
MessageBox.Show("SERV2");
}
else if (comboSelectServer.SelectedIndex == 2) //SERV3
{
MessageBox.Show("SERV3");
}
Try like this
private void button2_Click(object sender, EventArgs e)
{
if(comboSelectServer.SelectedItem.ToString()== "SERV1")
{
MessageBox.Show("SERV1");
}
else if(comboSelectServer.SelectedItem.ToString()== "SERV2")
{
MessageBox.Show("SERV2");
}
else if(comboSelectServer.SelectedItem.ToString()== "SERV3")
{
MessageBox.Show("SERV3");
}
}
Maybe I'm missing something, but why not simply do:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(comboSelectServer.SelectedItem.ToString());
}

Decrementing a double value using a button puts it back to 0. C#

I am using a text box to show quantity. Using 2 buttons I want to increment or decrements a value. So far increment works but when I try to decrement the same value it puts it back to 0 instead of going from 5 to 4.
private void button3_Click(object sender, EventArgs e)
{
double val = double.Parse(textBox1.Text);
while (val != 0)
{
val--;
}
textBox1.Text = val.ToString();
}
This is the code I am using.
You are using a while loop. That means it will decrement the value until it becomes 0.
Change your while to an if and it should work as expected.
private void button3_Click(object sender, EventArgs e)
{
double val = double.Parse(textBox1.Text);
if (val != 0)
val--;
textBox1.Text = val.ToString();
}
You are using while construction that does val-- util it is 0
In your case you simply need to do this:
private void button3_Click(object sender, EventArgs e) {
double val = double.Parse(textBox1.Text);
if (val > 0)
val--;
textBox1.Text = val.ToString();
}
In addition I recommend you to use double.TryParse method. So you don't need to add try ... catch statement:
private void button3_Click(object sender, EventArgs e) {
double val;
bool perseOk = double.TryParse(textBox1.Text, out val);
if (perseOk && val > 0) {
val--;
} else {
val = 0;
}
textBox1.Text = val.ToString();
}

Textbox null value when parsing and messagebox notification

I have made a form which works perfectly fine when the fields are filled in. If you click the "convert" button with a blank textbox, it throws an error due to parsing a null value.
Obviously this means that I've declared my variable upon the button click.
I would also like a message box to pop up if the field is empty, to prompt the user to enter data.
Here is the code I have for the convert button:
private void exitButton_Click(object sender, EventArgs e)
{
//closes the form
this.Close();
}
private void convertButton_Click(object sender, EventArgs e)
{
decimal measurementDecimal = decimal.Parse(enterTextBox.Text);
//if else arguments for radio buttons
if (string.IsNullOrWhiteSpace(enterTextBox.Text))
{
MessageBox.Show("Please enter a value");
}
else if (inchesFromRadioButton.Checked && (inchesToRadioButton.Checked))
{
convertedTextBox.Text = measurementDecimal.ToString();
}
else if (inchesFromRadioButton.Checked && (feetToRadioButton.Checked))
{
convertedTextBox.Text = (measurementDecimal / 12).ToString();
}
else if (inchesFromRadioButton.Checked && (yardsToRadioButton.Checked))
{
convertedTextBox.Text = (measurementDecimal / 36).ToString();
}
else if (feetFromRadioButton.Checked && (inchesToRadioButton.Checked))
{
convertedTextBox.Text = (measurementDecimal * 12).ToString();
}
else if (feetFromRadioButton.Checked && (feetToRadioButton.Checked))
{
convertedTextBox.Text = measurementDecimal.ToString();
}
else if (feetFromRadioButton.Checked && (yardsToRadioButton.Checked))
{
convertedTextBox.Text = (measurementDecimal / 3).ToString();
}
else if (yardsFromRadioButton.Checked && (inchesToRadioButton.Checked))
{
convertedTextBox.Text = (measurementDecimal * 36).ToString();
}
else if (yardsFromRadioButton.Checked && (feetToRadioButton.Checked))
{
convertedTextBox.Text = (measurementDecimal * 3).ToString();
}
else if (yardsFromRadioButton.Checked && (yardsToRadioButton.Checked))
{
convertedTextBox.Text = measurementDecimal.ToString();
}
else
{
MessageBox.Show("Parameters not set. Please select a 'From' and 'To'");
}
Solution 1 : You can perform null or empty check before parsing the input value.and if it is invalid display warning and return from the method.
Try This:
private void convertButton_Click(object sender, EventArgs e)
{
//if else arguments for radio buttons
if (string.IsNullOrWhiteSpace(enterTextBox.Text))
{
MessageBox.Show("Please enter a value");
return;
}
/*Your remaining code here*/
decimal measurementDecimal = decimal.Parse(enterTextBox.Text);
Solution 2: You can use decimal.TryParse() method for checking the valid decimal value.
From MSDN:
Converts the string representation of a number to its Decimal
equivalent. A return value indicates whether the conversion succeeded
or failed.
private void convertButton_Click(object sender, EventArgs e)
{
decimal measurementDecimal ;
if (!decimal.TryParse(enterTextBox.Text,out measurementDecimal))
{
MessageBox.Show("Please enter a valid value");
return;
}
else
{
/*Your remaining code here*/
}

_textChanged event is giving me error "Object reference not set to an instance of an object"

I'm new to C# so still finding my way around.
I have a button I want to enable only when a user enter text to textbox.
I get this error - "Object reference not set to an instance of an object".
Here is the related code (without the using and variables):
public MainWindow()
{
MessageBox.Show("Make sure to edit Settings tab.");
InitializeComponent();
if (startTextBox.Text == "0") // Checks to see if a textbox has some text other than zero. if no than the user cannot press button1 yet.
{
button1.IsEnabled = false;
}
else
{
button1.IsEnabled = true;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (radioButton1.IsChecked == false)
{
label17.Content = "No Hourly wage was set.";
}
}
private void add(object sender, RoutedEventArgs e) /// here is a very long method so I've removed its content.
}
public void printTime()
{
int Sum = (this.EndInt - this.StartInt);
int Money = (Sum * this.L1001);
label16.Content = Sum;
label17.Content = Money;
if ((textBox1.Text == "0") && ((textBox2.Text == "0") || (textBox3.Text == "0")))
{
label17.Content = "No Hourly wage was set.";
}
}
public void printTime2()
{
int Sum = (this.EndInt - this.StartInt);
MessageBox.Show("Is it possible that you've worked - " + Sum + " Hours?");
}
public void printTime3()
{
int Sum = (this.EndInt - this.StartInt);
int Money = (Sum * this.L1001);
label16.Content = Sum;
label17.Content = Money;
if (textBox1.Text == "0")
{
label17.Content = "No Hourly wage was set.";
}
}
public int Convert(String S)
{
int i = int.Parse(S);
return i;
}
// Input Validation For Excepting Integers Only!
private void input(object sender, TextCompositionEventArgs e)
{ CheckIsNumeric(e); }
private void CheckIsNumeric(TextCompositionEventArgs e)
{
int result; if (!(int.TryParse(e.Text, out result) || e.Text == "."))
{ e.Handled = true; MessageBox.Show("Numbers Only"); }
}
private void startTextBox_TextChanged(object sender, TextChangedEventArgs e)
{
button1.IsEnabled = true;
}
}
}
It's the scope problem. You didn't show where button1 is defined. But inside your event handler startTextBox_TextChanged, button1 definition is nowhere to be found (actually it needs to be instantiated as well). Since you try to invoke a method on an object (button1) which has not been instantiated yet, that exception was thrown.
If you post more than just those snippets, either I or someone else might be able to further help you.

Categories

Resources