Solved

RichTextbox or TextBOX

Posted on 2014-10-06
3
212 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

737 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