Solved

RichTextbox or TextBOX

Posted on 2014-10-06
3
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

724 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