开发者

Assign or modify values in bulk for the form items in C#.net?

I have a set of 20 buttons named button1, button2, button3, button4, button5, button6, …, button20.

I want to assign new Text value for each button:

button1.Text = "something";
button2.Text = "anotherthing";
.
button20.Text = "somethingelse";

These values will be assigned on a button click event of a master button. There are multiple master buttons which assign different values to each of these small buttons (button1, button2, …, button20).

To make the value assignment easier and more convenient, I wanted to load these buttons in an array and fetch the values from the database file.

But how to arrange them in array? And how to assign the value to these buttons from the database automatically?

Hope this questio开发者_如何学Gon will be answered.


I wanted to load these button in array (but HOW TO arrange them in ARRAY?).

Try this:

Button[] buttons = {
    button1,
    button2,
    button3,
    ...
};

You could also consider creating the buttons dynamically instead of using the designer, as this will cut out a lot of the repetitive work and make it easier to change the number of buttons in the future.

And fetch fill the values to it from the database file

Generally you shouldn't read directly from database files. You should use a database API and for example query using SQL. You can do this from C# using ADO.NET. You may also find an OR/M such as LINQ to SQL or the Entity Framework useful so that you don't have to write the SQL yourself.


If you don't want to create the buttons dynamically try this.

void AssignValues()
        {
            foreach (Button btn in this.Controls)
            {
                btn.Text = "Some Value You Want";
            }
        }

you can edit it and put in event or something else it depend on your need.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜