blah blah blah is here! blah blah » Close

up0down
link

private void setSingleDACVoltage(TextBox tb_OutputNumber_DAC, TextBox tb_Voltage_DAC)
{
double Volt_Value_DAC = Convert.ToDouble(tb_Voltage_DAC.Text);

String hexNumber = tb_OutputNumber_DAC.Text;
int i = Int32.Parse(hexNumber, NumberStyles.HexNumber);
byte Output_Number_DAC = Convert.ToByte(i);

if (Volt_Value_DAC >= -15 && Volt_Value_DAC <= 15)
{
bool Volt_Set_DAC;
Volt_Set_DAC = digitalIODll.setSingleDACVoltage(Output_Number_DAC, Volt_Value_DAC);

if (Volt_Set_DAC == true)
{
return;
}
else
{
MessageBox.Show("ERROR - unable to set DAC voltage, please check board connection");
}
}
else
{
MessageBox.Show("Voltage out of range");
}

}

Hello, the above code is to be able to set some voltages. I want to make sure that if the text box is empty, then I should consider that and make the textbox text = 0, so that i can convert the text to double. I tried if statements, doesnt seem to work .... any suggestions ?

last answered 2 years ago

1 answers

up0down
link

Try:

private void setSingleDACVoltage(TextBox tb_OutputNumber_DAC, TextBox tb_Voltage_DAC)
{
double Volt_Value_DAC = 0.0;

if(tb_Voltage_DAC.Text != "")
{
Volt_Value_DAC = Convert.ToDouble(tb_Voltage_DAC.Text);
}

String hexNumber = "0";

if (tb_OutputNumber_DAC.Text.ToLower().StartsWith("0x"))
{
tb_OutputNumber_DAC.Text = tb_OutputNumber_DAC.Text.Substring(2);
}

if (tb_OutputNumber_DAC.Text != "")
{
hexNumber = tb_OutputNumber_DAC.Text;
}

int i = Int32.Parse(hexNumber, NumberStyles.HexNumber);
byte Output_Number_DAC = Convert.ToByte(i);

if (Volt_Value_DAC >= -15 && Volt_Value_DAC <= 15)
{
bool Volt_Set_DAC = digitalIODll.setSingleDACVoltage(Output_Number_DAC, Volt_Value_DAC);

if (Volt_Set_DAC)
{
return;
}
else
{
MessageBox.Show("ERROR - unable to set DAC voltage, please check board connection");
}
}
else
{
MessageBox.Show("Voltage out of range");
}

}

Feedback