Is there any method to add a scroll bar to a group box? My code scenario is: I have many groupboxes on a form. All the groupboxes will appear in the same place and also the height and width is fixed. I'm setting groupboxes visible true/false according to a condition.
Some groupboxes ecxeed the width and height, so I want to put this all in scrollbar. Can anyone help me regarding this?
If its an Windows application then you need to add a panel to Group box and set a property "AutoScroll"=true.
place a groupbox on your form then add this code
int btnPos = 1;
Panel pnl = new Panel();
pnl.AutoScroll = true;
pnl.Top = 15;
pnl.Left = 2;
pnl.Width = groupBox1.Width - 8;
for (int i = 0; i < 22; i++)
{
Button _btn = new Button();
_btn.Text = "lbl";
_btn.Top = btnPos;
btnPos += 23;
pnl.Controls.Add(_btn);
}
groupBox1.Controls.Add(pnl);
Related
in c# winform, How to keep a control always in the center bottom of a form when form's size changed?
if I use DockStyle.Bottom, I can't set its width.
I tried it out. The following is the code, but I don't know why? It's mainly because I don't understand why the Left value will change when the Anchor. Bottom is set?
public void Run()
{
Form form = new Form();
form.Width = 600;
form.Height = 600;
Button btnOK = new Button();
btnOK.Text = "OK";
btnOK.Left = form.ClientSize.Width / 2 - btnOK.Width;
btnOK.Top = 0;
btnOK.Anchor = AnchorStyles.Bottom;
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.Left = form.ClientSize.Width / 2;
btnCancel.Top = 0;
btnCancel.Anchor = AnchorStyles.Bottom;
Panel panel = new Panel();
panel.Height = btnOK.Height;
panel.Width = form.ClientSize.Width;
panel.Dock = DockStyle.Bottom;
panel.Controls.Add(btnOK);
panel.Controls.Add(btnCancel);
form.Controls.Add(panel);
form.Show();
}
Use a TableLayoutPanel. You can Dock it to the Bottom or Anchor it to the Bottom, Left and Right. You would have one row then have a column with an absolute width for each control and one extra column on the left and another on the right with 50% width. Those two extra columns will take up half the empty space each, thus keeping the others in the middle.
I'm supposed to create a magic square in 2D using Windows Forms Application. It should look like this:
However, the user should be able to decide the size of the square (3x3, 5x5, 7x7, etc). I already wrote the code in a Console Application, but I don't know how to add the 2D graphics.
Somebody already asked this question (How do I put my result into a GUI?), and one of the answers was to use DataGridView, but I'm not sure if that's what I'm looking for, since I can't make it look like the picture.
Any ideas or advice?
You can use a TableLayoutPanel and add buttons to panel dynamically.
If you don't need interaction with buttons, you can add Label instead.
Create square dynamically:
public void CreateSquare(int size)
{
//Remove previously created controls and free resources
foreach (Control item in this.Controls)
{
this.Controls.Remove(item);
item.Dispose();
}
//Create TableLayoutPanel
var panel = new TableLayoutPanel();
panel.RowCount = size;
panel.ColumnCount = size;
panel.BackColor = Color.Black;
//Set the equal size for columns and rows
for (int i = 0; i < size; i++)
{
var percent = 100f / (float)size;
panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, percent));
panel.RowStyles.Add(new RowStyle(SizeType.Percent, percent));
}
//Add buttons, if you have your desired output in an array
//you can set the text of buttons from your array
for (var i = 0; i < size; i++)
{
for (var j = 0; j < size; j++)
{
var button = new Button();
button.BackColor = Color.Lime;
button.Font = new Font(button.Font.FontFamily, 20, FontStyle.Bold);
button.FlatStyle = FlatStyle.Flat;
//you can set the text of buttons from your array
//For example button.Text = array[i,j].ToString();
button.Text = string.Format("{0}", (i) * size + j + 1);
button.Name = string.Format("Button{0}", button.Text);
button.Dock = DockStyle.Fill;
//If you need interaction with buttons
button.Click += b_Click;
panel.Controls.Add(button, j, i);
}
}
panel.Dock = DockStyle.Fill;
this.Controls.Add(panel);
}
If you need interaction with buttons
void button_Click(object sender, EventArgs e)
{
var button = (Button)sender;
//Instead put your logic here
MessageBox.Show(string.Format("You clicked {0}", button.Text));
}
As an example, you can call
CreateSquare(3);
Screenshot:
You can create a Form and add a TableLayoutPanel with this property
tableLayoutPanel1.Dock = DockStyle.Fill;
tableLayoutPanel1.BackColor = Color.Gold;
and this is the result
When you create Row and Column, to fit correctly set the percentage in this way:
After this you can add a Button or Label in each square.
I have a few buttons to add on the form. In the code I'm setting up some button properties:
class DigitButton : Button
{
private static int digitBtnTag;
public DigitButton()
: base()
{
this.Size = new Size(30, 30);
this.Tag = digitBtnTag;
this.Text = (this.Tag).ToString();
this.Margin = new Padding(2);
this.Padding = new Padding(2);
digitBtnTag++;
}
}
In the MainForm.cs I have
for (int i = 0; i < dgtBtns.Length; i++)
{
dgtBtns[i] = new DigitButton();
dgtBtns[i].Click += new EventHandler(this.digitButtonClick);
digitPanel.Controls.Add(dgtBtns[i]);
}
So when I launch a program I see all my buttons in the one place: (0;0) on digitPanel despite property Margin. So why don't all these buttons automaticly "push" each other in the different directions? And how to make it?
Have you tried using a FlowLayout Panel ?
Also, this video might help:
Windows Forms Controls Lesson 5: How to use the FlowLayout Panel
that's not the way controls works in c#. i'm guessing you programed at java a bit because the layout in jave works that whay, but in c# just do
for (int i = 0; i < dgtBtns.Length; i++)
{
dgtBtns[i] = new DigitButton();
dgtBtns[i].Location = new Point(50, 50 * i); // Multiplying by i makes the location shift in every loop
dgtBtns[i].Click += new EventHandler(this.digitButtonClick);
digitPanel.Controls.Add(dgtBtns[i]);
}
you'll have to figure out the location parameters by trying and see
You need to define Left and Top then add the button height or width each time you loop to position your buttons correctly i.e.
int bTop=0;
int bLeft=0;
for (int i = 0; i < dgtBtns.Length; i++)
{
dgtBtns[i] = new DigitButton();
dgtBtns[i].Click += new EventHandler(this.digitButtonClick);
dgtBtns[i].Top = bTop;
bTop += dgtBtns[i].Height;
digitPanel.Controls.Add(dgtBtns[i]);
}
I am trying to create a series of RadioButtons on a WinForm. It works OK, but in the Click-event I want to capture the product ID and do stuff with that.
I am used to HTML-elements, and assigning data to a label and value for the RadioButton. With WinForms I cannot see the equivalent to the value attribute.
Any good advice on how to pass onn the product ID to the RadioButton Change-event?
var products = new Business.Products().GetAll();
if (!products.Any())
GrpCategories.Controls.Clear();
int y = 2;
int x = 2;
foreach (var product in products)
{
var btn = new RadioButton();
btn.Width = 100;
btn.Height = 20;
if (y >= GrpCategories.Height - btn.Height - 10)
{
x += btn.Width + 2;
y = 2;
}
y += btn.Height + 2;
btn.Appearance = Appearance.Button;
btn.Text = product.Name;
btn.Name = "BtnProduct_" + product.ID;
btn.Location = new Point(x, y);
GrpCategories.Controls.Add(btn);
}
Simply use the Tag-property of the RadioButton. This property can store any .NET object.
There is nothing like RadioButtonList control in winform or else it would have solved your problem like nothing.
But,then also You can group radio buttons by adding them inside a container such as a Panel control, a GroupBox control, or a form.
Drag a group panel inside your form and then add your radio buttons, this will group all the radio buttons inside that group.. as is done here.
I have a split container on panel 1 I have added a groupbox, in that groupbox is a flowcontrol which has dynamic number of textboxes, i have set both the groupbox and flowcontrol to dockstyle to fill.
In code i have also set the textboxes to dock style to fill, but they wont resize when i move the splitter, while the parent flowcontrol does resize.
Label labelInput = new Label();
TextBox listBoxNewInput = new TextBox();
listBoxNewInput.Name = ce.ToString();
labelInput.AutoSize = true;
labelInput.Font = new Font(labelInput.Font, FontStyle.Bold);
listBoxNewInput.Multiline = true;
// Add vertical scroll bars to the TextBox control.
listBoxNewInput.ScrollBars = ScrollBars.Vertical;
// Allow the RETURN key in the TextBox control.
listBoxNewInput.AcceptsReturn = true;
// Allow the TAB key to be entered in the TextBox control.
listBoxNewInput.AcceptsTab = true;
// Set WordWrap to true to allow text to wrap to the next line.
listBoxNewInput.WordWrap = true;
listBoxNewInput.Text = ts.ToString();
//listBoxNewInput.Width = 150;
listBoxNewInput.MinimumSize = new Size(200,150);
listBoxNewInput.MaximumSize = new Size(1000, 150);
listBoxNewInput.Dock = DockStyle.Fill;
listBoxNewInput.TextChanged += new EventHandler(listBoxNewInput_TextChanged);
//Add the newly created text box to the list of input text boxes
inputTextBoxesList.Add(listBoxNewInput);
//Add the labels and text box to the form
flowLayoutPanel1.Controls.Add(labelInput);
flowLayoutPanel1.Controls.Add(listBoxNewInput);
if i try to put controls directly in to spliter panel 1 only the first two controls appear, which do resize when i move the splitter
splitContainer1.Panel1.Controls.Add(labelInput); splitContainer1.Panel1.Controls.Add(listBoxNewInput);
->if the controls when i put them in flow control resize, when i move the splitter that would be good
OR
->All controls appear when i put them directly into the splitter panel 1
Based on your comments and what I think you are trying to accomplish, I think you need to replace the FlowLayoutPanel with a TableLayoutPanel because it sounds like you are just stacking one TextBox below another.
Create a TableLayoutPanel with 1 column and 1 row.
Here is a working example:
tableLayoutPanel1.AutoScroll = true;
tableLayoutPanel1.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
tableLayoutPanel1.RowStyles.Clear();
tableLayoutPanel1.RowStyles.Add(new RowStyle(SizeType.Absolute, 150));
for (int i = 0; i < 4; i++) {
AddTextBox("TextBox #" + i.ToString());
}
private void AddTextBox(string info) {
TextBox tx = new TextBox();
tx.Multiline = true;
tx.Text = info;
tx.ScrollBars = ScrollBars.Vertical;
tx.WordWrap = true;
tx.Height = 150;
tx.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;
tableLayoutPanel1.Controls.Add(tx);
}
Instead of docking, I set the height of the TextBox and then I set the Anchors so that when the SplitPanel resizes, the TextBoxes resize appropriately.