Home All Groups Group Topic Archive Search About

Text box number of characters VB6

Author
22 Feb 2009 2:04 AM
Edward
What's the simplest way to count the number of characters entered into a
text box?

Examples I've found like:
Private Sub Command1_Click()
Text2.Text = Text1.Text.Length
End Sub

Fail

Any suggestions appreciated.

Author
22 Feb 2009 2:16 AM
MikeD
"Edward" <nospam@mail.com> wrote in message
news:Do2ol.60995$fM1.9833@newsfe14.iad...
> What's the simplest way to count the number of characters entered into a
> text box?
>
> Examples I've found like:
> Private Sub Command1_Click()
> Text2.Text = Text1.Text.Length
> End Sub
>
> Fail
>
> Any suggestions appreciated.

You were probably finding examples for VB.NET, quite a different beast than
VB6.

Use the Len() function.  Look it up in VB6's Help.

Text2.Text = Len(Text1.Text)

--
Mike
Author
22 Feb 2009 2:26 AM
Edward
"Edward" <nospam@mail.com> wrote in message
news:Do2ol.60995$fM1.9833@newsfe14.iad...
> What's the simplest way to count the number of characters entered into a
> text box?
>
> Examples I've found like:
> Private Sub Command1_Click()
> Text2.Text = Text1.Text.Length
> End Sub
>
> Fail
>
> Any suggestions appreciated.

Resolved using:

Text2.Text = Len(Text1)
Author
22 Feb 2009 5:52 PM
Rick Rothstein
Show quote Hide quote
>> What's the simplest way to count the number of characters entered into a
>> text box?
>>
>> Examples I've found like:
>> Private Sub Command1_Click()
>> Text2.Text = Text1.Text.Length
>> End Sub
>>
>> Fail
>>
>> Any suggestions appreciated.
>
> Resolved using:
>
> Text2.Text = Len(Text1)

Just for clarity in future code maintenance sessions, I would suggest you
specify the Text property for the TextBox in the Len function call...

Text2.Text = Len(Text1.Text)

--
Rick (MVP - Excel)
Author
23 Feb 2009 12:07 AM
Bill McCarthy
Show quote Hide quote
"Rick Rothstein" <rick.newsNO.SPAM@NO.SPAMverizon.net> wrote in message
news:%23tUapZRlJHA.4404@TK2MSFTNGP06.phx.gbl...
>>> What's the simplest way to count the number of characters entered into a
>>> text box?
>>>
>>> Examples I've found like:
>>> Private Sub Command1_Click()
>>> Text2.Text = Text1.Text.Length
>>> End Sub
>>>
>>> Fail
>>>
>>> Any suggestions appreciated.
>>
>> Resolved using:
>>
>> Text2.Text = Len(Text1)
>
> Just for clarity in future code maintenance sessions, I would suggest you
> specify the Text property for the TextBox in the Len function call...
>
> Text2.Text = Len(Text1.Text)
>

yep, and the case for consistency. If you use the default property in one
part of the expression best to use it in the other part.
Author
23 Feb 2009 12:06 AM
Bill McCarthy
Hi Edward,

Show quoteHide quote
"Edward" <nospam@mail.com> wrote in message
news:YI2ol.60997$fM1.39693@newsfe14.iad...
> "Edward" <nospam@mail.com> wrote in message
> news:Do2ol.60995$fM1.9833@newsfe14.iad...
>> What's the simplest way to count the number of characters entered into a
>> text box?
>>
>> Examples I've found like:
>> Private Sub Command1_Click()
>> Text2.Text = Text1.Text.Length
>> End Sub
>>
>> Fail
>>
>> Any suggestions appreciated.
>
> Resolved using:
>
> Text2.Text = Len(Text1)

As Rick suggests, it's generally better to avoid the use of default
properties, and instead write:
Text2.Text = Len(Text1.Text)
Author
23 Feb 2009 9:11 AM
Mike Williams
"Bill McCarthy" <Bill McCarthy Is An Identity Thief> wrote in message
news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...

> Hi Edward. As Rick suggests, it's generally better to avoid
> the use of default properties, and instead write:
> Text2.Text = Len(Text1.Text)

Hi Edward. As Rick suggests, and as McCarthy now also suggests, it's
generally better to avoid the use of default properties, and instead write:

    Text2.Text = Len(Text1.Text)

Anyone else confirm that?    ;-)

Mike
Author
23 Feb 2009 12:05 PM
Jim Mack
Mike Williams wrote:
>
> Hi Edward. As Rick suggests, and as McCarthy now also suggests, it's
> generally better to avoid the use of default properties, and
> instead write:
>
>     Text2.Text = Len(Text1.Text)
>
> Anyone else confirm that?    ;-)


No, in fact I'd dispute it. It's not only best to avoid default
properties, it's also best to avoid implicit conversions.

  Text2.Text = CStr(Len(Text1.Text))

  (-:

--
   Jim Mack
   Twisted tees at http://www.cafepress.com/2050inc
   "We sew confusion"
Author
23 Feb 2009 9:15 AM
Mike Williams
"Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...

> Hi Edward, As Rick suggests, it's generally better to avoid
> the use of default properties, and instead write:
> Text2.Text = Len(Text1.Text)

.. . . and another totally useless answer gets added to McCarthy's brownie
points total by his Micro$oft puppet master.
Author
23 Feb 2009 9:40 AM
Dave O.
"Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
news:eIOp5cZlJHA.1248@TK2MSFTNGP03.phx.gbl...
> "Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
> news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...
>
>> Hi Edward, As Rick suggests, it's generally better to avoid
>> the use of default properties, and instead write:
>> Text2.Text = Len(Text1.Text)
>
> . . . and another totally useless answer gets added to McCarthy's brownie
> points total by his Micro$oft puppet master.

No brownie points for that, it uses ETC. It should read:
Text2.Text = CStr(Len(Text1.Text))

Dave O.
Author
23 Feb 2009 9:47 AM
Bill McCarthy
Hi Dave,

Show quoteHide quote
"Dave O." <nob***@nowhere.com> wrote in message
news:eJJTVrZlJHA.1252@TK2MSFTNGP03.phx.gbl...
>
> "Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
> news:eIOp5cZlJHA.1248@TK2MSFTNGP03.phx.gbl...
>> "Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
>> news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...
>>
>>> Hi Edward, As Rick suggests, it's generally better to avoid
>>> the use of default properties, and instead write:
>>> Text2.Text = Len(Text1.Text)
>>
>> . . . and another totally useless answer gets added to McCarthy's brownie
>> points total by his Micro$oft puppet master.
>

Oh Williams still trolling... how sad for him.

> No brownie points for that, it uses ETC. It should read:
> Text2.Text = CStr(Len(Text1.Text))
>


Len() returns a Long which is always safely converted to a String, i.e a
widening conversion. Implicit widening conversions are seldom a problem.
Implicit narrowing conversions are what are generally referred to as evil
type conversion.
Author
23 Feb 2009 1:33 PM
Dave O.
Show quote Hide quote
"Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
news:uFtl8yZlJHA.1252@TK2MSFTNGP03.phx.gbl...
> Hi Dave,
>
> "Dave O." <nob***@nowhere.com> wrote in message
> news:eJJTVrZlJHA.1252@TK2MSFTNGP03.phx.gbl...
>>
>> "Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
>> news:eIOp5cZlJHA.1248@TK2MSFTNGP03.phx.gbl...
>>> "Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
>>> news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...
>>>
>>>> Hi Edward, As Rick suggests, it's generally better to avoid
>>>> the use of default properties, and instead write:
>>>> Text2.Text = Len(Text1.Text)
>>>
>>> . . . and another totally useless answer gets added to McCarthy's
>>> brownie points total by his Micro$oft puppet master.
>>
>
> Oh Williams still trolling... how sad for him.
>
>> No brownie points for that, it uses ETC. It should read:
>> Text2.Text = CStr(Len(Text1.Text))
>>
>
>
> Len() returns a Long which is always safely converted to a String, i.e a
> widening conversion. Implicit widening conversions are seldom a problem.
> Implicit narrowing conversions are what are generally referred to as evil
> type conversion.

Who cares, it's bad practice which if used where it's probably OK will be
used where it's not OK.
If it's OK to allow implicit type conversion which always seem to work, then
it's equally OK to omit default properties because that always seems to
work - please try to be consistent.
VB.NET may encourage sloppy programming, but there is no need to spread that
rot here.

Dave O.
Author
23 Feb 2009 1:52 PM
Bill McCarthy
Show quote Hide quote
"Dave O." <nob***@nowhere.com> wrote in message
news:OGQ2gtblJHA.4372@TK2MSFTNGP02.phx.gbl...
>
> "Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
> news:uFtl8yZlJHA.1252@TK2MSFTNGP03.phx.gbl...
>> Hi Dave,
>>
>> "Dave O." <nob***@nowhere.com> wrote in message
>> news:eJJTVrZlJHA.1252@TK2MSFTNGP03.phx.gbl...
>>>
>>> "Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
>>> news:eIOp5cZlJHA.1248@TK2MSFTNGP03.phx.gbl...
>>>> "Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
>>>> news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...
>>>>
>>>>> Hi Edward, As Rick suggests, it's generally better to avoid
>>>>> the use of default properties, and instead write:
>>>>> Text2.Text = Len(Text1.Text)
>>>>
>>>> . . . and another totally useless answer gets added to McCarthy's
>>>> brownie points total by his Micro$oft puppet master.
>>>
>>
>> Oh Williams still trolling... how sad for him.
>>
>>> No brownie points for that, it uses ETC. It should read:
>>> Text2.Text = CStr(Len(Text1.Text))
>>>
>>
>>
>> Len() returns a Long which is always safely converted to a String, i.e a
>> widening conversion. Implicit widening conversions are seldom a problem.
>> Implicit narrowing conversions are what are generally referred to as evil
>> type conversion.
>
> Who cares, it's bad practice which if used where it's probably OK will be
> used where it's not OK.
> If it's OK to allow implicit type conversion which always seem to work,
> then it's equally OK to omit default properties because that always seems
> to work - please try to be consistent.

I suggest you look at the differences: in particular the differences between
widening conversions and narrowing.
A widening conversion is guaranteed to work.  The only problems I have ever
seen it cause is in code readability as the type may not be clear.
Default properties on the other hand can cause a whole heap of code
maintenance issues on top of any readability issues.

Narrowing conversions are of course a different matter. They should be
handled explicitly because there is the chance they fail.


> VB.NET may encourage sloppy programming, but there is no need to spread
> that rot here.
>

ROFL !  First, .NET was mentioned in regard to this, but now you mention it,
the fact is .Net is superior, not inferior.  .NET has explicit typing,
strong typing, optional strict semantics.  It's VB6 which forces you to use
assignment to force a cast.  But that's all an aside to what was being
discussed.    But if you like I'm happy to go into more detail there, such
as how VB on .NET has the CType operator, operator overloading, as well as
DirectCast etc. Let me know if you need more detail, as I'd hate you to
wrongly think VB.NET encourages sloppy programming.  it allows the same
sloppiness as VB6 does, but it also allows you to enforce better programming
practices.  HTH's :)
Author
23 Feb 2009 2:10 PM
Cor Ligthert[MVP]
Bill,

Australia is much larger then the Netherlands.

I hope you don't mind that I feel me perfectly well here.

:-)

Cor
Author
23 Feb 2009 7:23 PM
Karl E. Peterson
Cor Ligthert[MVP] wrote:
> I hope you don't mind that I feel me perfectly well here.

Cor, why don't you go feel yourself somewhere else, eh?  ;-)
--
..NET: It's About Trust!
http://vfred.mvps.org
Author
23 Feb 2009 4:00 PM
Dave O.
Show quote Hide quote
"Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
news:uPxb84blJHA.4696@TK2MSFTNGP02.phx.gbl...

>> Who cares, it's bad practice which if used where it's probably OK will be
>> used where it's not OK.
>> If it's OK to allow implicit type conversion which always seem to work,
>> then it's equally OK to omit default properties because that always seems
>> to work - please try to be consistent.
>
> I suggest you look at the differences: in particular the differences
> between widening conversions and narrowing.
> A widening conversion is guaranteed to work.  The only problems I have
> ever seen it cause is in code readability as the type may not be clear.
> Default properties on the other hand can cause a whole heap of code
> maintenance issues on top of any readability issues.
>
> Narrowing conversions are of course a different matter. They should be
> handled explicitly because there is the chance they fail.

I am well aware of the difference between for example converting a long to
an integer and converting an integer into a long,
In one case it will always work but in the other there could be a problem.
My point is that if you encourage non-expert users to omit the explicit type
conversion in some cases they are likey to leave it out where it is needed,
however if you encourage them to always be explicit then they will always
get it correct. There is no problem with specifying the conversion function
where the type is widening and it makes the code far clearer to read, so
omittion while technically acceptable is a sloppy practice especially when
teaching.

Dave O.
Author
23 Feb 2009 9:33 AM
Cor Ligthert[MVP]
Hi Bill,

This makes me curious, is there any explanation about this then then "In
generally better"?

In my opinion it is in generally better to use a property then a function.
However, I assume there is a special reason for this for VB6 and so I am
currious about that.

Cor

Show quoteHide quote
"Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...
> Hi Edward,
>
> "Edward" <nospam@mail.com> wrote in message
> news:YI2ol.60997$fM1.39693@newsfe14.iad...
>> "Edward" <nospam@mail.com> wrote in message
>> news:Do2ol.60995$fM1.9833@newsfe14.iad...
>>> What's the simplest way to count the number of characters entered into a
>>> text box?
>>>
>>> Examples I've found like:
>>> Private Sub Command1_Click()
>>> Text2.Text = Text1.Text.Length
>>> End Sub
>>>
>>> Fail
>>>
>>> Any suggestions appreciated.
>>
>> Resolved using:
>>
>> Text2.Text = Len(Text1)
>
> As Rick suggests, it's generally better to avoid the use of default
> properties, and instead write:
> Text2.Text = Len(Text1.Text)
>
>
Author
23 Feb 2009 9:51 AM
Bill McCarthy
Hi Cor,

"Cor Ligthert[MVP]" <Notmyfirstn***@planet.nl> wrote in message
news:%2378mXnZlJHA.1168@TK2MSFTNGP05.phx.gbl...
> Hi Bill,
>
> This makes me curious, is there any explanation about this then then "In
> generally better"?
>

Code that uses default properties can *in general* be harder to maintain.
For example, lets say you have a method Foo that expects a String argument,
Foo(ByVal x as string).  If you change that to variant then instead of the
textbox's text you end up getting the textbox itself.  That's kind of issue
can only be detected at runtime.


> In my opinion it is in generally better to use a property then a function.

Properties are an abstraction over functions to imply state. Where there is
no implied state, functions are typically more appropriate.


Show quoteHide quote
> However, I assume there is a special reason for this for VB6 and so I am
> currious about that.
>
> Cor
>
> "Bill McCarthy" <TPASoft.com Are Identity Thieves> wrote in message
> news:eWL8fzUlJHA.3760@TK2MSFTNGP03.phx.gbl...
>> Hi Edward,
>>
>> "Edward" <nospam@mail.com> wrote in message
>> news:YI2ol.60997$fM1.39693@newsfe14.iad...
>>> "Edward" <nospam@mail.com> wrote in message
>>> news:Do2ol.60995$fM1.9833@newsfe14.iad...
>>>> What's the simplest way to count the number of characters entered into
>>>> a text box?
>>>>
>>>> Examples I've found like:
>>>> Private Sub Command1_Click()
>>>> Text2.Text = Text1.Text.Length
>>>> End Sub
>>>>
>>>> Fail
>>>>
>>>> Any suggestions appreciated.
>>>
>>> Resolved using:
>>>
>>> Text2.Text = Len(Text1)
>>
>> As Rick suggests, it's generally better to avoid the use of default
>> properties, and instead write:
>> Text2.Text = Len(Text1.Text)
>>
>>
>