-
Sep 29th, 2022, 11:32 AM
#1
Thread Starter
PowerPoster
What is happening here ?
Hi,
I don't understand what is happening here...
I have a subroutine which is called when I change the text in a TextBox...
vb.net Code:
Private Sub BoxChange(sender As Object, e As System.EventArgs)
Static chk As String
If sender.text = chk Then Exit Sub ' Stop recursion.
chk = sender.Text.ToString.ToUpper
sender.Text = chk
End Sub
The textbox only has to have one letter, I enter a letter and control passes to this subroutine, if it's text doesn't match chk then control passes to Line 4, otherwise 'Exit Sub'.
From Line 4, chk is changed to upper case, then the textbox text is changed to chk, so the subroutine is called again, but this time its text IS the same as chk so control jumps to Line 6. Fine that's what it's supposed to do, BUT...
Step once more and the control jumps back to Line 5.
What's going on ?
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Sep 29th, 2022, 11:49 AM
#2
Re: What is happening here ?
If sender.text = chk Then Exit Sub
Check the value of sender.text and chk at that line.
-
Sep 29th, 2022, 11:52 AM
#3
Re: What is happening here ?
That seems really messy and confusing code. It is calling ToString on a .Text property that is already a string, it doesn't have Option Strict set either.
The problem you are seeing is down to recursion, step through the code again but this time have the call stack window open. That should explain why.
-
Sep 29th, 2022, 02:54 PM
#4
Lively Member
Re: What is happening here ?
I think it is something like this. You have the first pass through the subroutine. When it comes to line 5 the textbox value changes. But it did not finish doing that line because the subroutine is called again for a second pass through. When that second pass through is done it continues where it left off on the first pass through. And that was on line 5.
-
Sep 29th, 2022, 03:27 PM
#5
Thread Starter
PowerPoster
Re: What is happening here ?
Originally Posted by PlausiblyDamp
That seems really messy and confusing code. It is calling ToString on a .Text property that is already a string, it doesn't have Option Strict set either.
Yes, I agree, You'd think that 'chk = sender.Text.ToUpper' would do the job wouldn't you? However, doing that the '.ToUpper' part just throws up an error, 'ToLower' it seems is ok, (I've not tried it, it's likely to be lower case already) but 'ToUpper' isn't even offered.
Poppa
Along with the sunshine there has to be a little rain sometime.
-
Sep 29th, 2022, 05:24 PM
#6
Re: What is happening here ?
Originally Posted by Poppa Mintin
Yes, I agree, You'd think that 'chk = sender.Text.ToUpper' would do the job wouldn't you? However, doing that the '.ToUpper' part just throws up an error, 'ToLower' it seems is ok, (I've not tried it, it's likely to be lower case already) but 'ToUpper' isn't even offered.
Poppa
.ToLower works but .ToUpper throws an error? That sounds weird - what error does it throw?
-
Sep 29th, 2022, 08:58 PM
#7
Re: What is happening here ?
You need to turn Option Strict On. You're trying to fix issues that shouldn't even exist in the first place. Just do the right thing from the start and there will be fewer issues to fix.
-
Sep 30th, 2022, 03:01 AM
#8
Re: What is happening here ?
Originally Posted by Poppa Mintin
Hi,
I don't understand what is happening here...
I have a subroutine which is called when I change the text in a TextBox...
vb.net Code:
Private Sub BoxChange(sender As Object, e As System.EventArgs)
Static chk As String
If sender.text = chk Then Exit Sub ' Stop recursion.
chk = sender.Text.ToString.ToUpper
sender.Text = chk
End Sub
The textbox only has to have one letter, I enter a letter and control passes to this subroutine, if it's text doesn't match chk then control passes to Line 4, otherwise 'Exit Sub'.
From Line 4, chk is changed to upper case, then the textbox text is changed to chk, so the subroutine is called again, but this time its text IS the same as chk so control jumps to Line 6. Fine that's what it's supposed to do, BUT...
Step once more and the control jumps back to Line 5.
What's going on ?
Poppa
In fact, the more I think about this the more I am convinced you shouldn't be doing things this way. If you really must do this through code then just handle the KeyPress or KeyDown events, however a TextBox has a CharacterCasing property that will do this for you anyway.
-
Sep 30th, 2022, 04:44 AM
#9
Re: What is happening here ?
What is happening here? I don't know what is happening there, where you are but here I am having a cup of tea and a Digestive biscuit. I'm doing that whilst thinking that your requests for help could have a more intelligently thought out subject title that helps people to determine whether they want to dive into your request or not.
Perhaps you could title the next one, "unspecified problem from Yaddlethorpe" - but I suppose that might be providing TOO much information.
https://github.com/yereverluvinunclebert
Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.
By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.
-
Sep 30th, 2022, 07:05 AM
#10
Thread Starter
PowerPoster
Re: What is happening here ?
Originally Posted by jmcilhinney
You need to turn Option Strict On. You're trying to fix issues that shouldn't even exist in the first place. Just do the right thing from the start and there will be fewer issues to fix.
Option Strict is on by default.
Along with the sunshine there has to be a little rain sometime.
-
Sep 30th, 2022, 08:07 AM
#11
Re: What is happening here ?
The code you posted will not compile with Option Strict On, so you must either have it Off at the project level or the file level. Your 'sender' parameter is type Object, as is the case for all event handlers, and yet you are accessing its 'text' property. Nope.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|