sora0419
sora0419

Reputation: 2378

Dynamically Create Checkbox With Scrolling

I'm trying to build a form application in Visual Studio 2010 using C#.

The program will be reading a excel file that contains a list of filenames, and will dynamically generate textbox for each filename.

Below is my code, just for clarification. I wanted to make the label a link to the file, that's why I didn't use checkboxes[i].Text = filename

CheckBox[] checkboxes = new CheckBox[fileCount];
Label[] labels = new Label[fileCount];
for (int i = 0; i < fileCount; i++ )
{
    //creating a checkbox
    checkboxes[i] = new CheckBox();
    checkboxes[i].Location = new Point(360, (145 + i * 30));
    checkboxes[i].Name = String.Format("checkbox{0}", i.ToString());
    this.Controls.Add(checkboxes[i]);

    //creating filename label
    labels[i] = new Label();
    labels[i].Location = new Point(20, (150 + i * 30));
    labels[i].Text = existingFiles[i];
    labels[i].Width = 330;
    this.Controls.Add(labels[i]);
}

Say if fileCount equals to 100, it will make the form really big/long and won't be able to fit properly on most monitors.

Is there a way to make all dynamically generated checkboxes and labels all grouped in a area and just have the user be able to scroll? Something like a panel with scrolling? I don't know if there's anything like that.

I thought about using CheckedListBox, but doing that way I won't be able to make the filename a link. I want the user be able to click on the label and the file will be opened automatically, instead of selecting it.

Any help is appreciated!

Upvotes: 0

Views: 951

Answers (1)

Quoendithas
Quoendithas

Reputation: 124

Most controls have the AutoScroll property. Set this to true, and the control will automatically add a scrollbar when necessary. You can use the Panel control and add all of your links/checkboxes to that (if you don't want your whole form to scroll).

Upvotes: 1

Related Questions