Looping for user input in a textbox

  • Thread starter Thread starter Robert
  • Start date Start date
R

Robert

Thanks to anyone who has time to reply with some good advice or
indications. I posted this message to a visual basic group and was
kindly redirected here by an MVP, explaining that visual basic.net is
quite different to visual basic previous versions. Before anyone feels
tempted to flame or to scorn, I have RTFM, in fact various FM's and
nothing seems to help here. I have little experience of visual
basic.net at all, but I've been experimenting with this problem for
some time without making much headway.

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
 
Robert,

I could not quite understand why you compare the
whole .Text property to one character - should not you
analyze all the characters in the TextBox1?
You could either iterate through TextBox1.Text.Chars array
or use SubString method to get to individual character as
String.

Hope this helps...
 
Hi Robert,
This kind of things are always funny to do

I did make this, I don't know if I did make the sequence well.
I thought it was that a character before a higher character was a minus
character and that there only could be 1 minus character before a plus
character, but from that I am not sure of. So I did not test that and also
that is the only thing which is used.

But you have a lot of examples now.

It needs 1 button and 1 textbox on a form
\\\
Private Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim TestString As String = "IVXLCDM"
Dim i As Integer
TextBox1.Text = TextBox1.Text.ToUpper
For i = 0 To TextBox1.Text.Length - 1
If TestString.IndexOf(TextBox1.Text.Substring(i, 1)) = -1 Then
TextBox1.Text = TextBox1.Text & " Contains a not Roman value"
Exit Sub
End If
Next
Dim roman As New System.Text.StringBuilder(TextBox1.Text.ToUpper)
roman = roman.Replace("I", "1,")
roman = roman.Replace("V", "5,")
roman = roman.Replace("X", "10,")
roman = roman.Replace("L", "50,")
roman = roman.Replace("C", "100,")
roman = roman.Replace("D", "500,")
roman = roman.Replace("M", "1000,")
Dim romanstr As String = roman.ToString.Substring(0, roman.Length - 1)
Dim romanarr As String() = Split(romanstr, ",")
For i = 0 To romanarr.Length - 2
If CInt(romanarr(i)) < CInt(romanarr(i + 1)) Then
romanarr(i) = "-" & romanarr(i)
End If
Next
Dim result As Integer
For i = 0 To romanarr.Length - 1
result += CInt(romanarr(i))
Next
TextBox1.Text = TextBox1.Text & "=" & result.ToString
End Sub
///

I hope this helps a little bit?

Cor
 
Hi Robert,

I'm a bit puzzled why the MVP would send you scooting - this isn't a
language problem - it's a programming problem.

I'm also puzzled where 'flame'and 'scorn' come into it. Nor do I know what
RTFM or FM mean!

But who cares about that. ;-)

My advice is simple. Take the calculation out of the UI and put it
somewhere like a routine in Module RomanNumerals. A Roman numeral is
calculated as a unit - you can't do it piecemeal because the order of the
digits is important - "IX" versus "XI", for instance.

Thus, you would send the <entire> string to the converter (calculator is
too strong a word - keep it in reserve until you start proper adding - "XIV"
to "MCX")

Remember that low digits before a high digit detract while those following
cause addition.

Lol. I've just remembered what an FM is. Is there one for converting Roman
Numerals? Yes, and code too - you can do a Google for
Roman Numerals "VB.NET"
or
Roman Numerals VB 'This will get more.

But you may prefer the challenge of doing it yourself first. And you can
always bring it back here, too. ;-)

Regards,
Fergus
 
Hi Fergus,
Good morning

So check that chalenge I took.

Make a nice Crom(RomanString) from it

What will Conan think about that?

I need to go on for my rectangle first.

:-))

Cor
 
Morning Cor,

ROFL. Maybe I should go to bed - this one has me beaten!

Crom? Conan?? Rectangle ???

But I saw the challenge. ;-)

[Pause ... head scratch ... ponder ...]

I think I might know the Rectangle - was it red and moveable/zoomable by
any chance?

Regards,
Fergus
 
Hi Fergus,
CRom Convert Roman to integer (a nice function)
Conan Crom is the sword of Conan
Rectangle You are right I am busy to put it in a class but piece by
piece, I still don't know for myself if it will be a class or a custom
control.

Cor
 
Hi Cor,

|| Make a nice Crom(RomanString) from it
|| What will Conan think about that?

Lol. I think he'll ask what the strange marks on his sword are. ;-)

|| class or a custom control.

Now that's a good question. Perhaps 'twill be both with the UC using the
Class?

Regards,
Fergus
 
Hi Herfried,
I made this, but I don't know anymore how it was exactly with Roman figurs.
Is it so that XXC can exist or has it to be LXXX. ?
Cor
 
Hi Cor,

LXXX

According to Cor,
|| there only could be 1 minus character before a plus character
and I think he's right.;-)

Regards,
Fergus

'there can only be 1 minus character before a plus character
 
Sergey Poberezovskiy said:
Robert,

I could not quite understand why you compare the
whole .Text property to one character - should not you
analyze all the characters in the TextBox1?
You could either iterate through TextBox1.Text.Chars array
or use SubString method to get to individual character as
String.

Hope this helps...
It does in a way ,thanks and thank you to the others who wrote in with
chunks of code.

I very much want to analyze all the characters entered in textbox1.
I'm doing this exercise to learn vb.net and I read the codes posted
with great interest.

What I'm trying to control is slightly different and it's this:

Immediately as a user enters a character in textbox1 then this
character is converted and displayed in textbox2 without having to
press an ‘OK' or a ‘convert' button in sort of ‘real-time' - updating
on the fly. (If I used a cmd button I could send the complete string
to a module for validation and processing.)

So,

M produces 1000
MM produces 2000
C produces 100
I produces 1
II produces 2

but VV would produce an immediate error message saying the data
entered in textbox1 was incorrect and the display in textbox1 and
textbox2 WOULD REMAIN UNCHANGED. (caps for emphasis not for shouting)

In short, I would like the NEW string the user is attempting to type
in textbox1, in this case ‘VV', to be available for validation and
processing.

In pseudocode

Get a new user roman letter
Validate the resulting string
Convert
Display

And all without the enter button!

Thanks

Bob
 
Hi Robert,

Then it's the TextChanged event that you want to be handling.

Regards,
Fergus
 
Robert said:
It does in a way ,thanks and thank you to the others who wrote in with
chunks of code.

I very much want to analyze all the characters entered in textbox1.
I'm doing this exercise to learn vb.net and I read the codes posted
with great interest.

What I'm trying to control is slightly different and it's this:

Immediately as a user enters a character in textbox1 then this
character is converted and displayed in textbox2 without having to
press an 'OK' or a 'convert' button in sort of 'real-time' - updating
on the fly. (If I used a cmd button I could send the complete string
to a module for validation and processing.)

So,

M produces 1000
MM produces 2000
C produces 100
I produces 1
II produces 2

but VV would produce an immediate error message saying the data
entered in textbox1 was incorrect and the display in textbox1 and
textbox2 WOULD REMAIN UNCHANGED. (caps for emphasis not for shouting)

In short, I would like the NEW string the user is attempting to type
in textbox1, in this case 'VV', to be available for validation and
processing.

In pseudocode

Get a new user roman letter
Validate the resulting string
Convert
Display

And all without the enter button!

Thanks

Bob

I'm not sure I understand completely what you are trying to do but...
Couldn't you handle the text_changed event of textbox1 and send the entire
string everytime it fires for validation.
This would eliminate having a cmd button.
 
Hi Roberts

Forget that last post. - incomplete.

Then it <is> the TextChanged event that you want to be handling because
all sorts of things can cause a change which you don't want to have to handle
yourself - eg cut and paste.

This means that you'll be accepting changed text and validating it. Then,
if it's invalid, you have to change it back again. This will cause your
handler to be called again, but as you will be replacing a bad RomanNum with
the previous good 'un, it will validate and all's fine .

In order to set it back to it's previous good value, you need to <have>
that value. So you'll have a separate variable.

Incomplete code.

Private sPrevGoodUn As String
Sub TextChanged (..)
Value = EvaluateRomanNum (sender.Text) '-1 = Err
If Value > -1 Then
TextBox2.Text = Value
sPrevGoodUn = sender.Text
Else
sender.Text = sPrevGoodUn

Kind of thing.

Regards,
Fergus
 
Ok Robert...

You've got the weekend to figure this out as I have the answer... :-) I can
give it to you in chunks or I can post it all at one time. Personally I
think chunk mode might be fun but you might not. The trouble with posting
long listings (particularly if you are learning VB.Net) is that it can look
like "just a lot of code" and sometimes what it is doing and why it is doing
it isn't so easy to decipher.

Let me know,
Tom
 
Hi Tom, Robert

Cheers
Cheeks
Checks
Chucks
+Chunks+
Thunks
Thanks. ;-)

I'm going to second guess you Robert and reckon you'll say chunks because you've
already said this is a VB.NET learning experience.

I'd like to have the lot though, if you wouldn't mind, Tom: (e-mail address removed)

Regards,
Fergus
 
Back
Top