Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET Communications (http://www.xtremevbtalk.com/-net-communications/)
-   -   Controls.Find: how to use? (http://www.xtremevbtalk.com/-net-communications/325891-controls.html)

ecka333 04-26-2013 11:01 AM

Controls.Find: how to use?
 
I am doing my four channel timer for aquarium project. I am using Visual basic.Net Express 2010. Today i faced one ugly problem in this line:

Code:

Me.Controls.Find("TextBox" & Address, True)(0).Text = "0"
Compiler shows error:
"IndexOutOfRangeException was unhandled." and
"Index was outside the bounds of the array."
As i suspect "TextBox" & Address in the parentheses causes the problem. Address variable is byte type and it can be from 1 to 48 and from 53 to 55.
Tried to use
Code:

Me.Controls.Find("TextBox" + Cstr(Address), True)(0).Text = "0"
but result is the same. Then changed text to:
Code:

Me.Controls.Find("TextBox1", True)(0).Text = "0"
and it worked. But this is not a way out to me.
Maybe somebody faced with similar problem?

AtmaWeapon 04-26-2013 12:43 PM

First, don't ever use "+" to combine strings. Use "&". Depending on circumstances, "+" will sometimes interpret the strings as numbers and perform addition. (I doubt this is the problem.)

Second, how sure are you that Address contains "1", with no spaces or other characters? Have you verified this in a debugger?

Third, have you considered using a control array? It's easier to debug and more elegant, in my opinion.

If I were forced to write this code, it would look like this:
Code:

Dim textBoxId As String = String.Format("TextBox{0}", Address)
Dim candidates() As Control = Me.Controls.Find(textBoxId, True)
candidates(0).Text = "0"

Using more variables makes things easier to debug. It's rarely a bad idea.

katrina 05-27-2013 03:08 AM

I have faced with such problems before and couldn't find solutions. I'll try to use advice mentioned above. I think it'll be helpful.


All times are GMT -6. The time now is 11:15 PM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2017, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2017 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.