Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Placing result of  a calculation into a textbox

Posted on 2005-03-17
Medium Priority
Last Modified: 2010-04-23
I have an input form on which I have created a button which invokes a little calculator I have written to do simple arithmetic calculations.

The result in my little calculator is placed into a textbox called RESULT on the calculator form.

I would like to close my calculator and have the result stuffed into the textbox which has the focus on my  original form.

How can I get the value from the  RESULT textbox transferred to the  original textbox  which has the focus in VBNet.

(I have done this before in VB6 using a  GotFocus sub for every textbox but there must be an easier way!!)

Question by:GREEBA
  • 2
  • 2
LVL 34

Expert Comment

by:Brian Crowe
ID: 13566899
dim frm as new frmCalculator
dim strResult = frm.textbox1.text

The better way to do this would be to add a public readonly Result property to your calculator that can be accessed from the parent form.

Expert Comment

ID: 13571115
Or you could track which text box last had focus.

Private lastFocus as String

Private Sub FocusTextbox(sender as System.Object, e as System.EventArgs)

   lastFocus = sender.name

End Sub

Private Sub AddHandlers()
   AddHandler txtCalc1.Enter, AddressOf FocusTextbox
   AddHandler txtCalc2.Enter, AddressOf FocusTextbox
End Sub

Call AddHandlers from the main program block. Then any time one of the textboxes is Entered, its name becomes the value of lastFocus. A Select statement after you finish with the calculator will tell you where to dump the result.


Author Comment

ID: 13576480
To ctm5

I've tried it out and it seems it will give me what I want. Could you tell me what you mean by your last sentence
"A select statement... tell you where to dump the result." I don't know what this means. I've increased the points
value for your help.


Accepted Solution

ctm5 earned 800 total points
ID: 13576562
So you have your calculated value and now you want to stick it in the right text box. So a select statement based on the value of lastFocus tells you where to go:

Select Case lastFocus
   Case "txtCalc1"
        txtCalc1.Text = myValue
   Case "txtCalc2"
        txtCalc2.Text = myValue
End Select

But in the meantime, I remembered that there is an even easier way to do this. If you Dim lastFocus as a TextBox rather than a string, you don't need the Select statement at all. You would also change FocusTextBox so it reads: lastFocus = sender. Then when you are ready to dump your calculated value into the appropriate textbox, you just need to add a statement like this: lastFocus.Text = myValue.


Author Comment

ID: 13589707
The Dim lastFocus as textbox works brilliantly... many thanks

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question