Select Case statement is driving me mad! 
Author Message
 Select Case statement is driving me mad!

I have a KeyPress event in a Rich Text box that involves a Select Case
Statement. If I hit a space bar, it automatically highlights backwards 5
spaces, then, since it's now txtedit.seltext, depending on what the Seltext
actually is, the Select Case statement then does different things:
Select Case Ucase(txtedit.seltext:)
    Case "RNOLD"
        do this
    Case "HARRY"
        do this
    Case "DAVID"
        do something else
etc.

As you can see, the first one is actually supposed to be ARNOLD, but my
problem is that names, as you know are different lengths. I deal with longer
names this way...
What is harder is how to deal with shorter names, like JIM, or BOB, etc..

I've tried a Case Right("BOB", 3) or Case Right(txtEdit.SelText, 3)) = "BOB"
I realize I'm going in the wrong direction, but I'm stumped as to figuring
out how to do it ...
Any ideas?
--
David Wier



Fri, 26 Jul 2002 03:00:00 GMT  
 Select Case statement is driving me mad!

Quote:

>I have a KeyPress event in a Rich Text box that involves a Select Case
>Statement. If I hit a space bar, it automatically highlights backwards 5
>spaces, then, since it's now txtedit.seltext, depending on what the Seltext
>actually is, the Select Case statement then does different things:
>Select Case Ucase(txtedit.seltext:)
>    Case "RNOLD"
>        do this
>    Case "HARRY"
>        do this
>    Case "DAVID"
>        do something else
>etc.

>As you can see, the first one is actually supposed to be ARNOLD, but my
>problem is that names, as you know are different lengths. I deal with longer
>names this way...
>What is harder is how to deal with shorter names, like JIM, or BOB, etc..

>I've tried a Case Right("BOB", 3) or Case Right(txtEdit.SelText, 3)) = "BOB"
>I realize I'm going in the wrong direction, but I'm stumped as to figuring
>out how to do it ...
>Any ideas?

First I would check and see how many characters are in the textbox by using the
Len function

txtLength = Len(txtedit.text)
or with a variable
txtLenght = Len(MyText)

Now you can use if statements

if txtLength > 4 then
   ' code to highlight 5 characters
if txtLength = 4 then
   ' code to highlight 4 characters
if txtLenght = 3 then
  ' code to highlight 3 characters

and so on



Fri, 26 Jul 2002 03:00:00 GMT  
 Select Case statement is driving me mad!
That's good - and I fully understand it, but this is not a text box, it's a
rich text box where people are writing lots of stuff...I'm trying to capture
certain names, knowing that they are going to type space after it.
therefore, using the KeyPress event, when a space bar is pressed, that's why
I'm going back 5 spaces (a number I just picked out of the air, in this
case) and highlighting everything up to the space.
Then, I do whatever in the select case statement based on what is
highlighted.

--
David Wier


Quote:

> >I have a KeyPress event in a Rich Text box that involves a Select Case
> >Statement. If I hit a space bar, it automatically highlights backwards 5
> >spaces, then, since it's now txtedit.seltext, depending on what the
Seltext
> >actually is, the Select Case statement then does different things:
> >Select Case Ucase(txtedit.seltext:)
> >    Case "RNOLD"
> >        do this
> >    Case "HARRY"
> >        do this
> >    Case "DAVID"
> >        do something else
> >etc.

> >As you can see, the first one is actually supposed to be ARNOLD, but my
> >problem is that names, as you know are different lengths. I deal with
longer
> >names this way...
> >What is harder is how to deal with shorter names, like JIM, or BOB, etc..

> >I've tried a Case Right("BOB", 3) or Case Right(txtEdit.SelText, 3)) =
"BOB"
> >I realize I'm going in the wrong direction, but I'm stumped as to
figuring
> >out how to do it ...
> >Any ideas?

> First I would check and see how many characters are in the textbox by
using the
> Len function

> txtLength = Len(txtedit.text)
> or with a variable
> txtLenght = Len(MyText)

> Now you can use if statements

> if txtLength > 4 then
>    ' code to highlight 5 characters
> if txtLength = 4 then
>    ' code to highlight 4 characters
> if txtLenght = 3 then
>   ' code to highlight 3 characters

> and so on



Fri, 26 Jul 2002 03:00:00 GMT  
 Select Case statement is driving me mad!
Each time you press 'space', search backwards to the
previous 'space', then you should have the last whole word.

'space' = 'any whitespace'

Dag.


Quote:
> That's good - and I fully understand it, but this is not a text box, it's
a
> rich text box where people are writing lots of stuff...I'm trying to
capture
> certain names, knowing that they are going to type space after it.
> therefore, using the KeyPress event, when a space bar is pressed, that's
why
> I'm going back 5 spaces (a number I just picked out of the air, in this
> case) and highlighting everything up to the space.
> Then, I do whatever in the select case statement based on what is
> highlighted.

> --
> David Wier




> > >I have a KeyPress event in a Rich Text box that involves a Select Case
> > >Statement. If I hit a space bar, it automatically highlights backwards
5
> > >spaces, then, since it's now txtedit.seltext, depending on what the
> Seltext
> > >actually is, the Select Case statement then does different things:
> > >Select Case Ucase(txtedit.seltext:)
> > >    Case "RNOLD"
> > >        do this
> > >    Case "HARRY"
> > >        do this
> > >    Case "DAVID"
> > >        do something else
> > >etc.

> > >As you can see, the first one is actually supposed to be ARNOLD, but my
> > >problem is that names, as you know are different lengths. I deal with
> longer
> > >names this way...
> > >What is harder is how to deal with shorter names, like JIM, or BOB,
etc..

> > >I've tried a Case Right("BOB", 3) or Case Right(txtEdit.SelText, 3)) =
> "BOB"
> > >I realize I'm going in the wrong direction, but I'm stumped as to
> figuring
> > >out how to do it ...
> > >Any ideas?

> > First I would check and see how many characters are in the textbox by
> using the
> > Len function

> > txtLength = Len(txtedit.text)
> > or with a variable
> > txtLenght = Len(MyText)

> > Now you can use if statements

> > if txtLength > 4 then
> >    ' code to highlight 5 characters
> > if txtLength = 4 then
> >    ' code to highlight 4 characters
> > if txtLenght = 3 then
> >   ' code to highlight 3 characters

> > and so on



Fri, 26 Jul 2002 03:00:00 GMT  
 Select Case statement is driving me mad!
OK - I'll bite - how can you search backwards? I have no idea
Also, it could be at the beginning of a line (with a vbcrlf at the end of
the last line)
I know how to search forwards with Instr - but I have no idea on how to go
backwards, so I'll need syntax and an example.
Thanks for the input.
--
David Wier


Quote:
> Each time you press 'space', search backwards to the
> previous 'space', then you should have the last whole word.

> 'space' = 'any whitespace'

> Dag.



> > That's good - and I fully understand it, but this is not a text box,
it's
> a
> > rich text box where people are writing lots of stuff...I'm trying to
> capture
> > certain names, knowing that they are going to type space after it.
> > therefore, using the KeyPress event, when a space bar is pressed, that's
> why
> > I'm going back 5 spaces (a number I just picked out of the air, in this
> > case) and highlighting everything up to the space.
> > Then, I do whatever in the select case statement based on what is
> > highlighted.

> > --
> > David Wier




> > > >I have a KeyPress event in a Rich Text box that involves a Select
Case
> > > >Statement. If I hit a space bar, it automatically highlights
backwards
> 5
> > > >spaces, then, since it's now txtedit.seltext, depending on what the
> > Seltext
> > > >actually is, the Select Case statement then does different things:
> > > >Select Case Ucase(txtedit.seltext:)
> > > >    Case "RNOLD"
> > > >        do this
> > > >    Case "HARRY"
> > > >        do this
> > > >    Case "DAVID"
> > > >        do something else
> > > >etc.

> > > >As you can see, the first one is actually supposed to be ARNOLD, but
my
> > > >problem is that names, as you know are different lengths. I deal with
> > longer
> > > >names this way...
> > > >What is harder is how to deal with shorter names, like JIM, or BOB,
> etc..

> > > >I've tried a Case Right("BOB", 3) or Case Right(txtEdit.SelText, 3))
=
> > "BOB"
> > > >I realize I'm going in the wrong direction, but I'm stumped as to
> > figuring
> > > >out how to do it ...
> > > >Any ideas?

> > > First I would check and see how many characters are in the textbox by
> > using the
> > > Len function

> > > txtLength = Len(txtedit.text)
> > > or with a variable
> > > txtLenght = Len(MyText)

> > > Now you can use if statements

> > > if txtLength > 4 then
> > >    ' code to highlight 5 characters
> > > if txtLength = 4 then
> > >    ' code to highlight 4 characters
> > > if txtLenght = 3 then
> > >   ' code to highlight 3 characters

> > > and so on



Fri, 26 Jul 2002 03:00:00 GMT  
 Select Case statement is driving me mad!
If the user presses the space bar, start decrementing the SelStart and
incrementing the SelLength property until SelStart is at a space.

    If KeyAscii = vbKeySpace Then
        sl = 1
        With RTB
            sl = 2
            ss = .SelStart
            .SelStart = ss - 1
            .SelLength = sl
            Do While (Asc(.SelText) <> vbKeySpace) And (Asc(.SelText) <> 10) And (.SelStart > 1)
                .SelStart = .SelStart - 1
                .SelLength = sl
                sl = sl + 1
            Loop
            .SelStart = .SelStart + 1
            .SelLength = sl
            Name = .SelText
            .SelStart = ss
        End With
    End If

Now you will end up with the full name in the Name string.

You still have to compare it against all of your names.  How about you
try to find it in a large string that has all of the names?  That way,
you will know in one command whether or not to even attempt the Case
lines.

  'Bob starts at 11th chr., Carol at 21, Ted at 31, Alice at 41, more at 51, 61...
  Test = "..........BOB......CAROL.....TED.......ALICE....."
  Name = "." & UCase$(Name) & "."

  Select Case InStr(Test, Name) \ 10
  Case 0  'No match
  Case 1  'Bob
  Case 2  'Carol
  Case 3  'Ted
  Case 4  'Alice
  End Select

The most common case will be when a space was input but no name
was found, assuming the user is entering more than just names.
In that case, Instr returns a 0, and the first Case is executed.
If it was found then Instr returned some value higher than 0 and
the specific case can be found by comparing numbers instead of strings.
Numbers work better.

Notice how this avoids checking every word (ending in a space) the user
enters, against all the names listed in your select/case section.
It will execute faster.

HTH
LFS

Quote:

> I have a KeyPress event in a Rich Text box that involves a Select Case
> Statement. If I hit a space bar, it automatically highlights backwards 5
> spaces, then, since it's now txtedit.seltext, depending on what the Seltext
> actually is, the Select Case statement then does different things:
> Select Case Ucase(txtedit.seltext:)
>     Case "RNOLD"
>         do this
>     Case "HARRY"
>         do this
>     Case "DAVID"
>         do something else
> etc.

> As you can see, the first one is actually supposed to be ARNOLD, but my
> problem is that names, as you know are different lengths. I deal with longer
> names this way...
> What is harder is how to deal with shorter names, like JIM, or BOB, etc..

> I've tried a Case Right("BOB", 3) or Case Right(txtEdit.SelText, 3)) = "BOB"
> I realize I'm going in the wrong direction, but I'm stumped as to figuring
> out how to do it ...
> Any ideas?
> --
> David Wier



Fri, 26 Jul 2002 03:00:00 GMT  
 Select Case statement is driving me mad!
Take a look at the rtfText.Span method:

Something like:

Private Sub rtfText_KeyPress(KeyAscii As Integer)

    If KeyAscii = vbKeySpace Then
        KeyAscii = 0
        rtfText.Span Chr$(vbKeySpace), False, True
        Text1 = rtfText.SelText
        rtfText.SelText = rtfText.SelText & Chr$(vbKeySpace)
    End If

End Sub

HTH...

Dag.


Quote:
> OK - I'll bite - how can you search backwards? I have no idea
> Also, it could be at the beginning of a line (with a vbcrlf at the end of
> the last line)
> I know how to search forwards with Instr - but I have no idea on how to go
> backwards, so I'll need syntax and an example.
> Thanks for the input.
> --
> David Wier



> > Each time you press 'space', search backwards to the
> > previous 'space', then you should have the last whole word.

> > 'space' = 'any whitespace'

> > Dag.



> > > That's good - and I fully understand it, but this is not a text box,
> it's
> > a
> > > rich text box where people are writing lots of stuff...I'm trying to
> > capture
> > > certain names, knowing that they are going to type space after it.
> > > therefore, using the KeyPress event, when a space bar is pressed,
that's
> > why
> > > I'm going back 5 spaces (a number I just picked out of the air, in
this
> > > case) and highlighting everything up to the space.
> > > Then, I do whatever in the select case statement based on what is
> > > highlighted.

> > > --
> > > David Wier




> > > > >I have a KeyPress event in a Rich Text box that involves a Select
> Case
> > > > >Statement. If I hit a space bar, it automatically highlights
> backwards
> > 5
> > > > >spaces, then, since it's now txtedit.seltext, depending on what the
> > > Seltext
> > > > >actually is, the Select Case statement then does different things:
> > > > >Select Case Ucase(txtedit.seltext:)
> > > > >    Case "RNOLD"
> > > > >        do this
> > > > >    Case "HARRY"
> > > > >        do this
> > > > >    Case "DAVID"
> > > > >        do something else
> > > > >etc.

> > > > >As you can see, the first one is actually supposed to be ARNOLD,
but
> my
> > > > >problem is that names, as you know are different lengths. I deal
with
> > > longer
> > > > >names this way...
> > > > >What is harder is how to deal with shorter names, like JIM, or BOB,
> > etc..

> > > > >I've tried a Case Right("BOB", 3) or Case Right(txtEdit.SelText,
3))
> =
> > > "BOB"
> > > > >I realize I'm going in the wrong direction, but I'm stumped as to
> > > figuring
> > > > >out how to do it ...
> > > > >Any ideas?

> > > > First I would check and see how many characters are in the textbox
by
> > > using the
> > > > Len function

> > > > txtLength = Len(txtedit.text)
> > > > or with a variable
> > > > txtLenght = Len(MyText)

> > > > Now you can use if statements

> > > > if txtLength > 4 then
> > > >    ' code to highlight 5 characters
> > > > if txtLength = 4 then
> > > >    ' code to highlight 4 characters
> > > > if txtLenght = 3 then
> > > >   ' code to highlight 3 characters

> > > > and so on



Fri, 26 Jul 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. Select Case - What am I doing wrong?

2. VBScript Select Case vs VB Select Case

3. Select Case is driving me NUTTS.....

4. Select Case Statement Help

5. Select Case Statement

6. Criteria for select case statements

7. Embedded or nested Select Case statements

8. Using a wildcard string comparison in a select case statement

9. Can you nest Select Case statements?

10. If...Then...Else or Select Case Statement

11. Select Case Statement

12. If statement in select case problem

 

 
Powered by phpBB® Forum Software