Adding values in text boxes

  • Thread starter Thread starter David Plotts
  • Start date Start date
D

David Plotts

I'm a beginner with VB.net, only had one class in college on it.

I can't seem to remember how to add text boxes up.

I want to add the values in text boxes together, and put the value into a
label

This is what I have, but it just adds the values together. So if
txt_roads_pre.text has 5, and txt_parking_pre.text has 6,
lbl_impervious_pre.text would have "56"

lbl_impervious_pre.Text = txt_roads_pre.Text + txt_parking_pre.Text +
txt_driveway_pre.Text + txt_sidewalk_pre.Text + txt_building_pre.Text +
txt_decks_pre.Text + txt_pools_ponds_pre.Text + txt_other_pre.Text
 
I'm a beginner with VB.net, only had one class in college on it.

I can't seem to remember how to add text boxes up.

I want to add the values in text boxes together, and put the value into a
label

This is what I have, but it just adds the values together. So if
txt_roads_pre.text has 5, and txt_parking_pre.text has 6,
lbl_impervious_pre.text would have "56"

lbl_impervious_pre.Text = txt_roads_pre.Text + txt_parking_pre.Text +
txt_driveway_pre.Text + txt_sidewalk_pre.Text + txt_building_pre.Text +
txt_decks_pre.Text + txt_pools_ponds_pre.Text + txt_other_pre.Text

You could just wrap them in CInt() or Int32.Parse() or
Convert.ToInt32() etc...

All of the above will convert the Text property of the textbox into a
integer, which then be added together. If using Option Strict On you
will need to convert the sum back to a string to store in the label
though.

i.e.
lbl_impervious_pre.Text = txt_roads_pre.Text + txt_parking_pre.Text + ...

becomes,

lbl_impervious_pre.Text = CStr(CInt(txt_roads_pre.Text) +
CInt(txt_parking_pre.Text) + ... )


Thanks,

Seth Rowe
 
Great, thanks.

That label only updates with the values when it is clicked on. Is there a
way to have it update as the numbers are changed in the text box?
 
You'd have to capture the Change events for every textbox, which you could
do by adding something like this in your Form_Load event.

AddHandler txt_roads_pre.Changed, AddressOf RecalculateMyNumbers
AddHandler txt_parking_pre.changed, AddressOf RecalculateMyNumbers

and so on, assuming RecalculateMyNumbers is a routine that does what it
says.

Robin S.
Ts'i mahnu uterna ot twan ot geifur hingts uto.
------------------------------------------------
 
AddHandler txt_roads_pre.Changed, AddressOf RecalculateMyNumbers
AddHandler txt_parking_pre.changed, AddressOf RecalculateMyNumbers

Or just attach the event to the RecalculateMyNumbers method with the
handles keyword (only applicable if the controls are added at design
time)

ie

public sub RecalculateMyNumbers() handles txt_roads_pre.Changed,
txt_parking_pre.Changed, ......

end sub

Both do the same, but the second example is what the designer
generates when you map an event, while the addhandler is used mainly
for dynamic mapping of events (like for dynamically created controls).

Thanks,

Seth Rowe
 
Back
Top