Converting Strings to decimals and displaying in forms or textbox

R

RG

I am reading in from a serial port a 16 bit number from 0 to 65536. It
is being read as a string from my microcontroller into VB using DEC5
or 5 digits are displayed. I need to display this value live as a
decimal number so I can manipulate the value.

For instance I have the following line:

TextBox2.Text = buffer1 - 32768

but this generates an error after a few iterations because buffer1 is
a string.
I tried using a label.text also but I get the same problem.

Is there a different control to display numbers?
If not I assume I need to convert.todouble(buffer1).
But this also casues an error.
I also tried converting to double then back to text to display in the
textbox but I am still getting errors.

buffer1 = Convert.ToDouble(buffer1)
buffer1 = buffer1 - 32768
TextBox2.Text = Convert.ToString(buffer1)

Any help would be appreciated.
 
R

RG

No Luck
TextBox2.Text = CInt(buffer1) - 32768
error is: Conversion from string "" to type 'Integer' is not valid.

TextBox2.Text = CDbl(buffer1) - 32768
error is: Conversion from string "" to type 'Double' is not valid.

The reason I am using double is because I need to eventually multiply
the value by a decimal.
I would also like to display it with 2 decimal places.
 
T

tomb

RG said:
I am reading in from a serial port a 16 bit number from 0 to 65536. It
is being read as a string from my microcontroller into VB using DEC5
or 5 digits are displayed. I need to display this value live as a
decimal number so I can manipulate the value.

For instance I have the following line:

TextBox2.Text = buffer1 - 32768

but this generates an error after a few iterations because buffer1 is
a string.
I tried using a label.text also but I get the same problem.

Is there a different control to display numbers?
If not I assume I need to convert.todouble(buffer1).
But this also casues an error.
I also tried converting to double then back to text to display in the
textbox but I am still getting errors.

buffer1 = Convert.ToDouble(buffer1)
buffer1 = buffer1 - 32768
TextBox2.Text = Convert.ToString(buffer1)

Any help would be appreciated.
I would try this:

TextBox2.Text = CInt(buffer1) - 32768

or

TextBox2.Text = CDbl(buffer1) - 32768

although I don't see why you are trying to use doubles for this anyway.

T
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

RG said:
I am reading in from a serial port a 16 bit number from 0 to 65536. It
is being read as a string from my microcontroller into VB using DEC5
or 5 digits are displayed. I need to display this value live as a
decimal number so I can manipulate the value.

For instance I have the following line:

TextBox2.Text = buffer1 - 32768

but this generates an error after a few iterations because buffer1 is
a string.
I tried using a label.text also but I get the same problem.

Is there a different control to display numbers?
If not I assume I need to convert.todouble(buffer1).
But this also casues an error.
I also tried converting to double then back to text to display in the
textbox but I am still getting errors.

buffer1 = Convert.ToDouble(buffer1)

The conversion works fine, but when you put the value back in the string
variable, it's automatically converted back to a string. You need a
separate varable that is of the type double.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top