Solved

RichTextbox or TextBOX

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Import a txt file into a DataGridView and TextBox 20 52
ASP/VB email question 4 46
Complex SQL statement in VB.NET 7 31
Passing data between Forms 3 20
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…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

830 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