null TextBoxes in Database Entry Form

Hello,

1. The Form has TextBoxes c1,c2,c3 receiving currency and c4 which shows the totals:

c4=c1+c2+c3

As it is now, an "Add New" Button_Click event initialize all the TextBoxes to "0.0". While this works, it places a burden on the user when entering data (he has to erase the "0.0" or might make a mistake say typing "1" without erasing "0.0" turning it into "10.0").

2. How could the c4 always show some result, starting from the initial 0.0 to c4=c1 then c4=c2 c4=c3 or c4=c1+c2 or c4=c1+c3 or c4=c1+c2+c3

Thanks,

[614 byte] By [CesarFrancisco] at [2007-12-28]
# 1

Here's an example - basically, the getfocus and lostfocus events are used to wipe out the textbox, and restore the 0.0 in case nothing is entered. A try/catch is used in case the value in the textbox isn't convertible to a decimal, and the textchanged event is used to update the results in the other textbox:

Public Class Form1

Private Sub TextBox1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.GotFocus
If TextBox1.Text = "0.0" Then TextBox1.Text = ""
End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
If TextBox1.Text = "" Then TextBox1.Text = "0.0"
End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Try
TextBox2.Text = CDec(TextBox1.Text) + CDec(TextBox1.Text)
Catch ex As Exception
End Try

End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TextBox1.Text = "0.0"
End Sub
End Class

AlexMoura at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2

Hi,

Set the textboxes to be blank.

Then the sum is>>

Dim result As Double

If c1.text="" then

c1.text="0.0"

EndIf

If c2.text="" then

c2.text="0.0"

EndIf

If c3.text="" then

c3.text="0.0"

EndIf

result=CDBL(c1.text + c2.text + c3.text)

c4.text=CSTR(result)

That should do it. :-)

Regards,

S_DS

Spidermans_DarkSide at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

You could do this prior to calculation


Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
resetdefaultvalues()
End Sub


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
resetdefaultvalues() '//Reset Default value for textboxes

Dim sglAnswer As Single = 0
For Each t As Control In Me.Controls
If TypeOf (t) Is TextBox And t.Name.StartsWith("TextBox") Then
sglAnswer = sglAnswer & CType(t.Text, Single)
End If
Next

Me.txtAnswer.Text = sglAnswer.ToString
End Sub

Sub resetdefaultvalues()
For Each t As Control In Me.Controls
If TypeOf (t) Is TextBox And t.Name.StartsWith("TextBox") Then
If t.Text.Length = 0 Then t.Text = "0.0"
End If
Next
End Sub
End Class

Or do it when the textbox loses focus



Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Recalc()
End Sub

Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus, TextBox2.LostFocus
If CType(sender, TextBox).Text.Length = 0 Then
CType(sender, TextBox).Text = "0.0"
End If
Recalc()
End Sub


Sub Recalc()
For Each t As Control In Me.Controls
If TypeOf (t) Is TextBox And t.Name.StartsWith("TextBox") Then
If t.Text.Length = 0 Then t.Text = "0.0"
End If
Next


Dim sglAnswer As Single = 0
For Each t As Control In Me.Controls
If TypeOf (t) Is TextBox And t.Name.StartsWith("TextBox") Then
sglAnswer = sglAnswer & CType(t.Text, Single)
End If
Next

Me.txtAnswer.Text = sglAnswer.ToString
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Recalc()
End Sub
End Class




spotty at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4

Hi Spotty,

Thanks for your time. The EntryForm is the front-end to a Database and a somewhat more complicated that it was posted originally. (It has other TextBoxes like Date, ID number and Location, UnitPrice).

As it is now, the "AddNewRecord" Button loads a fresh EntryForm. This event also initialize 2 set of 3 TextBoxes with "0.0" .

Three calculations are performed. Ctotal=C1+C2+C3, D1=C1*UnitPrice, Etotal=D1+D2+D3. All of them are declared as Single.

The user Tabs through the TextBoxes and enters data. Ctotal_OnFocus on the final performs C1+C2+C3 and Etotal_OnFocus D1+D2+D3.

So far so good. But what if, the user made a mistake and goes back to any of the TextBoxes. Well, the cursor stucks in the field unless the user type a number. For this reason, every TextBox have a TextBox_MouseEnter event which rewrites "0.0" into the field of concern.

To do: As it is now, the fields Etotal or Ctotal would no update on the fly, initilazation is ugly ( C1TextBox.Text = "0.0",... C3TextBox.Text = "0.0"; D1TextBoxText="0.0" )ad nauseam. Neither are the variable declarations are charming.

I hope hearing from you,

P.S. Thanks again. This sub() thing looks good. It is new for this newbie - so I have to sleep over it.

CesarFrancisco at 2007-9-4 > top of Msdn Tech,Visual Basic,Visual Basic General...