Solved

RichTextbox or TextBOX

Posted on 2014-10-06
3
206 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
Comment Utility
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
Comment Utility
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
Comment Utility
If you double click in the textbox all text is selected then it overwrites.  This is normal behavior.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

772 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

10 Experts available now in Live!

Get 1:1 Help Now