Solved

RichTextbox or TextBOX

Posted on 2014-10-06
3
208 Views
Last Modified: 2014-10-10
Hi Expert
I like to bind a variable that is numeric and has 2 decimal places.
I would like to show 0.00 in richtextbox or textbox when the variable is 0
similar to what we has in Foxpro.
and when you start over writing it and press . it goes to decimal position
thanks
0
Comment
Question by:b001
  • 2
3 Comments
 
LVL 12

Accepted Solution

by:
ktaczala earned 500 total points
ID: 40363638
Try something like this:

  Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        'navagation keys don't matter
        If e.KeyChar = "." Then Exit Sub 'decimal
        If e.KeyChar = Convert.ToChar(Keys.Back) Then Exit Sub 'backspace
        If (Not Char.IsDigit(e.KeyChar)) Then
            e.Handled = True ' ignore anything that's not numeric
        End If
    End Sub

    Private Sub TextBox1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
        If TextBox1.Text = "" Then Exit Sub 'leave it blank or TextBox1.Text = "0.00"
        Try
            Dim testval As Decimal = CDbl(TextBox1.Text)
        Catch ex As Exception
            If MessageBox.Show("Invalid Character in TextBox1 Field", "Invalid Character", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) = Windows.Forms.DialogResult.Retry Then
                e.Cancel = True
                Exit Sub
            Else
                TextBox1.Text = "0.00"
            End If
        End Try
        NumberFormat(TextBox1)
        End Sub

    Private Sub NumberFormat(ByVal Text As TextBox)
        Dim temptext As TextBox = Text
        If Len(temptext.Text) > 0 Then
            'if no decimal then add decimal
            temptext.Text = FormatNumber(CDbl(temptext.Text), 2)
            Dim x As Integer = temptext.SelectionStart.ToString
            If x = 0 Then
                temptext.SelectionStart = Len(temptext.Text)
                temptext.SelectionLength = 0
            Else
                temptext.SelectionStart = x
                temptext.SelectionLength = 0
            End If
        End If

        'Write it back to the visible text box
        Text.Text = "$" & temptext.Text ' remove "$" &  if you don't want to display dollar sign.
    End Sub

Open in new window

0
 

Author Comment

by:b001
ID: 40366249
Hi ktaczala
Thanks. That is working ok.
But When I start typing it does not over write it instead existing text start scrolling to right.
Is there way to stop existing text scrolling and over wright with new keys pressed.
0
 
LVL 12

Expert Comment

by:ktaczala
ID: 40367571
If you double click in the textbox all text is selected then it overwrites.  This is normal behavior.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now