Prev: 3 Missing
Next: Cumulative Update - May 2009
From: Rick Rothstein on 28 Oct 2009 11:01 'Nobody' already gave you the solution... just check for KeyAscii ('Nobody' misstated this was KeyCode) value to see if it is for a number and "eat" the keystroke (set KeyAscii=0) in the KeyPress event if it isn't. -- Rick (MVP - Excel) "catharinus" <csvanderwerf(a)planet.nl> wrote in message news:7c213101-92f8-44ef-9272-e1641d1a4f9f(a)z41g2000yqz.googlegroups.com... > On 28 okt, 13:08, "Ralph" <nt_consultin...(a)yahoo.com> wrote: >> "catharinus" <csvanderw...(a)planet.nl> wrote in message >> >> news:e3d91828-3ecc-4b1c-a394-15c139cb8eab(a)k17g2000yqh.googlegroups.com...> >> On 28 okt, 11:07, "Nobody" <nob...(a)nobody.com> wrote: >> > > "catharinus" <csvanderw...(a)planet.nl> wrote in message >> >> news:a5e2a7a4-1ed0-43d1-b43f-2e0695eb7f5b(a)a32g2000yqm.googlegroups.com... >> >> >> >> > > > Hello >> >> > > > Is it possible to set the style of a column in MHFLexGrid? >> > > > I want to be able to set the style to a number or to a string >> > > > When the style of the col is a number, the user cannot insert a >> > > > string, for example the letter A. >> > > > In listview I can use the tag for that, but how should I do that in >> > > > MSHFlexgrid? >> >> > > Perhaps by setting KeyCode in KeyPress event to 0 for unwanted >> characters? >> >> > mh, thanks, but a bit creepy I think >> >> LOL >> >> What a great seasonal choice of a word. But then what is programming, if >> not >> the finding and implementing of 'creepy' ways to do things? >> >> -ralph > > but do you have a solution to this problem?
From: Ralph on 28 Oct 2009 13:44 "catharinus" <csvanderwerf(a)planet.nl> wrote in message news:7c213101-92f8-44ef-9272-e1641d1a4f9f(a)z41g2000yqz.googlegroups.com... > > but do you have a solution to this problem? Stolen from someone around here. Modify to fit .... Private Sub MSFlexGrid_KeyPress(KeyAscii As Integer) With MSFlexGrid Select Case KeyAscii Case 8: ' BACKSPACE If .Text <> "" Then .Text = _ Left$(.Text, (Len(.Text) - 1)) Case 13: ' ENTER Select Case .Col Case Is < (.Cols - 1): SendKeys "{right}" Case (.Cols - 1): If (.Row + 1) = .Rows Then Rows = .Rows + 1 SendKeys "{home}" + "{down}" End Select Case Else .Text = .Text + Chr$(KeyAscii) 'write your own keyascii Validations under 'commented lines Select Case .Col Case 0, 1, 2: 'if (your condition(s)) then accept only charectors 'Else ' keyascii=0 'End If Case Else: End Select End Select End With End Sub I usually don't use SendKeys in production, but it is pretty handy for ad hoc programs. -ralph
From: Jeff Johnson on 29 Oct 2009 10:33 "Rick Rothstein" <rick.newsNO.SPAM(a)NO.SPAMverizon.net> wrote in message news:uT2qT%239VKHA.4704(a)TK2MSFTNGP02.phx.gbl... > 'Nobody' already gave you the solution... just check for KeyAscii > ('Nobody' misstated this was KeyCode) value to see if it is for a number > and "eat" the keystroke (set KeyAscii=0) in the KeyPress event if it > isn't. The solution isn't so much creepy as crappy. What about paste, for example...? Of course, it also depends on how this "in-cell editing" is being performed in the first place.
From: Rick Rothstein on 29 Oct 2009 11:52 >> 'Nobody' already gave you the solution... just check for KeyAscii >> ('Nobody' misstated this was KeyCode) value to see if it is for a number >> and "eat" the keystroke (set KeyAscii=0) in the KeyPress event if it >> isn't. > > The solution isn't so much creepy as crappy. What about paste, for > example...? Of course, it also depends on how this "in-cell editing" is > being performed in the first place. Actually, I think I have been away too long from VB... I had forgotten that the FlexGrid control does not allow for direct cell entry. The full control mechanism can be used on a TextBox assumed to be used for taking in the value to be inserted into the grid. Below is an old posting of mine showing how to restrict entry into a TextBox to either digits only or to a floating point number... Here are some solutions which I've posted in the past (there is code below for both, entries with digits only and for entries with decimal points). The routines work quite well and protects the TextBox from pasting non-numeric entries (the user can paste valid data though) as well as stopping non-numeric keypresses. (Be wary of KeyPress only type solutions which may be posted later on as they can't protect against bad data being Paste'd in.) For typing digits only in the TextBox ===================================== Dim LastPosition As Long Private Sub Text1_Change() Static LastText As String Static SecondTime As Boolean If Not SecondTime Then With Text1 If .Text Like "*[!0-9]*" Then Beep SecondTime = True .Text = LastText .SelStart = LastPosition Else LastText = .Text End If End With End If SecondTime = False End Sub Private Sub Text1_MouseDown(Button As Integer, _ Shift As Integer, X As Single, Y As Single) With Text1 LastPosition = .SelStart 'Place any other MouseDown event code here End With End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) With Text1 LastPosition = .SelStart 'Place any other KeyPress checking code here End With End Sub For typing floating point numbers in the TextBox ========================================= ' Set the maximum number of digits before the ' decimal point in the MaxWhole constant. Set ' the maximum number of digits after the decimal ' point in the MaxDecimal constant. Dim LastPosition As Long Private Sub Text1_Change() Static LastText As String Static SecondTime As Boolean Const MaxDecimal As Integer = 4 Const MaxWhole As Integer = 2 With Text1 If Not SecondTime Then If .Text Like "*[!0-9.]*" Or _ .Text Like "*.*.*" Or _ .Text Like "*." & String$(1 + MaxDecimal, "#") Or _ .Text Like String$(MaxWhole, "#") & "[!.]*" Then Beep SecondTime = True .Text = LastText .SelStart = LastPosition Else LastText = .Text End If End If End With SecondTime = False End Sub Private Sub Text1_MouseDown(Button As Integer, _ Shift As Integer, X As Single, Y As Single) With Text1 LastPosition = .SelStart 'Place any other MouseDown event code here End With End Sub Private Sub Text1_KeyPress(KeyAscii As Integer) With Text1 LastPosition = .SelStart 'Place any other KeyPress checking code here End With End Sub Note that you will have check for the Text property containing a single character consisting of a decimal point since that must be allowed as a starting character. If you want to allow negative, as well as positive values, then use this If statement in place of the second If statement in the Text1_Change event code above: If .Text Like "*[!0-9.+-]*" Or _ .Text Like "*.*.*" Or _ .Text Like "*." & String$(1 + MaxDecimal, "#") Or _ .Text Like "*" & String$(MaxWhole, "#") & "[!.]*" Or _ .Text Like "?*[+-]*" Then Note that now you will have to check the Text property for this one to see if it contains a single plus, minus or decimal point. I guess I should mention that I'm in the US where the decimal point is a "dot". If your decimal point is some other characters, then make the obvious substitutions in the If-Then tests above; or you could query the system for the decimal point character, store it in a variable and concatenate that into the string values above in place of the decimal point ("dot") that I show above. In keeping with the non-APIness of this solution, here is what I use to get the system's decimal point. DecimalPointSymbol = Format$(0, ".") -- Rick (MVP - Excel)
From: catharinus on 5 Nov 2009 07:33
On 29 okt, 16:52, "Rick Rothstein" <rick.newsNO.S...(a)NO.SPAMverizon.net> wrote: > >> 'Nobody' already gave you the solution... just check for KeyAscii > >> ('Nobody' misstated this was KeyCode) value to see if it is for a number > >> and "eat" the keystroke (set KeyAscii=0) in the KeyPress event if it > >> isn't. > > > The solution isn't so much creepy as crappy. What about paste, for > > example...? Of course, it also depends on how this "in-cell editing" is > > being performed in the first place. > > Actually, I think I have been away too long from VB... I had forgotten that > the FlexGrid control does not allow for direct cell entry. The full control > mechanism can be used on a TextBox assumed to be used for taking in the > value to be inserted into the grid. Below is an old posting of mine showing > how to restrict entry into a TextBox to either digits only or to a floating > point number... > > Here are some solutions which I've posted in the past (there is code below > for both, entries with digits only and for entries with decimal points). The > routines work quite well and protects the TextBox from pasting non-numeric > entries (the user can paste valid data though) as well as stopping > non-numeric keypresses. (Be wary of KeyPress only type solutions which may > be posted later on as they can't protect against bad data being Paste'd in.) > > For typing digits only in the TextBox > ===================================== > Dim LastPosition As Long > > Private Sub Text1_Change() > Static LastText As String > Static SecondTime As Boolean > If Not SecondTime Then > With Text1 > If .Text Like "*[!0-9]*" Then > Beep > SecondTime = True > .Text = LastText > .SelStart = LastPosition > Else > LastText = .Text > End If > End With > End If > SecondTime = False > End Sub > > Private Sub Text1_MouseDown(Button As Integer, _ > Shift As Integer, X As Single, Y As Single) > With Text1 > LastPosition = .SelStart > 'Place any other MouseDown event code here > End With > End Sub > > Private Sub Text1_KeyPress(KeyAscii As Integer) > With Text1 > LastPosition = .SelStart > 'Place any other KeyPress checking code here > End With > End Sub > > For typing floating point numbers in the TextBox > ========================================= > ' Set the maximum number of digits before the > ' decimal point in the MaxWhole constant. Set > ' the maximum number of digits after the decimal > ' point in the MaxDecimal constant. > Dim LastPosition As Long > > Private Sub Text1_Change() > Static LastText As String > Static SecondTime As Boolean > Const MaxDecimal As Integer = 4 > Const MaxWhole As Integer = 2 > With Text1 > If Not SecondTime Then > If .Text Like "*[!0-9.]*" Or _ > .Text Like "*.*.*" Or _ > .Text Like "*." & String$(1 + MaxDecimal, "#") Or _ > .Text Like String$(MaxWhole, "#") & "[!.]*" Then > Beep > SecondTime = True > .Text = LastText > .SelStart = LastPosition > Else > LastText = .Text > End If > End If > End With > SecondTime = False > End Sub > > Private Sub Text1_MouseDown(Button As Integer, _ > Shift As Integer, X As Single, Y As Single) > With Text1 > LastPosition = .SelStart > 'Place any other MouseDown event code here > End With > End Sub > > Private Sub Text1_KeyPress(KeyAscii As Integer) > With Text1 > LastPosition = .SelStart > 'Place any other KeyPress checking code here > End With > End Sub > > Note that you will have check for the Text property containing a single > character consisting of a decimal point since that must be allowed as a > starting character. If you want to allow negative, as well as positive > values, then use this If statement in place of the second If statement in > the Text1_Change event code above: > > If .Text Like "*[!0-9.+-]*" Or _ > .Text Like "*.*.*" Or _ > .Text Like "*." & String$(1 + MaxDecimal, "#") Or _ > .Text Like "*" & String$(MaxWhole, "#") & "[!.]*" Or _ > .Text Like "?*[+-]*" Then > > Note that now you will have to check the Text property for this one to see > if it contains a single plus, minus or decimal point. > > I guess I should mention that I'm in the US where the decimal point is a > "dot". If your decimal point is some other characters, then make the obvious > substitutions in the If-Then tests above; or you could query the system for > the decimal point character, store it in a variable and concatenate that > into the string values above in place of the decimal point ("dot") that I > show above. In keeping with the non-APIness of this solution, here is what I > use to get the system's decimal point. > > DecimalPointSymbol = Format$(0, ".") > > -- > Rick (MVP - Excel) Oke Guys, thanks Rick gave a solution of how to work with a Column that is defined as a number or string. But that's not what I want. I want to define a column as a number or string. In listview you can do that very easily: " Me.ListView1.ColumnHeaders.Add(, , "Nr", 700).Tag = "Number" Me.ListView1.ColumnHeaders.Add(, , "Word", 1700, 0).Tag = "String" And after defining a column in this way, one can create a routine on how to work with these columns, but that was not my question. Why is it so easy in Listview and not in MSHFlexgrid?? Catharinus van der Werf csvanderwerf(a)planet.nl |