Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox?
Format data before loading into textbox? Format data before loading into textbox?
Format data before loading into textbox?
Go Back  Xtreme Visual Basic Talk > > > Format data before loading into textbox?


Reply
 
Thread Tools Display Modes
  #1  
Old 04-23-2013, 10:05 AM
RM33 RM33 is offline
Newcomer
 
Join Date: Jan 2013
Posts: 20
Default Format data before loading into textbox?


I am using VB.Net 2010.

This is the problem. I have data in an Oracle database. The numbers in Oracle are not in currency format. They a straight up numbers. No dollar signs or commas. When I load the data into a textbox, I want the data to be converted into a currency format. Here is the problem:

1.) Data binding is out. I am creating custom controls. These controls need to be generic so we can use in different applications.

2.) Textchanged event not working. The problem with textchange is that it works on every key stroke. Sure, when I first load the data it works, but then I get problems when typing.

I need the textbox to respond to an outside force like textbox1.text = 123.45.

So what can be done? Is there a way of coding "not while keypress" so the textchanged event only fires when the end user is not typing?
Reply With Quote
  #2  
Old 04-23-2013, 10:33 AM
DrPunk's Avatar
DrPunkFormat data before loading into textbox? DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

My first comment would be when things like this are very hard to do and you end up trying to fudge stuff to get it to work then it's generally a design issue and it'd be better to come up with a different design than try and force the design to work.

Ignoring that, what you seem to be asking is that you want to ignore the TextChanged event if it was caused by the user typing. Hopefully I've got that right.

You know that a user is typing into a control using the KeyDown, KeyUp, and KeyPress events.

Setting up a quick test shows the order of those events and the TextChanged event.

Code:
    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        Debug.Print("KeyDown")
    End Sub

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Debug.Print("KeyPress")
    End Sub

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        Debug.Print("KeyUp")
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        Debug.Print("TextChanged")
    End Sub
That produces the following result...
Quote:
KeyDown
KeyPress
TextChanged
KeyUp
So, if you log when a user has pressed a key down then when the TextChanged event fires you can check that and know if a user has typed the change or not. Remembering to clear the log when the key goes up again.
Code:
    Dim keyPressed As Boolean = False

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        keyPressed = True
    End Sub

    Private Sub TextBox1_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyUp
        keyPressed = False
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If keyPressed Then
            debug.print("Changed by user typing")
        Else
            debug.print("NOT changed by typing")
        End If
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        TextBox1.Text = "Not changed by user typing"
    End Sub
Button1 there puts some text in the textbox to show it spots that a user didn't type the change.

It's not fool proof. The catching the key up is VERY important and can sometimes be missed. For example I originally had messageboxes showing the message in the TextChanged, but that caused it to miss the keyup event.
__________________
There are no computers in heaven!

Last edited by DrPunk; 04-23-2013 at 10:38 AM.
Reply With Quote
  #3  
Old 04-23-2013, 10:58 AM
DrPunk's Avatar
DrPunkFormat data before loading into textbox? DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Can you not just override the Text property?

Code:
Public Class MyTextBox
    Inherits TextBox

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)
            MyBase.Text = value

            ' Format the value like you seemed to be trying in TextChanged

        End Set
    End Property
End Class
Users cannot change the text of a textbox via the .Text property. Only code (aka outside force) can.

Seems far more simple to me. VB6 to .NET in 2 posts.
__________________
There are no computers in heaven!
Reply With Quote
  #4  
Old 04-23-2013, 11:07 AM
Gruff's Avatar
GruffFormat data before loading into textbox? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Your Pardon Doctor.

I couldn't get your inherited textbox example to work.
I would much prefer to use your suggestion than my kludges.

could you expand on your post with tested code?

RM33,

What about using a Masked Edit Control? Visually it will show the dollar sign and commas.
The data whether typed in or pushed in from the database will be displayed as you want.
The Raw data in the control will not have those modifications.

Another simple method if you can live without the commas is to use a label placed before the textbox set to a Dollar sign.

Yet another option would be to place a label over the textbox. Have the label show the formatted value. If the user clicks on the label make the label invisible. The user types in the changes minus any dollar signs or commas. When he presses the return key the la bel is updated with the formatted value and made visible again. Update both the label and the textbox when changes are made via the database.

In practice it would be fairly seamless.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T

Last edited by Gruff; 04-23-2013 at 12:00 PM.
Reply With Quote
  #5  
Old 04-24-2013, 03:25 AM
DrPunk's Avatar
DrPunkFormat data before loading into textbox? DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

This code, if the .Text property is set to a number it will format it as currency. Anything a user types into the textbox is not affected.
Code:
Public Class MyTextBox
    Inherits TextBox

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)

            If IsNumeric(value) Then
                MyBase.Text = Format(CDbl(value), "c")
            Else
                MyBase.Text = value
            End If

        End Set
    End Property
End Class
__________________
There are no computers in heaven!
Reply With Quote
  #6  
Old 06-17-2013, 09:23 AM
RM33 RM33 is offline
Newcomer
 
Join Date: Jan 2013
Posts: 20
Default

Quote:
Originally Posted by DrPunk View Post
This code, if the .Text property is set to a number it will format it as currency. Anything a user types into the textbox is not affected.
Code:
Public Class MyTextBox
    Inherits TextBox

    Public Overrides Property Text() As String
        Get
            Return MyBase.Text
        End Get
        Set(ByVal value As String)

            If IsNumeric(value) Then
                MyBase.Text = Format(CDbl(value), "c")
            Else
                MyBase.Text = value
            End If

        End Set
    End Property
End Class
Thanks DrPunk.

This code works however, when I use "Public Overrides Property" all my other codes in the custom textbox control stops working. I have an ENUM that contains options like "Letters_Only". When I use the code above all my ohter code stops working. No error message.

The option "Public Overrides Property" overrides everything I have done. Is there another way of implementing this good idea without override?

Thanks for the help. I am kind of new to writting custom controls.
Reply With Quote
  #7  
Old 06-18-2013, 08:35 AM
DrPunk's Avatar
DrPunkFormat data before loading into textbox? DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

I don't really follow. Could you post an example of some code that doesn't work properly now?

Your Enum comment makes me wonder if you are trying to make a single custom control do too much. You might be better off with a LettersOnlyTextBox instead of a textbox control that you can set to Letters_Only. It's not often that a control has to act differently at runtime, i.e. that Enum property would be set at design time and not change during runtime.
__________________
There are no computers in heaven!
Reply With Quote
  #8  
Old 06-19-2013, 10:36 AM
RM33 RM33 is offline
Newcomer
 
Join Date: Jan 2013
Posts: 20
Default

Quote:
Originally Posted by DrPunk View Post
I don't really follow. Could you post an example of some code that doesn't work properly now?

Your Enum comment makes me wonder if you are trying to make a single custom control do too much. You might be better off with a LettersOnlyTextBox instead of a textbox control that you can set to Letters_Only. It's not often that a control has to act differently at runtime, i.e. that Enum property would be set at design time and not change during runtime.
The goal of the company is to have 1 textbox class that can do it all for your application. That way the programmer just drags and drops the textbox class and picks something from the ENUM choices and they are done. No coding. And if we need to change something it is done centrally at the textbox class.

Like I said everything was working until I added "Public Overrides Property". I considered using Shadow instead of Overrides. But I don't know to much about Shadow and I am concerned of unintended consequences.

Anyway. Below is my code. I have formating when people leave the textbox and keypress.

Thanking you in advance. RM33

Public Class clsTextBox
Inherits TextBox

Public Sub New()
Me.Font = New Font("MS Sans Serif", 8, FontStyle.Bold, GraphicsUnit.Point, Nothing)
Me.BorderStyle = Windows.Forms.BorderStyle.None
Me.BackColor = Color.White
End Sub

Public Enum enum_Input_Settings
Normal = 0
AlphabetsOnly = 1
Currency = 2
DecimalNumeric = 4
AlphaNumeric = 5
End Enum

Public Enum enum_CaseFormat
None = 0
LowerCase = 1
UpperCase = 2
TitleCase = 3
End Enum

Private Sub clsTextBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Leave
If ((Me.CFS_MandatoryField = True) And (CType(sender, TextBox).Text = String.Empty)) Then
mIsValidated = False
Me.Focus()
Me.BackColor = Me.mMandatoryColor

If (mShowMsg = True) Then Call PopupBalloon("This textbox can't be empty..!!", ToolTipIcon.Error)
'RaiseEvent OnVaidationError("Can not be empty..!!")

Else
Me.BackColor = Me.mLeaveFocusColor
mIsValidated = True
End If

If ((IsNumeric(CType(sender, TextBox).Text) = True) And (mInputType = enum_Input_Settings.DecimalNumeric)) Then
CType(sender, TextBox).Text = Microsoft.VisualBasic.FormatNumber(CType(sender, TextBox).Text, mPrecision, , , TriState.False)

End If

If IsNumeric(CType(sender, TextBox).Text) = True And mInputType = enum_Input_Settings.Currency Then
CType(sender, TextBox).Text = Format(CDbl(CType(sender, TextBox).Text), "c")
End If
End Sub


Private Sub OnTextBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Me.KeyPress
Select Case mInputType
Case enum_Input_Settings.DecimalNumeric
If ((Char.IsDigit(e.KeyChar) = False) And (Char.IsControl(e.KeyChar) <> True) And (e.KeyChar <> "."c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("."c) = True) And (e.KeyChar = "."c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text = String.Empty) And (e.KeyChar = "."c)) Then
e.Handled = True
End If
Case enum_Input_Settings.AlphabetsOnly
If ((Char.IsLetter(e.KeyChar) = False) And ((e.KeyChar <> Chr(32))) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
Case enum_Input_Settings.AlphaNumeric
If ((Char.IsLetterOrDigit(e.KeyChar) = False) And ((e.KeyChar <> Chr(32))) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
Case enum_Input_Settings.Currency
If ((Char.IsDigit(e.KeyChar) = False) And (e.KeyChar <> ","c) And (e.KeyChar <> "."c) And (e.KeyChar <> "+"c) And (e.KeyChar <> "-"c) And (Char.IsControl(e.KeyChar) <> True)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("."c) = True) And (e.KeyChar = "."c)) Then
e.Handled = True
End If
If Len(CType(sender, TextBox).Text) = 0 And (e.KeyChar = "$"c) Then
e.Handled = False
End If
If ((CType(sender, TextBox).Text.Contains("-"c) = True) And (e.KeyChar = "-"c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text.Contains("+"c) = True) And (e.KeyChar = "+"c)) Then
e.Handled = True
End If
'If ((CType(sender, TextBox).Text = String.Empty) And (e.KeyChar = "."c)) Then
' e.Handled = True
'End If
'If ((e.KeyChar = "."c) And (CType(sender, TextBox).Text.Length = 1) And ((CType(sender, TextBox).Text = "+") Or (CType(sender, TextBox).Text = "-"))) Then
' e.Handled = True
'End If
If ((CType(sender, TextBox).Text <> String.Empty) And (e.KeyChar = "+"c)) Then
e.Handled = True
End If
If ((CType(sender, TextBox).Text <> String.Empty) And (e.KeyChar = "-"c)) Then
If CType(sender, TextBox).SelectionLength = CType(sender, TextBox).Text.Length Then
e.Handled = False
Else
e.Handled = True
End If
End If
Case Else
'DO NOTHING
End Select
End Sub
Reply With Quote
  #9  
Old 06-19-2013, 10:42 AM
DrPunk's Avatar
DrPunkFormat data before loading into textbox? DrPunk is offline
Senior Contributor

* Expert *
 
Join Date: Apr 2003
Location: Never where I want to be
Posts: 1,403
Default

Quote:
The goal of the company is to have 1 textbox class that can do it all for your application. That way the programmer just drags and drops the textbox class and picks something from the ENUM choices and they are done. No coding. And if we need to change something it is done centrally at the textbox class.
Choosing one control and setting the correct Enums, to me, is harder than choosing the correct control from the Toolbox (no coding required either).

Every single control you add has to be set. And set correctly. That becomes tedious quick. Copy and pasting controls would become their friend. Whereas separate controls just need to be added and they work.

Some controls would share common themes and therefore they'd inherit from other controls so it's not like the code becomes duplicated and difficult to maintain. In fact maintaining one class that does EVERYTHING is difficult. You're finding that out. It'll only get worse as time goes on.

I'm a bit short on time at the moment. I'll try and have a look at your code tomorrow. But in the meantime have a think about what I've typed there.
__________________
There are no computers in heaven!
Reply With Quote
Reply

Tags
currency, format


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox? Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox? Format data before loading into textbox?
Format data before loading into textbox?
Format data before loading into textbox?
 
Format data before loading into textbox?
Format data before loading into textbox?
 
-->