Dynamically Create Checkbox With Scrolling - c#

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!

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).

Related

How to move labels that was added programmatically?

I have Xml file which stores names of objects. When I start the program, I load the object names and then make new label for each object name.
(I know there is posibility to move labels by using _MouseMove() and _MouseDown() methods, but it works only if I put the label by drag and drop in [Project] or create new label by Label label = new Label();. Generally: when control object has his own name.)
So is there any posibility to move labels which was added by the code like here below?
public List<Activities> listOfActivities = new List<Activities>();
listOfActivities = XmlSerialization.ReadFromXmlFile<List<Activities>>("activities.txt");
foreach (Activities activity in listOfActivities)
{
Point point = new Point(activity.xLabel, activity.yLabel);
Label label = new Label();
label.Text = activity.name;
label.Location = point;
label.Visible = true;
this.Controls.Add(label);
}

row0 of tableLayoutPanel keeps getting large

Good day!
We have a tableLayoutPanel, that is being use to add combo boxes and labels in runtime.
When the user clicks the button, what our code does is that, it creates the controls in runtime, and then it adds it to tableLayoutPanel's container.. When we click the 'Add' button once, we should see one cell created just like below
Now, when we add multiple controls, we notice that row 0 accumulates a large space. How do we remove that?
notice that program1 has large space, but program 2,3,4 have evenly space.. Why is that so? Thanks
Here is our code when the user clicks the 'Add' button
int cLeft = 1;
public System.Windows.Forms.ComboBox AddNewComboBox()
{
System.Windows.Forms.Label lab = new System.Windows.Forms.Label();
System.Windows.Forms.ComboBox com = new System.Windows.Forms.ComboBox();
tableLayoutPanel1.Controls.Add(lab,0,cLeft-1);
lab.Text = "Program " + cLeft.ToString() + ":";
lab.Name = "label" + cLeft.ToString();
tableLayoutPanel1.Controls.Add(com,1,cLeft-1);
com.Width = 220;
com.Name = "comboBox " + cLeft.ToString();
cLeft = cLeft + 1;
return com;
}
I encountered a similar problem once.
I think the problem is that the first row you insert is auto sized. If you add a blank row at the beginning that is auto sized and all the controls afterwards with a fixed size it should work.
Alternatively you could create an own UserControl wherein you place your label and combobox and add this with a maxheight to your tablelayout panel.

how to add more labels according to the later input?

I don't know whether it is clear. I mean a form has an input textbox and a button. If I input 5 in the textbox and click on the button, the form will add 5 labels...
The question is I don't know it is 5 or 4 or 3……before the code is running and the input.
I don't know how to add the labels and how to define or get their names in order to use them later in the code.
I am just learning windows applications development with VS using C#....
And also this is my first ask in stackoverflow please forgive me if it is not clear. Is there anybody can help me?
let's split your entire problem into few steps of understanding:
What basically down the line, you are asking, is to how to add controls dynamically in a winform, in your case the control is label, so wrap your label creating logic in a function like below:
protected Label CreateLabel(string Id, string text)
{
Label lbl = new Label();
lbl.Name = Id;
lbl.Text = text;
return lbl;
}
Now you need to add as many labels as the number entered in a given textBox and upon a button click, so possibly something like below in button's click event:
protected void button_Clicked(object sender, EventArgs e)
{
//make sure nothing invalid string comes here
int counter = Convert.ToInt32(txtCount.text);
for(int i=0;i<counter;i++)
{
var lbl = CreateLabel("rand"+i, "Label" +i);
container.Controls.Add(lbl);//container can be your form
}
}
Now the basic problem in winforms you will face, will be about the positioning of these dynamically added labels. The most simple way to go about it is to add your labels to winforms FlowLayoutPanel. It automatically aligns the controls. There are other layout controls available aswell. so do this :
drag and drop a FlowLayoutPanel on your form and give it the name "container", rest assured
For example:
for(var i=0; i<N; i++ ) {
var l= new Label();
l.Text = "some name #" + i.ToString();
l.Width = 200;
l.Location = new Point(30, 20);
parent.Controls.Add(l);
}
You can use this as:
Label[] arrLabel;
int num = 0;
int.TryParse(textBox1.Text, out num);
arrLabel = new Label[num];
for (int i = 0; i < num; i++)
{
arrLabel[i] = new Label();
arrLabel[i].Text = "Label #" + (i+1);
arrLabel[i].Width = 20;
arrLabel[i].Location = new Point(30+10*(i+1), 20);
this.Controls.Add(arrLabel[i]);
}

Adding Space into a StackPanel full of Textboxes

I am new at C# & XAML development. I created a metro app with several textboxes. These textboxes are loaded in XAML data through a StackPanel in C# code, it has to be hardcoded. The problem is, I have no clue how I can add some empty spaces between every single textbox. Has anyone an idea?
The Code :
private void AddLastestCreatedField()
{
// Load the last created Field From DB
DBFunction.FieldTypes latestField;
DBFunction.Class1 myDBClass = new DBFunction.Class1();
latestField = myDBClass.GetLastestField();
// add new textbox and put it on the screen
var dragTranslation = new TranslateTransform();
//Generate the TextBox
TextBox fieldTextBox = new TextBox();
fieldTextBox.Name = "fieldTextBox_" + latestField.ID.ToString();
fieldTextBox.FontSize = 15;
fieldTextBox.Background.Opacity = 0.8;
ToolTip toolTip = new ToolTip();
toolTip.Content = latestField.Description;
ToolTipService.SetToolTip(fieldTextBox, toolTip);
fieldTextBox.IsReadOnly = true;
// Add Drag and Drop Handler for TextBox
fieldTextBox.ManipulationMode = ManipulationModes.All;
fieldTextBox.ManipulationDelta += fieldTextBox_ManipulationDelta;
fieldTextBox.ManipulationCompleted += fieldTextBox_ManipulationCompleted;
fieldTextBox.RenderTransform = dragTranslation;
dragTranslationDict.Add(fieldTextBox.Name, dragTranslation);
fieldTextBox.RenderTransform = dragTranslation;
// Add TextBox to a List to control later
TxtBoxList.Add(fieldTextBox);
// Generate TextBlock for each TextBlock
TextBlock fieldTextBlock = new TextBlock();
// fieldTextBlock.Name = "fieldTextBlock_" + cnt.ToString();
fieldTextBlock.TextAlignment = TextAlignment.Right;
fieldTextBlock.HorizontalAlignment = Windows.UI.Xaml.HorizontalAlignment.Right;
fieldTextBlock.Name = "fieldTextBlock_" + latestField.ID.ToString();
fieldTextBlock.Text = latestField.Name;
fieldTextBlock.FontSize = 15;
fieldTextBlock.Height = 33;
// Add Drag and Drop Handler for TextBlock
var dragTranslation2 = new TranslateTransform();
fieldTextBlock.RenderTransform = dragTranslation2;
dragTranslationDict2.Add(fieldTextBlock.Name, dragTranslation2);
// Add TextBlock to a list to control later
TxtBlockList.Add(fieldTextBlock);
TextBoxStack.Children.Add(fieldTextBox);
TextBlockStack.Children.Add(fieldTextBlock);
}
I'll skip the usual "What have you tried?" question and say you probably can get what you need by setting the Margin property on the TextBox - the Margin property will add "space" around the control size as a sort of padding (not to be confused with the Padding property, which will add space inside the control extents)
I don't know what you are really up to, but either use the Margin-property of the textbox. It defines, how much space there will be around the control,
See MSDN for more information.

C# Winforms - Adding forms to a FlowPanel control

I have a page where I have to modify variables which are strings with pairs of values and labels. I was using a datagrid object but its not sufficient for whats required ( or eventually will not anyway ).
So I have a form which is a text label and textbox, and a flowpanel, and I'm trying to programmatically add instances of this form for each variable to the flowpanel, and Im getting nothing. Googling for the solution bring sup lots of video tutorials involving clicking on buttons in the UI designer and dropping them on flow panels, I want to do this programmatically however.
What is the 'correct' or 'standard' way of doing this.
The data (in pairs) sounds like it might fit better with a TableLayoutPanel, but the theory is the same; just call .Controls.Add(...) and it should work:
FlowLayoutPanel panel = new FlowLayoutPanel();
Form form = new Form();
panel.Dock = DockStyle.Fill;
form.Controls.Add(panel);
for (int i = 0; i < 100; i++)
{
panel.Controls.Add(new TextBox());
}
Application.Run(form);
or with a TableLayoutPanel:
TableLayoutPanel panel = new TableLayoutPanel();
Form form = new Form();
panel.Dock = DockStyle.Fill;
panel.ColumnCount = 2;
form.Controls.Add(panel);
for (int i = 0; i < 100; i++)
{
panel.Controls.Add(new Label { Text = "label " + i });
panel.Controls.Add(new TextBox { Text = "text " + i });
}
Also - I wonder if a PropertyGrid would fit your needs better? This will handle all the "get value", "show value", "parse value", "store value" logic, and can be plugged with things like ICustomTypeDescriptor to allow dynamic properties.
To add instances of a form to a flowlayout panel, I do the following:
Form1 f1 = new Form1();
f1.TopLevel = false;
f1.Visible = true;
flowLayoutPanel1.Controls.add(f1);
Seems to work OK in my test code.

Categories

Resources