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 10: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:


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

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

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 11:43 AM

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:

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 02: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 02:46 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 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.