How to create list of checkboxes with scroll? - c#

I'm trying this code:
arrList = new List<CheckBox>();
for (int j = 0; j < 20; j++)
{
CheckBox check = new CheckBox();
arrList.Add(check);
}
CheckBox[] cb = arrList.ToArray();
for (int i = 0; i < 20; i++)
{
cb[i].Text = "sometext";
cb[i].Location = new System.Drawing.Point(10, 15 + i * 20);
cb[i].BackColor = System.Drawing.Color.Silver;
cb[i].Name = "somename";
cb[i].Size = new System.Drawing.Size(59, 17);
cb[i].Checked = true;
groupBox1.Controls.Add(cb[i]);
}
How can I add a vertical scroll bar there? (maybe I should use something instead of groupbox?)

You could use a Panel control and set it's AutoScroll property to True

Related

How to get boxes true in a list of checkboxes?

I have to display a list of checkboxes (50) from a table A, in this list i have to mark as true boxes that are checked depending on elements in table B.
The goal is to have all the 50 checkboxes with those checked.
Can you guys help me please.
int envid = int.Parse(txtenvid.Text);
DataView oDvPop = PopulationBL.BigList();
DataView listPop = PopulationBL.SmallListCheckeditems(envid, 1);
CheckBoxList list = new CheckBoxList();
CheckBox check;
CheckBox check2;
for (int i = 0; i < listPop.Count; i++)
{
check = new CheckBox();
check2 = new CheckBox();
check.Text = listPop[i]["POP"].ToString();
for (int j = 0; j < oDvPop.Count; j++)
{
check2.Text = oDvPop[j]["POP_LABEL"].ToString();
if (check.Text == check2.Text)
check.Checked = true;
checkBoxes.Add(check);
chkListPop.Controls.Add(check);
}
}
chkListPop.Controls.Add(check);
First of all you are doing
CheckBox check = new CheckBox();
CheckBox check2 = new CheckBox();
outside the loop so will have the last values assign in all of checkboxes. So insatiate it inside loop.
CheckBox check;
CheckBox check2;
for (int i = 0; i < listPop.Count; i++)
{
check = new CheckBox();
check2 = new CheckBox();
}

using dynamically created labels c#

So I have
TableLayoutPanel table = new System.Windows.Forms.TableLayoutPanel();
public int d;
private void button1_Click(object sender, EventArgs e)
{
d = (int)numericUpDown1.Value;
table.ColumnCount = d;
table.RowCount = d;
this.Controls.Add(table);
int k = 1;
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
Label lab = new System.Windows.Forms.Label();
lab.Size = new Size(50, 50);
lab.Text = (k).ToString();
lab.TabIndex = k++;
lab.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(lab, j, i);
}
table.Show();
}
That is one Button Click method.
I have Button2 Click method and I want to change lab.TabIndex or/and lab.Text.
How can I do that?
And second question:
how can i do something on Click one of that labels? Let's say that i want to change a color one of the labels i click...how can I do that?
I'm a beginner so...have mercy :)
define a array of labels (global variable, like you did with table):
Label[] labels;
in button1_Click, add the line of code:
labels = new Label[d*d]; // array of d*d labels
inside the loop, define the specific label, like:
labels[i*d+j] = new System.Windows.Forms.Label();
So your loop will look like:
for (int i = 0; i < d; ++i)
for (int j = 0; j < d; j++)
{
labels[i*d+j] = new System.Windows.Forms.Label();
labels[i*d+j].Size = new Size(50, 50);
labels[i*d+j].Text = (k).ToString();
labels[i*d+j].TabIndex = k++;
labels[i*d+j].TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
table.Controls.Add(labels[i*d+j], j, i);
}
you can access them from other buttons also in the same way (labels[n])

How to set wpf grid rowspan programmatically?

How to set wpf grid rowspan programmatically? I am using mvvm pattern.
I have grid lines enabled and the below doesn't work:
RowDefinition row0 = new RowDefinition();
myGrid.RowDefinitions.Add(row0)
for (int i = 1; i <= RowsCount; i++)
{
RowDefinition row = new RowDefinition();
myGrid.RowDefinitions.Add(row);
TextBlock txt3 = new TextBlock();
txt3.Text = i.ToString();
txt3.FontSize = 12;
txt3.FontWeight = FontWeights.Bold;
Grid.SetRow(txt3, i);
myGrid.Children.Add(txt3);
}
ColumnDefinition column0 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(column0);
char c = 'A';
for (int i = 1; i <= ColumnsCount; i++)
{
ColumnDefinition column = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(column);
TextBlock txt3 = new TextBlock();
txt3.Text = c.ToString();
txt3.FontSize = 12;
txt3.FontWeight = FontWeights.Bold;
Grid.SetColumn(txt3, i);
myGrid.Children.Add(txt3);
switch (i)
{
case 1:
for (int j = 1; j <= RowsCount; j++)
{
TextBlock txt = new TextBlock();
txt.Text = ColumnAROI[j-1].ToString();
Grid.SetRow(txt, j);
Grid.SetColumn(txt, i);
Grid.SetRowSpan(txt, 2);
// Grid.SetRowSpan(txt, TubeRowSpan[j]);
myGrid.Children.Add(txt);
}
break;
}
c++;
}
for (int j = 1; j <= RowsCount; j++)
{
TextBlock txt = new TextBlock();
txt.Text = ColumnAROI[j-1].ToString();
Grid.SetRow(txt, j);
Grid.SetColumn(txt, i);
Grid.SetRowSpan(txt, TubeRowSpan[j]);
myGrid.Children.Add(txt);
}
TubeRowSpan is an ObservableCollection of type int and defined as below. It contains all 1's except at position 2 (3rd element).
private ObservableCollection<int> _TubeRowSpan = new ObservableCollection<int>();
public ObservableCollection<int> TubeRowSpan
{
get { return _TubeRowSpan; }
set
{
if (_TubeRowSpan != value)
{
_TubeRowSpan = value;
RaisePropertyChanged(() => TubeRowSpan);
}
}
}
You'll have to post the rest of your code, notably how you're creating your row definitions. The following code creates 3 rows for each element and sets the rowspan to 2, it's easy to see from the result that it's working as expected:
int numChildren = 10;
int numRows = numChildren * 3;
for (int j = 0; j < numRows; j++)
this.myGrid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(30) });
for (int j = 0; j < numChildren; j++)
{
TextBlock txt = new TextBlock();
txt.Background = Brushes.AliceBlue;
txt.Text = "Row " + Convert.ToString(j);
Grid.SetRow(txt, j*3);
Grid.SetRowSpan(txt, 2);
myGrid.Children.Add(txt);
}
I suspect there's something wrong with your RowDefinitions which is causing them to collapse to a height of 0 making it look like it's not working when in fact it is. Try replacing your entire code with mine and then replace your own parts back in bit by bit.

How to get the Row index and Columns index in the datagridView

My question is: When I click some Checkbox, how can I get the current checkbox control's index from DataGridView
Here is my snick code
dataGridView2.RowCount = 5;
dataGridView2.ColumnCount = 4;
for (int i = 0; i < dataGridView2.ColumnCount; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
box = new CheckBox();
box.Text = "MyDate";
//box.Size = new System.Drawing.Size(15, 15);
dataGridView2.Controls.Add(box);
Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
box.Left = rec.Left;
box.Top = rec.Top;
}
}
}
It looks like that you try adding pure CheckBoxes to your DataGridView without using a DataGridViewCheckBoxColumn, the solution for this approach is simple like this:
for (int i = 0; i < dataGridView2.ColumnCount; i++)
{
for (int j = 0; j < dataGridView2.RowCount; j++)
{
box = new CheckBox();
box.Text = "MyDate";
//box.Size = new System.Drawing.Size(15, 15);
dataGridView2.Controls.Add(box);
Rectangle rec = dataGridView2.GetCellDisplayRectangle(i, j, true);
box.Left = rec.Left;
box.Top = rec.Top;
//Added code
box.Tag = new Point(i,j);
box.Click += CheckBoxesClicked;
}
}
private void CheckBoxesClicked(object sender, EventArgs e){
CheckBox chb = sender as CheckBox;
if(chb.Tag != null) {
Point coord = (Point)chb.Tag;
MessageBox.Show(string.Format("Row index: {0}\nColumn index: {1}", coord.Y, coord.X);
}
}
You should use a DataGridViewCheckBoxColumn instead, with that approach, you can handle the event CellContentClick...
if you are using the CellContentClick event or any other event that you get the DataGridViewCellEventArgs then you have ColumnIndex and RowIndex properties that are the column and row of the cell changed
Check this links. This gives you details about grid view.
http://msdn.microsoft.com/en-us/library/ms972814.aspx
http://msdn.microsoft.com/en-us/library/aa479344.aspx

How to add a matrix of OvalShapes to the window?

I want to draw 16*64 matrix, each one containing a:
Microsoft.VisualBasic.PowerPacks.OvalShape.
I used this:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape =
new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
}
How can I show it in the Window?
Creating a separate container for each shape is not required. Also you can skip the additional container list for the shapes. So you could use this very compact code.
var ovalShapes = new Microsoft.VisualBasic.PowerPacks.ShapeContainer()
{
Dock = DockStyle.Fill,
Margin = new Padding(0),
Padding = new Padding(0),
};
for (int i = 0; i < 16; i++)
for (int j = 0; j < 64; j++)
ovalShapes.Shapes.Add(
new Microsoft.VisualBasic.PowerPacks.OvalShape()
{
Width = 20,
Height = 20,
FillStyle = Microsoft.VisualBasic.PowerPacks.FillStyle.Solid,
FillColor = Color.Green,
Location = new Point(20 * i, 20 * j),
});
this.Controls.Add(ovalShapes);
From MSDN:
An OvalShape control cannot be displayed directly on a form or
container control; it must be contained in a ShapeContainer object.
After you initialize an OvalShape, you will have to set its Parent
property either to an existing ShapeContainer or to a new instance of
ShapeContainer.
Try to set Location and add your controls to the Form:
List<Microsoft.VisualBasic.PowerPacks.OvalShape> ovalShape = new List<Microsoft.VisualBasic.PowerPacks.OvalShape>();
for (int i = 0; i < 16; i++)
{
for (int j = 0; j < 64; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = 20;
ovl.Height = 20;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovl.Location = new Point(ovl.Width*i, ovl.Height*j);
ovalShape.Add(ovl);
}
}
foreach(OvalShape os in ovalShape)
{
Microsoft.VisualBasic.PowerPacks.ShapeContainer shapeContainer = new Microsoft.VisualBasic.PowerPacks.ShapeContainer();
Control c = new Control();
shapeContainer.Parent = c;
os.Parent = shapeContainer;
myForm.Controls.Add(c);
}
First simplify
int totalCount = 1024; //16*64
const int shapeWidth =20;
const int shapeHeight = 20;
for (int j = 0; j < totalCount; j++)
{
OvalShape ovl = new OvalShape();
ovl.Width = shapeWidth;
ovl.Height = shapeHeight;
ovl.FillStyle = FillStyle.Solid;
ovl.FillColor = Color.Green;
ovalShape.Add(ovl);
}
After pickup your drawing area and decide how much shapes per row you would like to have.
So some hypothetical code could look like this:
int shapesPerRowCount = 5;
int yPos = 0;
for(int i=0;i<ovalShape.Count;i++)
{
if(i % shapesPerRowCount == 0) //reach end of the row, so offset Y position by Height
yPos += shapeHeight;
int xPos = i*shapeWidth;
DrawShapeAtPos(ovalShape[i], xPos, yPos); //some special function that draws the shape
}
A very generic code, but just to provide you an idea.
If it's not what you're searching for, please clarify.

Categories