2 textbox questions

  • Thread starter Thread starter monika
  • Start date Start date
M

monika

hi...

I have a form where I have a txt box where i want to accept from user only
percentages. How can I make a check in this case?

I wanted user to know that he will enter percentages in it. So what i
thought was that when user enters the number in the textbox and when he
moves away from the textbox a "%" sign should appear. how can i do that?

secondly i wanted to make my textbox deactivated and grayed. That is when he
is selecting an option A then the textbox in activated and and when he sects
option B the textbox is visible but grayed. i don't to hide it.

thanks
Monika
 
Hi Monika

If you by "Form" mean userform, see if this get you started:

Private Sub TextBox1_Enter()
With TextBox1
..BackColor = vbWhite
..TextAlign = fmTextAlignLeft
..SelStart = 0
..SelLength = Len(.Text)
End With
End Sub

Private Sub TextBox1_KeyPress(ByVal _
KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
Case Else
KeyAscii = 0
End Select
End Sub

Private Sub TextBox1_Exit(ByVal _
Cancel As MSForms.ReturnBoolean)
With TextBox1
.BackColor = RGB(195, 195, 195)
.TextAlign = fmTextAlignRight
.Text = Val(.Text) & " %"
End With
End Sub

What this doesn't do as written is handling paste by Ctrl V or allow decimal entries. Post
back if that is of interest.

"Deactivated" is for most controls set by Enabled:

TextBox1.Enabled = False
TextBox1.Enabled = True
 
Back
Top