469,266 Members | 2,028 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,266 developers. It's quick & easy.

TextBox input

As a pre-newbie to visual basic, I haven't programmed for years and I
am having the greatest of difficulties coping with it. If any kind
soul can help me out with advice then I'd be most grateful.

I am at the stage where I can design a form where I can insert text
boxes. You type in a word in textbox1, press OK and it appears in
textbox2. Not rocket science I know. But I trust this gives you a
feel for my level.

What I want to do is this:

As you type a character into textbox1 then this character is instantly
displayed in textbox2 and, this for me is the tricky bit, YOU DON'T
NEED TO PRESS RETURN or OK or any button at all. Please excuse the
shouting.

(What I will be designing eventually will be a translation program
which converts Roman numerals into Arabic numerals. Each letter as
entered in textbox1 will be translated into Arabic numerals, after
validation, and displayed in textbox2. I've written the validation
algorithm, have desk-checked it and it works.)

Before anyone feels tempted to flame or to scorn, I have RTFM, in fact
various FM's and nothing seems to help here. InputBoxes won't do, of
course.

In ye old BASIC it might go something like this

INPUT "Type in a letter"; letter$
GOSUB loads of validation and processing
PRINT letter$

This probably looks like Elvish to anyone born after 1980.

Thanks if you can help.

Bob
Jul 17 '05 #1
12 12995
> As a pre-newbie to visual basic, I haven't programmed for years and I
am having the greatest of difficulties coping with it. If any kind
soul can help me out with advice then I'd be most grateful.

I am at the stage where I can design a form where I can insert text
boxes. You type in a word in textbox1, press OK and it appears in
textbox2. Not rocket science I know. But I trust this gives you a
feel for my level.

What I want to do is this:

As you type a character into textbox1 then this character is instantly
displayed in textbox2 and, this for me is the tricky bit, YOU DON'T
NEED TO PRESS RETURN or OK or any button at all. Please excuse the
shouting.

(What I will be designing eventually will be a translation program
which converts Roman numerals into Arabic numerals. Each letter as
entered in textbox1 will be translated into Arabic numerals, after
validation, and displayed in textbox2. I've written the validation
algorithm, have desk-checked it and it works.)

Before anyone feels tempted to flame or to scorn, I have RTFM, in fact
various FM's and nothing seems to help here. InputBoxes won't do, of
course.

In ye old BASIC it might go something like this

INPUT "Type in a letter"; letter$
GOSUB loads of validation and processing
PRINT letter$

This probably looks like Elvish to anyone born after 1980.


Forget the "old" BASIC... it will only hold back your thinking. For a
TextBox, the Change event is fired each time text in the TextBox changes. To
answer your stated question,

Private Sub Text1_Change()
Text2.Text = Text1.Text
End Sub

will update the text in Text2 whenever something is type (or deleted) in
Text1. To do the conversions you indicated you wanted to do, you would put
that conversion code in the Change event of Text1.

Rick - MVP
Jul 17 '05 #2
In addition to what Rick said, this will help with the roman-to-Arabic
number conversion ..
http://www.mvps.org/vbnet/code/helpe...bertoroman.htm

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.
"Robert" <sh***************@yahoo.co.uk> wrote in message
news:95**************************@posting.google.c om...
: As a pre-newbie to visual basic, I haven't programmed for years and I
: am having the greatest of difficulties coping with it. If any kind
: soul can help me out with advice then I'd be most grateful.
:
: I am at the stage where I can design a form where I can insert text
: boxes. You type in a word in textbox1, press OK and it appears in
: textbox2. Not rocket science I know. But I trust this gives you a
: feel for my level.
:
: What I want to do is this:
:
: As you type a character into textbox1 then this character is instantly
: displayed in textbox2 and, this for me is the tricky bit, YOU DON'T
: NEED TO PRESS RETURN or OK or any button at all. Please excuse the
: shouting.
:
: (What I will be designing eventually will be a translation program
: which converts Roman numerals into Arabic numerals. Each letter as
: entered in textbox1 will be translated into Arabic numerals, after
: validation, and displayed in textbox2. I've written the validation
: algorithm, have desk-checked it and it works.)
:
: Before anyone feels tempted to flame or to scorn, I have RTFM, in fact
: various FM's and nothing seems to help here. InputBoxes won't do, of
: course.
:
: In ye old BASIC it might go something like this
:
: INPUT "Type in a letter"; letter$
: GOSUB loads of validation and processing
: PRINT letter$
:
: This probably looks like Elvish to anyone born after 1980.
:
: Thanks if you can help.
:
: Bob
Jul 17 '05 #3
Thanks to you both for taking the time and trouble to write. That
seemed to do the trick.

Bob
Jul 17 '05 #4
Thanks to you both for taking the time and trouble to write. That
seemed to do the trick.
Bob
Jul 17 '05 #5
The coding

Private Sub Text1_Change()
Text2.Text = Text1.Text
End Sub

worked a treat. Thanks again.

Sorry to be so thick but when you say "To do the conversions you
indicated you wanted to do, you would put that conversion code in the
Change event of Text1." does that mean like this?

Private Sub Text1_Change(conversion/validation code here?)
Text2.Text = Text1.Text
End Sub

I've done quite a bit of experimenting and can't seem to get that to
work. I suspect I'm doing something fundamentally wrong which won't be
for the first time. As I have things at the moment an M produces 1000
in textbox1 AND textbox2 (good), a repeated M produces 2000 (good)
but any other letter does too, even a backspace, despite all my case
statements.

Thanks for any helpful indications.

Bob

"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<P5********************@comcast.com>...
As a pre-newbie to visual basic, I haven't programmed for years and I
am having the greatest of difficulties coping with it. If any kind
soul can help me out with advice then I'd be most grateful.

I am at the stage where I can design a form where I can insert text
boxes. You type in a word in textbox1, press OK and it appears in
textbox2. Not rocket science I know. But I trust this gives you a
feel for my level.

What I want to do is this:

As you type a character into textbox1 then this character is instantly
displayed in textbox2 and, this for me is the tricky bit, YOU DON'T
NEED TO PRESS RETURN or OK or any button at all. Please excuse the
shouting.

(What I will be designing eventually will be a translation program
which converts Roman numerals into Arabic numerals. Each letter as
entered in textbox1 will be translated into Arabic numerals, after
validation, and displayed in textbox2. I've written the validation
algorithm, have desk-checked it and it works.)

Before anyone feels tempted to flame or to scorn, I have RTFM, in fact
various FM's and nothing seems to help here. InputBoxes won't do, of
course.

In ye old BASIC it might go something like this

INPUT "Type in a letter"; letter$
GOSUB loads of validation and processing
PRINT letter$

This probably looks like Elvish to anyone born after 1980.


Forget the "old" BASIC... it will only hold back your thinking. For a
TextBox, the Change event is fired each time text in the TextBox changes. To
answer your stated question,

Private Sub Text1_Change()
Text2.Text = Text1.Text
End Sub

will update the text in Text2 whenever something is type (or deleted) in
Text1. To do the conversions you indicated you wanted to do, you would put
that conversion code in the Change event of Text1.

Rick - MVP

Jul 17 '05 #6
> The coding

Private Sub Text1_Change()
Text2.Text = Text1.Text
End Sub

worked a treat. Thanks again.

Sorry to be so thick but when you say "To do the conversions you
indicated you wanted to do, you would put that conversion code in the
Change event of Text1." does that mean like this?

Private Sub Text1_Change(conversion/validation code here?)
Text2.Text = Text1.Text
End Sub


No, like this...

Private Sub Text1_Change()
' Do something with Text1.Text
' Assign it to Text2.Text
End Sub

For example, let's say you wanted to make anything typed in Text1 be
repeated in Text2, but in all caps. You could use the built-in UCase$
function like this

Private Sub Text1_Change()
Text2.Text = UCase$(Text1.Text)
End Sub

Use what ever conversion code you have (it could be a function like above,
or it could take place in-line over several lines) in place of the function
above.

Rick - MVP
Jul 17 '05 #7
A simple Roman numeral calculator.

More help appreciated if anyone has a few moments. Thanks.

Below is the code for textbox1 (without all the processing in each
case statement, of course, to make things clearer and omitting all the
declarations to keep it shorter).

At the moment ‘m' or another Roman letter typed in textbox1 produces
1000 in textbox2 or the corresponding value of that Roman letter.

‘mm' typed in textbox1 produces 0 in textbox2 but with a ‘backspace'
to ‘m' you get 2000 in textbox2. The logic of this is clear,
naturally.

However I would like ‘mm' in textbox1 to produce 2000, ‘mmc' to
produce 2100.

I thought the answer might lie in something like textbox1_keypress
with which I have been experimenting but cannot get this to work at
all.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TextBox1.TextChanged

ROMAN = TextBox1.Text
ROMAN = UCase(ROMAN)

Select Case ROMAN

Case Is = "M"
NEWNUMBER = 1000

Case Is = "D"
NEWNUMBER = 500

Case Is = "C"
NEWNUMBER = 100

Case Is = "L"
NEWNUMBER = 50

Case Is = "X"
NEWNUMBER = 10

Case Is = "V"
NEWNUMBER = 5

Case Is = "I"
NEWNUMBER = 1

Case Else
NEWNUMBER = 0

End Select

SUM = SUM + NEWNUMBER
TextBox2.Text = SUM
End Sub

Thanks for any help or indications.

Bob
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<Bv********************@comcast.com>...
The coding

Private Sub Text1_Change()
Text2.Text = Text1.Text
End Sub

worked a treat. Thanks again.

Sorry to be so thick but when you say "To do the conversions you
indicated you wanted to do, you would put that conversion code in the
Change event of Text1." does that mean like this?

Private Sub Text1_Change(conversion/validation code here?)
Text2.Text = Text1.Text
End Sub


No, like this...

Private Sub Text1_Change()
' Do something with Text1.Text
' Assign it to Text2.Text
End Sub

For example, let's say you wanted to make anything typed in Text1 be
repeated in Text2, but in all caps. You could use the built-in UCase$
function like this

Private Sub Text1_Change()
Text2.Text = UCase$(Text1.Text)
End Sub

Use what ever conversion code you have (it could be a function like above,
or it could take place in-line over several lines) in place of the function
above.

Rick - MVP

Jul 17 '05 #8
> Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TextBox1.TextChanged


OH!!!! You are using VB.NET. Almost any advice you get here will be the
incorrect way for you to proceed. Here is my standard reply to people who
indicate they are using VB.NET

Almost everybody in this newsgroup is using VB6 or lower. While you may get
a stray answer to VB.NET questions here, you should ask them in newsgroups
devoted exclusively to .NET programming. Look for newsgroups with either the
word "dotnet" or "vsnet" in their name.

For the microsoft news server, try these newsgroups...

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb
microsoft.public.vsnet.general

For the news.devx.com news server, try these

vb.dotnet.discussion
vb.dotnet.technical

There are some others, but these should get you started.

Rick - MVP
Jul 17 '05 #9
On 2 Nov 2003 03:05:58 -0800, sh***************@yahoo.co.uk (Robert)
wrote:
A simple Roman numeral calculator.

More help appreciated if anyone has a few moments. Thanks.

Below is the code for textbox1 (without all the processing in each
case statement, of course, to make things clearer and omitting all the
declarations to keep it shorter).

At the moment ‘m' or another Roman letter typed in textbox1 produces
1000 in textbox2 or the corresponding value of that Roman letter.

‘mm' typed in textbox1 produces 0 in textbox2 but with a ‘backspace'
to ‘m' you get 2000 in textbox2. The logic of this is clear,
naturally.

However I would like ‘mm' in textbox1 to produce 2000, ‘mmc' to
produce 2100.

I thought the answer might lie in something like textbox1_keypress
with which I have been experimenting but cannot get this to work at
all.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TextBox1.TextChanged

ROMAN = TextBox1.Text
ROMAN = UCase(ROMAN)

Select Case ROMAN

Case Is = "M"
NEWNUMBER = 1000

Case Is = "D"
NEWNUMBER = 500

Case Is = "C"
NEWNUMBER = 100

Case Is = "L"
NEWNUMBER = 50

Case Is = "X"
NEWNUMBER = 10

Case Is = "V"
NEWNUMBER = 5

Case Is = "I"
NEWNUMBER = 1

Case Else
NEWNUMBER = 0

End Select

SUM = SUM + NEWNUMBER
TextBox2.Text = SUM
End Sub

Thanks for any help or indications.


Your algorithm needs some work. You're assuming textbox1.text is one
character in length. You need to loop through textbox1.text one
character at a time. Have a look at VB string functions.

Also there are positional rules in roman numerals where, for example:

VI = 5 + 1 = 6
IV = 5 - 1 = 4

Your algorithm needs to keep track of the preceding character and
determine if it is less than, greater than or equal to the current
character.

There are also some other rules to consider. I seem to recall that you
should never have a string of four identical characters (eg. IIII for
4 instead of IV). Although, I do recall that IIII is acceptable on
clocks, sundials or perhaps it's garden sundials - some timekeeping
application anyway.
Jul 17 '05 #10
Thanks for taking the time and trouble to reply, Ben.

'You need to loop through textbox1.text one character at a time. Have
a look at VB string functions.'

I suspect that looping around textbox1 in some way or using
textbox1.keypress is the way to go but no matter where I place the
loops, or what I do to keypress I cannot get this to work.

I take your point, of course, about the rules, positioning and the
like. I left all that code out so as not to clutter things. Thanks
again.

Bob
Ben Hall <be*******@yahoo.com.au> wrote in message news:<5p********************************@4ax.com>. ..
On 2 Nov 2003 03:05:58 -0800, sh***************@yahoo.co.uk (Robert)
wrote:
A simple Roman numeral calculator.

More help appreciated if anyone has a few moments. Thanks.

Below is the code for textbox1 (without all the processing in each
case statement, of course, to make things clearer and omitting all the
declarations to keep it shorter).

At the moment ?m' or another Roman letter typed in textbox1 produces
1000 in textbox2 or the corresponding value of that Roman letter.

?mm' typed in textbox1 produces 0 in textbox2 but with a ?backspace'
to ?m' you get 2000 in textbox2. The logic of this is clear,
naturally.

However I would like ?mm' in textbox1 to produce 2000, ?mmc' to
produce 2100.

I thought the answer might lie in something like textbox1_keypress
with which I have been experimenting but cannot get this to work at
all.

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TextBox1.TextChanged

ROMAN = TextBox1.Text
ROMAN = UCase(ROMAN)

Select Case ROMAN

Case Is = "M"
NEWNUMBER = 1000

Case Is = "D"
NEWNUMBER = 500

Case Is = "C"
NEWNUMBER = 100

Case Is = "L"
NEWNUMBER = 50

Case Is = "X"
NEWNUMBER = 10

Case Is = "V"
NEWNUMBER = 5

Case Is = "I"
NEWNUMBER = 1

Case Else
NEWNUMBER = 0

End Select

SUM = SUM + NEWNUMBER
TextBox2.Text = SUM
End Sub

Thanks for any help or indications.


Your algorithm needs some work. You're assuming textbox1.text is one
character in length. You need to loop through textbox1.text one
character at a time. Have a look at VB string functions.

Also there are positional rules in roman numerals where, for example:

VI = 5 + 1 = 6
IV = 5 - 1 = 4

Your algorithm needs to keep track of the preceding character and
determine if it is less than, greater than or equal to the current
character.

There are also some other rules to consider. I seem to recall that you
should never have a string of four identical characters (eg. IIII for
4 instead of IV). Although, I do recall that IIII is acceptable on
clocks, sundials or perhaps it's garden sundials - some timekeeping
application anyway.

Jul 17 '05 #11
Thanks Rick

I've reposted this question to a vb.net group.

Ora pro mi

Bob
"Rick Rothstein" <ri************@NOSPAMcomcast.net> wrote in message news:<hJ********************@comcast.com>...
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal
e As System.EventArgs) Handles TextBox1.TextChanged


OH!!!! You are using VB.NET. Almost any advice you get here will be the
incorrect way for you to proceed. Here is my standard reply to people who
indicate they are using VB.NET

Almost everybody in this newsgroup is using VB6 or lower. While you may get
a stray answer to VB.NET questions here, you should ask them in newsgroups
devoted exclusively to .NET programming. Look for newsgroups with either the
word "dotnet" or "vsnet" in their name.

For the microsoft news server, try these newsgroups...

microsoft.public.dotnet.general
microsoft.public.dotnet.languages.vb
microsoft.public.vsnet.general

For the news.devx.com news server, try these

vb.dotnet.discussion
vb.dotnet.technical

There are some others, but these should get you started.

Rick - MVP

Jul 17 '05 #12
I'm not sure whether this will help you or not, the code is in VB6, but here
is some code I've posted before for converting to and from Roman numbers.
Perhaps you can convert it to VB.NET.

Rick - MVP

Function FromRoman(ByVal RomanNumber As String) As Long
Dim DigitCount As Integer
Dim NumeralCount As Integer
Dim Answer() As Long
Dim Digits() As Byte
Dim RomanNumerals() As Byte
RomanNumerals = StrConv("IVXLCDM", vbFromUnicode)
Digits = StrConv(UCase$(RomanNumber), vbFromUnicode)
ReDim Answer(UBound(Digits))
For DigitCount = UBound(Digits) To 0 Step -1
For NumeralCount = 0 To UBound(RomanNumerals)
If Digits(DigitCount) = RomanNumerals(NumeralCount) Then
If NumeralCount Mod 2 Then
Answer(DigitCount) = 5 * 10 ^ ((NumeralCount - 1) / 2)
Else
Answer(DigitCount) = 10 ^ (NumeralCount / 2)
End If
Exit For
End If
Next
Next
For DigitCount = UBound(Answer) To 0 Step -1
If DigitCount < UBound(Answer) Then
If Answer(DigitCount) < Answer(DigitCount + 1) Then
Answer(DigitCount) = -Answer(DigitCount)
End If
End If
FromRoman = FromRoman + Answer(DigitCount)
Next
End Function

Function ToRoman(ByVal Number As Long) As String
Dim X As Integer
Dim DigitIndex As Integer
Dim MaxNumeralCount As Integer
Dim Digits() As Byte
Dim Numerals() As Byte
Numerals = StrConv("IVXLCDM", vbFromUnicode)
MaxNumeralCount = Number \ 10 ^ (UBound(Numerals) \ 2)
Number = Number Mod 10 ^ (UBound(Numerals) \ 2)
ToRoman = String$(MaxNumeralCount, _
Numerals(UBound(Numerals)))
Digits = StrConv(CStr(Number), vbFromUnicode)
For X = 0 To UBound(Digits)
DigitIndex = 2 * (UBound(Digits) - X)
If Digits(X) = vbKey9 Then
ToRoman = ToRoman & Chr$(Numerals(DigitIndex)) & _
Chr$(Numerals(DigitIndex + 2))
ElseIf Digits(X) >= vbKey5 Then
ToRoman = ToRoman & Chr$(Numerals(DigitIndex + 1)) & _
String$(Digits(X) - vbKey0 - 5, _
Chr$(Numerals(DigitIndex)))
ElseIf Digits(X) = vbKey4 Then
ToRoman = ToRoman & Chr$(Numerals(DigitIndex)) & _
Chr$(Numerals(DigitIndex + 1))
Else
ToRoman = ToRoman & String$(Digits(X) - vbKey0, _
Chr$(Numerals(DigitIndex)))
End If
Next
End Function
Jul 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Jerry | last post: by
7 posts views Thread by I am Sam | last post: by
3 posts views Thread by Justin Morris via DotNetMonster.com | last post: by
1 post views Thread by colleen1980 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.