473,396 Members | 2,037 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Textbox access.

16
Hi. I am having difficulty looping through texboxes. I do not want to use Controls. I have named my textboxes as:

textBoxBuy1, textBoxBuy2, textBoxBuy3, .....

There are many more textboxes in the form (with different names so that I can use the desired ones in a loop) but I do not want to refer to them while looping. I want to loop all the textboxes named textBoxBuy only. This is what I am doing:

for (int i = 1; i < 6; i++)
{
if ((TextBox)Controls["textBoxBuy" + i.ToString()].Text.Length != 0) {}
}

The error that I am getting is:

Error 1 Cannot convert type 'int' to 'System.Windows.Forms.TextBox'

Can someone please help me out with this?
Thanks
Dec 13 '09 #1
2 1463
GaryTexmo
1,501 Expert 1GB
Sometimes it's good to break your lines down instead of trying to cram as much information as possible onto one line ;)

Your problem is brackets. You're applying the cast to Text.Length instead of the control. The line should actually look like...

Expand|Select|Wrap|Line Numbers
  1. if (((TextBox)Controls["textBoxBuy" + i.ToString()]).Text.Length != 0) { }
However, as I'm sure you've gathered by now this is a very difficult line to read and make sure you have correct. Maybe try something like the following... it's easier to read, and a little safer since it's quite possible that you don't find the control you're looking for with a text string.

Expand|Select|Wrap|Line Numbers
  1. for (int i = 1; i < 6; i++)
  2. {
  3.     TextBox tb = this.Controls["textBoxBuy" + i.ToString()] as TextBox;
  4.     if (tb != null && tb.Text.Length != 0)
  5.     {
  6.     }
  7. }
Dec 13 '09 #2
poko
16
Thanks Gary, it worked. And I'll try to remember your tip :)
Dec 13 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Neil | last post by:
Is there an editable RTF textbox control which allows the user to apply bold, italic, etc.? I tried the Microsoft Rich Textbox Control, but there doesn't seem to be a way to allow the user to...
3
by: hermawih | last post by:
Hi , can anyone help me , please . In Ms Rich textbox control , It is easy to insert object than to insert picture . I want to insert picture in my Ms rtf activeX control but Ms Access does...
3
by: Steve B. | last post by:
Using: VS dB: MS-Access Although the the DataGrid displays the proper date from an Access file (Date/Time DataType) an adjacent bound textbox for that same DataGrid field displays the date and...
2
by: Alex Shirley | last post by:
HI I'm trying to iterate through all the textboxes on a webpage and trim them for spaces. i.e. If a user enters " hello world " we correct it to "hello world" So far I've come up with...
1
by: guoqi zheng | last post by:
Dear sir, I have a textbox inside a repeart control, autopostback set to true for that textbox. When the value of this textbox changes, I need to access current row and update database. ...
19
by: hamil | last post by:
I have a form with one button, Button1, and a Textbox, Textbox1 I have a class, class1 as follows. Public Class Class1 Public DeForm As Object Sub doit() DeForm.Textbox1.text = "It works"...
3
by: DotNetNewbie | last post by:
I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days. I am having trouble getting one of the exercises to work at the end of day 4. Exercises: 1. Create a new...
9
by: davidr | last post by:
Hi, I've researched this a lot and have a well understanding but still can't get the darn textbox in the javascript. I have a textbox that is set to runat="server" and I have a client...
7
by: robert.waters | last post by:
I have an Access database frontend linked via ODBC to a large (gigabytes) mysql database. I need to view a large amount of data in a a textbox (variable up to 300K), but I receive a 'there isnt...
1
by: JFKJr | last post by:
Hello everyone, the following Access VBA code opens an excel file and creates textboxes in a given range of cells dynamically. The code attaches "MouseUP" and "Exit" events to the textboxes (using...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.