0

I'm trying to create an application where the button text is dynamically set.

enter image description here

The above blank buttons are supposed to be set once an item is added on the database. For example, I added 'New Item Here' on the database and it should look like the image below.

enter image description here

So far, what I do is add an item on the database and manually set the button text which requires rebuilding/reinstalling the application.

Is there a way I can automatically set the button text? Or perhaps, make a populated listview that looks like a group of buttons?

Thanks!

2 Answers 2

0

Establish a connection to the database, then pull out all the required data from it and assign each value to the button name

//Set button name

ButtonName.Text = variable name that holds the string from database ;
Sign up to request clarification or add additional context in comments.

1 Comment

I did that sir, but the problem is I got a lot of Buttons to set value. Like ButtonName1.Text="Item1", ButtonName2.Text="Item2"..until Button10. Is there a way I can put the statements into a loop so that it will not consume long lines?
0

Use this loop Statement

The data array represents buttonNames from database

 string[] data = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" };

int i = 1;
while (i < 11)
     {
 foreach (var x in data)
 {
         String button = "button";
         String Button = (button + i).ToString();
         Button.Text=x;
         i++;
     }
 }

In your GUI design, give the buttons names like "button1","button2" ,"button3" etc

2 Comments

Thank you sir. How am I going to do line Button1.Text = "item1"? It can't be the string see above.
@benziv the edited code above.. If you run into errors, let me know

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.