473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Bound text box

Rob
Assume 3 controls on a form....

Textbox1, Textbox2, and Textbox3

If you want Textbox3 to equal Val(Textbox1) + Val(Textbox2)..

How do you hardcode this to Textbox3 ?
Dec 27 '05 #1
16 2197
Assuming you have already validated that the first 2 textboxes do, in fact
contain numeric data:

Textbox3.Text = CType(Textbox1.Text, Double) + CType(Textbox2.Text, Double)

Or, for better exception management:

Dim x, y, z As Double
Try
x = CType(Textbox1.Text, Double)
y = CType(Textbox2.Text, Double)
z= x + y
Catch e As Exception
Textbox3.Text = "You must enter only numeric data!"
End Try
"Rob" <rw*****@comcast.net> wrote in message
news:AI********************@comcast.com...
Assume 3 controls on a form....

Textbox1, Textbox2, and Textbox3

If you want Textbox3 to equal Val(Textbox1) + Val(Textbox2)..

How do you hardcode this to Textbox3 ?

Dec 27 '05 #2
Rob
Thanks Scott,

My "real" question is.... where do you put the code ? Let's say you are
using the designer... May I put "= CType(Textbox1.Text, Double) +
CType(Textbox2.Text, Double)" into a property of TextBox3... or must it be
Event driven (i.e., "On Click")

"Scott M." <s-***@nospam.nospam> wrote in message
news:uW****************@TK2MSFTNGP10.phx.gbl...
Assuming you have already validated that the first 2 textboxes do, in fact
contain numeric data:

Textbox3.Text = CType(Textbox1.Text, Double) + CType(Textbox2.Text,
Double)

Or, for better exception management:

Dim x, y, z As Double
Try
x = CType(Textbox1.Text, Double)
y = CType(Textbox2.Text, Double)
z= x + y
Catch e As Exception
Textbox3.Text = "You must enter only numeric data!"
End Try
"Rob" <rw*****@comcast.net> wrote in message
news:AI********************@comcast.com...
Assume 3 controls on a form....

Textbox1, Textbox2, and Textbox3

If you want Textbox3 to equal Val(Textbox1) + Val(Textbox2)..

How do you hardcode this to Textbox3 ?


Dec 28 '05 #3
A simple way to do it is in the textchanged event of textbox1 and textbox2
"Rob" <rw*****@comcast.net> wrote in message
news:Q5******************************@comcast.com. ..
Thanks Scott,

My "real" question is.... where do you put the code ? Let's say you are
using the designer... May I put "= CType(Textbox1.Text, Double) +
CType(Textbox2.Text, Double)" into a property of TextBox3... or must it be
Event driven (i.e., "On Click")

"Scott M." <s-***@nospam.nospam> wrote in message
news:uW****************@TK2MSFTNGP10.phx.gbl...
Assuming you have already validated that the first 2 textboxes do, in fact contain numeric data:

Textbox3.Text = CType(Textbox1.Text, Double) + CType(Textbox2.Text,
Double)

Or, for better exception management:

Dim x, y, z As Double
Try
x = CType(Textbox1.Text, Double)
y = CType(Textbox2.Text, Double)
z= x + y
Catch e As Exception
Textbox3.Text = "You must enter only numeric data!"
End Try
"Rob" <rw*****@comcast.net> wrote in message
news:AI********************@comcast.com...
Assume 3 controls on a form....

Textbox1, Textbox2, and Textbox3

If you want Textbox3 to equal Val(Textbox1) + Val(Textbox2)..

How do you hardcode this to Textbox3 ?



Dec 28 '05 #4
You generally put your code into event handlers or methods that are then
called by other code.

There are many events you can choose from, but the first thing you want to
do is identify "when" do you want your code to execute. You wouldn't put
any of this code into anything that has to do with Textbox3 because you want
to see the answer when someone clicks a button (most likely), so the code
goes into the click event of the button you add to the form.
"Rob" <rw*****@comcast.net> wrote in message
news:Q5******************************@comcast.com. ..
Thanks Scott,

My "real" question is.... where do you put the code ? Let's say you are
using the designer... May I put "= CType(Textbox1.Text, Double) +
CType(Textbox2.Text, Double)" into a property of TextBox3... or must it be
Event driven (i.e., "On Click")

"Scott M." <s-***@nospam.nospam> wrote in message
news:uW****************@TK2MSFTNGP10.phx.gbl...
Assuming you have already validated that the first 2 textboxes do, in
fact contain numeric data:

Textbox3.Text = CType(Textbox1.Text, Double) + CType(Textbox2.Text,
Double)

Or, for better exception management:

Dim x, y, z As Double
Try
x = CType(Textbox1.Text, Double)
y = CType(Textbox2.Text, Double)
z= x + y
Catch e As Exception
Textbox3.Text = "You must enter only numeric data!"
End Try
"Rob" <rw*****@comcast.net> wrote in message
news:AI********************@comcast.com...
Assume 3 controls on a form....

Textbox1, Textbox2, and Textbox3

If you want Textbox3 to equal Val(Textbox1) + Val(Textbox2)..

How do you hardcode this to Textbox3 ?



Dec 29 '05 #5

"Rob" <rw*****@comcast.net> wrote in message
news:Q5******************************@comcast.com. ..
Thanks Scott,

My "real" question is.... where do you put the code ? Let's say you are
using the designer... May I put "= CType(Textbox1.Text, Double) +
CType(Textbox2.Text, Double)" into a property of TextBox3... or must it be
Event driven (i.e., "On Click")


In VB6 you'd use the Textbox1_Change and Textbox2_Change events.

Why not TextBox?_TextChanged ?

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
TextBox3.Text = TextBox1.Text & TextBox2.Text

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged

TextBox3.Text = TextBox1.Text & TextBox2.Text

End Sub


Dec 30 '05 #6
> In VB6 you'd use the Textbox1_Change and Textbox2_Change events.

Why not TextBox?_TextChanged ?
You could put this code into a TextChanged event, but it is not the most
common approach (and wasn't in VB 6.0 either). Generally, a button is
created explicitly for users to click to cause some calculation.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox1.TextChanged
TextBox3.Text = TextBox1.Text & TextBox2.Text

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles TextBox2.TextChanged

TextBox3.Text = TextBox1.Text & TextBox2.Text

End Sub


Your code above isn't really the best approach because you have written the
same thing in 2 places. Now, sure you could create a third procedure that
the first two call so that the code isn't duplicated, but this is besides
the point. The point is to determine which *event* you want to trigger the
code.
Dec 30 '05 #7

"Scott M." <s-***@nospam.nospam> wrote in message
news:ey**************@TK2MSFTNGP12.phx.gbl...
In VB6 you'd use the Textbox1_Change and Textbox2_Change events.

Why not TextBox?_TextChanged ?


You could put this code into a TextChanged event, but it is not the most
common approach (and wasn't in VB 6.0 either). Generally, a button is
created explicitly for users to click to cause some calculation.
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox1.TextChanged
TextBox3.Text = TextBox1.Text & TextBox2.Text

End Sub

Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e
As System.EventArgs) Handles TextBox2.TextChanged

TextBox3.Text = TextBox1.Text & TextBox2.Text

End Sub


Your code above isn't really the best approach because you have written
the same thing in 2 places. Now, sure you could create a third procedure
that the first two call so that the code isn't duplicated, but this is
besides the point. The point is to determine which *event* you want to
trigger the code.


The code I gave him was the simplest possible solution and it worked. From
that he can add as much complication as he desires. KISS.
Dec 31 '05 #8
> The code I gave him was the simplest possible solution and it worked. From
that he can add as much complication as he desires. KISS.


You think writing the same code in 2 different places is the simpleset
solution? Uh, well ok.

As I said earlier, it's really a matter of choosing between TextChanged and
a button's Click event handler (as you say, they both work) but there are
those who believe (me among them) that you should give the user a control to
interact with in order for an operation to be triggerd.

Both scenarios are simplistic, it's a usability issue (and with my solution,
there is less code to add).
Dec 31 '05 #9

"Scott M." <s-***@nospam.nospam> wrote in message
news:uz*************@TK2MSFTNGP12.phx.gbl...
The code I gave him was the simplest possible solution and it worked.
From that he can add as much complication as he desires. KISS.


You think writing the same code in 2 different places is the simpleset
solution? Uh, well ok.

As I said earlier, it's really a matter of choosing between TextChanged
and a button's Click event handler (as you say, they both work) but there
are those who believe (me among them) that you should give the user a
control to interact with in order for an operation to be triggerd.

Both scenarios are simplistic, it's a usability issue (and with my
solution, there is less code to add).


You'll never get it.
Dec 31 '05 #10
Rob
Thank you both for responding...

Here is the situation....

I am coming from MS-Access where you may bind a formula directly to a text
box...

What the user's like about this approach (in a Quote providing application)
is that they do not have to do anything to see a "Total" on the form... the
Total gets created as the user add components....

So in Access, you can code such that if the texbox is null, don't show a
total, otherwise, let them see the $ amount... without having to click a
button.

As far as programming goes... I would like to have an "Update Total" button,
but do not know how user's will like that....
"Homer J Simpson" <no****@nowhere.com> wrote in message
news:vwntf.25596$OU5.24963@clgrps13...

"Scott M." <s-***@nospam.nospam> wrote in message
news:uz*************@TK2MSFTNGP12.phx.gbl...
The code I gave him was the simplest possible solution and it worked.
From that he can add as much complication as he desires. KISS.


You think writing the same code in 2 different places is the simpleset
solution? Uh, well ok.

As I said earlier, it's really a matter of choosing between TextChanged
and a button's Click event handler (as you say, they both work) but there
are those who believe (me among them) that you should give the user a
control to interact with in order for an operation to be triggerd.

Both scenarios are simplistic, it's a usability issue (and with my
solution, there is less code to add).


You'll never get it.

Dec 31 '05 #11
I hear what you are saying, but although you may find the automatic
calculation connected directly to the textbox a nice feature, general
usability standards would say you should have a button to perform the
operation (or at least a button in addition to the automatic calculation).

It is certainly possible (as I've said) to attach your code to the
TextChanged event of the textboxes and then have each event handler call a
dedicated routine to do the operation. That's not *wrong*. I'm merely
saying that there are certain aspects of a well-created UI that should be
given consideration and having a button to click is one of them.
"Rob" <rw*****@comcast.net> wrote in message
news:Ss******************************@comcast.com. ..
Thank you both for responding...

Here is the situation....

I am coming from MS-Access where you may bind a formula directly to a
text box...

What the user's like about this approach (in a Quote providing
application) is that they do not have to do anything to see a "Total" on
the form... the Total gets created as the user add components....

So in Access, you can code such that if the texbox is null, don't show a
total, otherwise, let them see the $ amount... without having to click a
button.

As far as programming goes... I would like to have an "Update Total"
button, but do not know how user's will like that....
"Homer J Simpson" <no****@nowhere.com> wrote in message
news:vwntf.25596$OU5.24963@clgrps13...

"Scott M." <s-***@nospam.nospam> wrote in message
news:uz*************@TK2MSFTNGP12.phx.gbl...
The code I gave him was the simplest possible solution and it worked.
From that he can add as much complication as he desires. KISS.

You think writing the same code in 2 different places is the simpleset
solution? Uh, well ok.

As I said earlier, it's really a matter of choosing between TextChanged
and a button's Click event handler (as you say, they both work) but
there are those who believe (me among them) that you should give the
user a control to interact with in order for an operation to be
triggerd.

Both scenarios are simplistic, it's a usability issue (and with my
solution, there is less code to add).


You'll never get it.


Dec 31 '05 #12
> You'll never get it.

Not if you don't explain what it is you are trying to say, I won't. You
stated that your solution was the simplest possible one, and then touted the
KISS principal as if what I showed was somehow rocket science. You and I
showed the same thing. The only difference is that you put your code in 2
places and have it being invoked from the TextChanged event and I put it in
one place and have it being invoked from a button's Click event.

Now, tell me again how your solution is the simplest and what it is that I
don't get?
Dec 31 '05 #13

"Scott M." <s-***@nospam.nospam> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
You'll never get it.
Not if you don't explain what it is you are trying to say, I won't. You
stated that your solution was the simplest possible one, and then touted
the KISS principal as if what I showed was somehow rocket science. You
and I showed the same thing. The only difference is that you put your
code in 2 places and have it being invoked from the TextChanged event and
I put it in one place and have it being invoked from a button's Click
event.

Now, tell me again how your solution is the simplest and what it is that I
don't get?


You are in an education mode. This user requires the simplest possible
solution. Once he sees it working he can add whatever complication or
enhancements he desires. This is how to teach:-

First, show him a bubble sort. Demonstrate that it works.
Second, show him how slowly it works.
Third, show him a Shell sort. Now he understands.

BTW, a one line routine is not optimized for speed or size by calling it as
a subroutine instead of running inline.

Dec 31 '05 #14
> You are in an education mode. This user requires the simplest possible
solution. Once he sees it working he can add whatever complication or
enhancements he desires.
I'll ask you again (third time) how puting the code in two different places,
rather than just one is the simplest possible solution? Since your code and
mine are the same, the only difference becomes where it is put and how much
coding is involved. Your code works perfectly fine, but requires more code
to make it work (and more maintenance if the calculation were to change), so
what is it that you believe is simpler than one line of code:

TextBox3.Text = TextBox1.Text & TextBox2.Text

Really, step off your high horse for a second and think about this. This is
ONE line of code put in ONE place - - seems pretty darn simple to me.
This is how to teach:-

First, show him a bubble sort. Demonstrate that it works.
Second, show him how slowly it works.
Third, show him a Shell sort. Now he understands.
Thanks for the tip. Since I own and operate a technology training company
(TechTrainSolutions.com) and have been doing IT education for 15 years and
assorted industry training for 10 years before that, I'll stick to my
methodology, which works exceptionally well thanks. It's based on
well-known education principals for teaching adults (which is different than
how you teach children).
BTW, a one line routine is not optimized for speed or size by calling it
as a subroutine instead of running inline.


That's not necessarially true. There are several reasons why repeating the
same calculation in more than one place is a bad idea, poor technique and
why just about every professional developer will tell you so:

1. It's not good because if the calculation/algorythm needs to change, you
have to change it in more than one place.
2. It's not good because you may miss one of the places it needs to be
updated.
3. It also opens the door to the possibility that the two (or more) lines
will inadvertently become different from each other, causing unexpected
results.

As for optimization, calling a method rather than doing the operation from
one within one method will have virtually no impact on performance. In
other words, contrary to your statement, a one line routine's performance is
not impacted by the fact that it needs to be called from another method.

It seems to me that you just want to argue no matter what is said. If you
believe your technique is better, great - use it. But, please don't make
claims that are incorrect and have no basis in fact.

Happy New Year!
Dec 31 '05 #15
By the way, your line of code:

TextBox3.Text = TextBox1.Text & TextBox2.Text

wouldn't produce the mathematical addition of the two values in either VB
6.0 or VB.NET, it would produce the concatenation of the two values.

The correct VB.NET statement is:

Textbox3.Text = CType(Textbox1.Text, Double) + CType(Textbox2.Text, Double)

The CType function calls are required because Option Strict should be turned
on at all times (you knew that, right?) and with Option Strict turned on,
you can't attempt to add Textbox values (or do any math at all) without
casting the value first.

-Scott
Dec 31 '05 #16
Correction:

Textbox3.Text = CType(CType(Textbox1.Text, Double) + CType(Textbox2.Text,
Double), String)
"Scott M." <s-***@nospam.nospam> wrote in message
news:eT**************@TK2MSFTNGP09.phx.gbl...
By the way, your line of code:

TextBox3.Text = TextBox1.Text & TextBox2.Text

wouldn't produce the mathematical addition of the two values in either VB
6.0 or VB.NET, it would produce the concatenation of the two values.

The correct VB.NET statement is:

Textbox3.Text = CType(Textbox1.Text, Double) + CType(Textbox2.Text,
Double)

The CType function calls are required because Option Strict should be
turned on at all times (you knew that, right?) and with Option Strict
turned on, you can't attempt to add Textbox values (or do any math at all)
without casting the value first.

-Scott

Dec 31 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Paul Edwards | last post by:
I am writing in c# with Visual Studio 2003. I am creating a form that shows one record from a Dataset that is populated with only one record from SQL Server 2000. I am using bound controls, both...
4
by: vulcaned | last post by:
Hi All, Hopefully I explain this well........ In Access97 I have a form which has a tab control on it, each tab has a sub-form which is bound to its appropriate table(I'll call them 'Detail'...
6
by: BBM | last post by:
Hi, I'm having trouble getting control data binding to work. As I understand it, the simplest form of databinding requires three things: 1) A control (say a textbox) on a form or user...
9
by: Greg | last post by:
Binding Manager & dataset - won't add record I've got an untyped dataset with controls bound through code. The user can select a question number from a bound combobox, and the question number and...
7
by: Andrew McKendrick | last post by:
Hi, I've noticed a bug in VB.NET (latest .NET Framework)... - I have a TabControl on a form with several tabs. - Each tab contains text boxes that are bound to fields in a data source...
6
by: timbobd | last post by:
I have a Windows form that displays a database table in a DataGrid. When you click on a row, the row's values get copied to bound TextBoxes below, and when the "Save" button is clicked the database...
4
by: jon f kaminsky | last post by:
Hi- I've seen this problem discussed a jillion times but I cannot seem to implement any advice that makes it work. I am porting a large project from VB6 to .NET. The issue is using the combo box...
1
by: Bill | last post by:
Problem: Combo box data disappears from view when a requery is done See "Background" below for details on tables, forms & controls On a form, I want to use the setting of bound combo box C1...
0
by: Frnak McKenney | last post by:
Can I use a bound ComboBox for both browsing and editing? I'm working on a small, standalone database application using Visual C#.NET 2003 and an Access data file. In order to keep the number...
0
by: Mike | last post by:
So here's the situation (.NET 2.0 btw): I have a form, and on this form is a textbox among many other databound controls. The textbox is bound to a field in a data table via the Text property. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.