473,379 Members | 1,355 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,379 software developers and data experts.

Is there any easy method to do this?

there are 64 buttons(button1,button2....button64).
Also a textbox and button "ok ".

when 15 is entered into the textbox and clicked ok Backcolcor of button15 shauold change to red;

my Approach is
1.valve entered is passed to i
2.then using while loop i change the back color to red
while(i==15)
{button15.Backcolor=Color.red;
}

Above approach works but needs 64 while loops and that makes the code lenghty.


is there any method to reduce the code

i want to know wheather i can use "button"+i.tostring(); here to reduce the code
Nov 17 '09 #1

✓ answered by Curtis Rutland

First, no, you cannot build a string and use it to reference a variable.

Second, why would you use a loop if there is nothing to repeat? You're using "while" statements as if they were "if" statements.

If I had to do something like this, I would create the buttons in code, in a proper "for loop", rather than in the designer. And as I created them, I would add them to a System.Collections.Generic.Dictionary<int,Button>.

Expand|Select|Wrap|Line Numbers
  1. Dictionary<int, Buttons> buttonsDict = new Dictionary<int, Buttons>();
  2. for(int i=1; i<=64; i++)
  3. {
  4.   Button b = new Button();
  5.   //set all of button b's attributes
  6.   buttonsDict.Add(i, b);
  7. }
  8.  
Now, later, when your user wants to change the background, let's say they typed in "15". You can get that button reference like this:
Expand|Select|Wrap|Line Numbers
  1. buttonsDict[15].BackColor = Colors.Red;

2 1099
Curtis Rutland
3,256 Expert 2GB
First, no, you cannot build a string and use it to reference a variable.

Second, why would you use a loop if there is nothing to repeat? You're using "while" statements as if they were "if" statements.

If I had to do something like this, I would create the buttons in code, in a proper "for loop", rather than in the designer. And as I created them, I would add them to a System.Collections.Generic.Dictionary<int,Button>.

Expand|Select|Wrap|Line Numbers
  1. Dictionary<int, Buttons> buttonsDict = new Dictionary<int, Buttons>();
  2. for(int i=1; i<=64; i++)
  3. {
  4.   Button b = new Button();
  5.   //set all of button b's attributes
  6.   buttonsDict.Add(i, b);
  7. }
  8.  
Now, later, when your user wants to change the background, let's say they typed in "15". You can get that button reference like this:
Expand|Select|Wrap|Line Numbers
  1. buttonsDict[15].BackColor = Colors.Red;
Nov 17 '09 #2
thanks for .i think that solved my problem
Nov 17 '09 #3

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

Similar topics

6
by: JW | last post by:
I'm displaying product thumbnails with brief descriptions on web pages. Clicking on the product does a javascript popup with larger image and detailed description info passed to the javascript...
1
by: Guinness Mann | last post by:
I develop with SQL Server 2000 at work. Because I occasionally work at home I also have SQL Server there. Although the data is not all that important, occasionally I make changes to the...
5
by: Harold Howe | last post by:
Howdy, 1- Are property reads in effect volatile? Or is the compiler allowed to optimize property reads away? ie class foo { public int bar { get { return m_bar;}
2
by: Sasha | last post by:
Hi, What is an easy way to create DateTime from a string that contains date in the following format "yyyyMMdd"? Take care, Sasha
8
by: Adam Clauss | last post by:
I have a folder containing many subfolders (and subfolders and....) all containing various .cs files. Is there any "easy" way to get them all added to the solution. Preferable would be that the...
5
by: gregarican | last post by:
I have a simple VC# form that validates its text boxes' contents and I want to insert these text box contents into an SQL Server 2005 Express DB. I have added the DB as a data source but right off...
1
by: samadams_2006 | last post by:
Hello, I haven't done much programming in .asp and was wondering if someone could provide me with some sample code to do the following task... I have a Web Service called: ...
1
by: samadams_2006 | last post by:
Hello, I haven't done much programming in .asp and was wondering if someone could provide me with some sample code to do the following task... I have a Web Service called: ...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
2
by: jalanb3 | last post by:
Evening all, And thank you for your valuable reading skills. The following pattern turned up in coding tonight. It works, but I'm suspicious, it just seems too easy. So any comments or...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.