Lint316

Hey folks. I'm having a wee problem that I was wondering if somebody could give me some advice.

My windows form has a panel which i need to display buttons. I need it to read from my database and generate a list of buttons, each button providing access to the value it read from. For example, if there are four unique id's in the database, the panel loads with four buttons, each one pointing to a diffrent unique id which later opens a form displaying their information. So if another unique id was added, a fifth button would be displayed after refreshing. Any advice would be greatly appreciated because I have no idea how to do this




Re: Visual C# Express Edition Load Buttons Panel

IsshouFuuraibou

Well the one method is to make, initialize, and add the buttons to the panel at runtime and adjust the size.

int TrackY = 4;
...
Button temp = new Button();
temp.Click += new EventHandler(ButtonClicks);
temp.Tag = UniqueID;
temp.Location = new Point( X, Yi ); // Where Yi is based on the number of buttons
temp.Size = new Size();// If you want to adjust size;
temp.Text = UniqueID.ToString();
myPanel.Controls.Add(temp);
TrackY += temp.Height + 4; // Add some spacing

repeating that in a loop for the amount of Unique IDs is what you can do, but track Y as your doing that, finally to adjust the size...

myPanel.Height = TrackY;
this.height = myPanel.Location.Y + myPanel.Height + 4;

Alternatively you can let the control scroll, and at some point you'll want to have a max height for your form and switch to scrolling the panel

However this soon gets unreasonable as your number of Unique IDs grow, at some point there will be too many.

you should look into DataSet and Data binding, you'll be able to do a better interface utilizing data bindings for fields and allowing for advancing through.





Re: Visual C# Express Edition Load Buttons Panel

Lint316

Cheers for that. Is it even possible to do this a different way. Maybe use a Large Listbox or a ListView. Im new to c# and Im just finding out about these things now. I usually program in Java, thats why I came up with the buttons idea




Re: Visual C# Express Edition Load Buttons Panel

IsshouFuuraibou

You can do this in many ways,

Buttons,
ListBox,
ComboBox,
and many others

Mostly you'll change how you handle updating and accessing from the list.

In the case of ListBox (and comboBox) you're selection will be the Unique Id (as a string) and you use the events of the ListBox (doubleclick most likely) to bring up the selected Id. You'd be able to manage the ListBox entries simply with ListBox.Items.Clear and ListBox.Items.Add instead of having to deal with removing, creating, and adjusting your screen because of having to add a button (or any other control like that).

I don't know what your full goal is, but using data bindings would allow you to most likely combine the two screens (one to select the UID, the other to display info on them) into one screen where you have controls to select and move through your data set on the UID the fields would automatically follow provided your data bindings are done properly.