I have a TableLayoutPanel with 2 columns and 1 row. In each row is a FlowLayoutPanel in which I want to create Labels. It will have to much labels for my form and I want a scrollbar in it. I already got a scrollBar for each FlowLayoutPanel, but I would like to have
namespace Flow_layout_Panel_test1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
flowLayoutPanel1.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel1.AutoScroll = true;
flowLayoutPanel1.WrapContents = false;
flowLayoutPanel2.FlowDirection = FlowDirection.TopDown;
flowLayoutPanel2.AutoScroll = true;
flowLayoutPanel2.WrapContents = false;
//makes a scrollbar for each flowlayoutpanel
for (int x = 0; x < 50; x++) //Example Labels
{
Label lbl1 = new Label();
Label lbl2 = new Label();
lbl1.Name = "lbl_" + x;
lbl1.Text = x.ToString();
lbl2.Name = "lbl2_" + x;
lbl2.Text = x.ToString();
flowLayoutPanel1.Controls.Add(lbl1);
flowLayoutPanel2.Controls.Add(lbl2);
}
}
}
}
How it currently looks:
Related
try
{
int txtno = 10;
int Textbox_pointY = 15;
int label_pointY = 15;
int label_pointX = 10;
int Textbox_pointX = 75;
panel1.Controls.Clear();
for (int i = 0; i < txtno; i++)
{ //Lable creation
Label lbl = new Label();
panel1.Controls.Add(lbl);
lbl.Text = "Test_" + i;
lbl.Location = new Point(label_pointX, label_pointY);
label_pointY += 22;
//Text box creating
TextBox a = new TextBox();
panel1.Controls.Add(a);
a.Text = (i + 1).ToString();
a.Location = new Point(Textbox_pointX, Textbox_pointY);
//panel1.Show();
Textbox_pointY += 22;
//label_pointY += 5;
}
}
catch (Exception)
{
MessageBox.Show(e.ToString());
}
When dynamically generate label and enter code heretext box in winform c# Label overlaps over textbox .I need to keep text box closely to label text .I have added my code here .
Set AutoSize to false and specify labels' Width explicitly:
int txtno = 10;
int label_pointY = 15;
int label_pointX = 10;
int Textbox_pointX = 75;
// Don't do this: it just removes conrols from the panel,
// but does't free resources (and you have resource leakage)
// panel1.Controls.Clear();
// If you want to get rid of all controls on the panel1 (i.e. dispose them)
// do it like this:
for (int i = panel1.Controls.Count - 1; i >= 0; --i)
panel1.Controls[i].Dispose();
for (int i = 0; i < txtno; i++) {
Label lbl = new Label() {
Parent = panel1,
Text = "Test_" + i,
Location = new Point(label_pointX, label_pointY),
AutoSize = false,
Width = Textbox_pointX - label_pointX,
};
TextBox box = new TextBox() {
Parent = panel1,
Text = (i + 1).ToString(),
Location = new Point(Textbox_pointX, label_pointY)
};
label_pointY += Math.Max(box.Height, lbl.Height);
}
I have a checkedListBox in a TabControl
What I want is to create a label and NumericUpDown dynamically, when User check an item of checkedListBox it will show the new label and NumericUpDown
Then , when it Unchecked this item ,The numericUpDown will be clear (empty).
Conclusion: As many checked items , as many w've create labels and NumericUpDowns.
Please, how will I do that ??
For each checkbox item in your checkedListBox in properties switch to events and create subscriber checkBoxName_CheckStateChanged for event CheckStateChanged.
The code in the sucriber can be like this:
private void checkBox1_CheckStateChanged(object sender, EventArgs e)
{
var source = sender as CheckBox;
if (source.Checked == true)
{
this.numericUpDown1.Text = "TextWhenChecked";
this.labelAtTheNumericUpDown.Text = "TextWhenChecked";
}
else
{
this.numericUpDown1.Text = "TextWhenUnchecked";
this.label1.Text = "TextWhenUnchecked";
}
}
You fill the strings as you want. These are only examples.
To have only checkBox checked at a time look at here: https://stackoverflow.com/a/24693858/6650581.
What you need to do is creating Label and NumericUpDown manually and show it by adding to Controls collection. A TableLayoutPanel can help you arranging controls without setting Size and calculate Location manually.
Here is an example:
public class MainForm : Form
{
private CheckedListBox checkedListBox;
private TableLayoutPanel tableLayoutPanel;
public MainForm()
{
InitializeComponent();
//Fill checkedListBox and create controls
for( int i = 0; i <= 5; i++ )
{
checkedListBox.Items.Add( i.ToString() );
Label lbl = new Label()
{
Name = "lbl" + i,
Text = "Label " + i,
Visible = false
};
NumericUpDown num = new NumericUpDown()
{
Name = "num" + i,
Value = i,
Visible = false
};
tableLayoutPanel.Controls.Add( lbl, 0, i );
tableLayoutPanel.Controls.Add( num, 1, i );
}
}
private void checkedListBox_ItemCheck( object sender, ItemCheckEventArgs e )
{
if( e.NewValue == CheckState.Checked )
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = true;
tableLayoutPanel.Controls["num" + e.Index].Visible = true;
}
else
{
tableLayoutPanel.Controls["lbl" + e.Index].Visible = false;
((NumericUpDown)tableLayoutPanel.Controls["num" + e.Index]).Value = 0M;
}
}
private void InitializeComponent()
{
this.checkedListBox = new System.Windows.Forms.CheckedListBox();
this.tableLayoutPanel = new System.Windows.Forms.TableLayoutPanel();
this.SuspendLayout();
//
// checkedListBox
//
this.checkedListBox.Location = new System.Drawing.Point(8, 8);
this.checkedListBox.Name = "checkedListBox";
this.checkedListBox.Size = new System.Drawing.Size(200, 100);
this.checkedListBox.TabIndex = 1;
this.checkedListBox.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.checkedListBox_ItemCheck);
//
// tableLayoutPanel
//
this.tableLayoutPanel.AutoScroll = true;
this.tableLayoutPanel.ColumnCount = 2;
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel.Location = new System.Drawing.Point(8, 112);
this.tableLayoutPanel.Name = "tableLayoutPanel";
this.tableLayoutPanel.Size = new System.Drawing.Size(200, 100);
this.tableLayoutPanel.TabIndex = 2;
//
// MainForm
//
this.ClientSize = new System.Drawing.Size(223, 227);
this.Controls.Add(this.tableLayoutPanel);
this.Controls.Add(this.checkedListBox);
this.Name = "MainForm";
this.ResumeLayout(false);
}
}
I am trying to create a user control using a Windows Forms Panel and number of Labels with some text dynamically added to the Panel horizontally. I am trying with below code and the Labels get overridden.
public partial class AllergyBar : Panel
{
public AllergyBar()
: base()
{
InitializeComponent();
}
int X = 0, Y=0;
int height, width;
public AllergyBar(List<String> lstAlerts)
: base()
{
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.Name = "panel2";
this.Size = new System.Drawing.Size(75, 23);
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
foreach (string alert in lstAlerts)
{
Label AllergyLabel = new Label();
AllergyLabel.Text = alert;
width = AllergyLabel.Size.Width;
Y = AllergyLabel.Location.Y;
AllergyLabel.Location = new System.Drawing.Point(X+width, Y);
AllergyLabel.Size = new System.Drawing.Size(75, 23);
AllergyLabel.AutoSize = true;
AllergyLabel.BorderStyle = BorderStyle.FixedSingle;
AllergyLabel.Dock = DockStyle.Fill;
this.Controls.Add(AllergyLabel);
}
InitializeComponent();
}
}
You have to update X value at the end of each loop:
public partial class AllergyBar : Panel
{
public AllergyBar(): base()
{
InitializeComponent();
}
int X = 0, Y=0;
int height, width;
public AllergyBar(List<String> lstAlerts): base()
{
InitializeComponent();
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.Name = "panel2";
this.Size = new System.Drawing.Size(75, 23);
this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
foreach (string alert in lstAlerts)
{
Label AllergyLabel = new Label();
AllergyLabel.Text = alert;
AllergyLabel.Location = new System.Drawing.Point(X, Y);
AllergyLabel.AutoSize = true;
AllergyLabel.BorderStyle = BorderStyle.FixedSingle;
AllergyLabel.Dock = DockStyle.Fill;
X += AllergyLabel.Width;
this.Controls.Add(AllergyLabel);
}
}
}
I am trying to make a simple five in a row (gomoku) game for two players using windows forms and c#. I put a picturebox with a picture and stretched it out on the form. Now I want to put labels at all the intersections on the picture board so a user can click them and change their background color to black or white.
How can I make the labels created clickable on the form?
public partial class Form1 : Form
{
int labelCount = 0;
int iteration = 0;
public Form1()
{
InitializeComponent();
Label[] board = new Label[361];
for (int i = 0; i < 361; i++)
{
board[i] = new Label
{
Name = "label" + i,
Height = 55,
Width = 55,
MinimumSize = new Size(55, 55),
Text = "label " + i
};
}
int x = 0;
int y = 0;
foreach (var Label in board)
{
if (x >= 580)
{
x = 0;
y = y + Label.Height + 55;
}
Label.Location = new Point(x, y);
this.Controls.Add(Label);
x += Label.Width;
}
}
}
Should I make a one-dimensional [361] or two-dimensional array[{A,1}, {A,2}....{D,1}] to easily check for a winner? How can I connect it to the created labels so the array data corresponds to the objects on the board?
Well Sorry If don`t understand your question. For the Q.1 to add 361 labels you can try the code below. I hope it will help you.
public int x = 0;
public int y = 0;
private Label[] moku = new Label[361];
private void Form1_Load(object sender, EventArgs e)
{
try
{
for (int i = 0; i < 361; i++)
{
moku[i] = new Label();
moku[i].Parent = pictureBox1;//make the picturebox parent
moku[i].Location = new Point(x, y);
moku[i].Text = "O";
moku[i].Name = "moku" + i;
moku[i].BackColor = Color.Transparent;
pictureBox1.Controls.Add(moku[i]);
y += 55;
if (y >= 361) { x += 55; y = 0; x+=55; }
}
}catch(Exception er)
{
MessageBox.Show(er.ToString());
}
}
I prefer using a 2D array because it's easier if you want to check the surrounding boxes.
Form design:
Full source:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public enum Player
{
Empty = 0,
White,
Black
}
public partial class Form1 : Form
{
// initialize board of 5x5
private Player[,] board = new Player[5, 5];
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
DrawBoard();
}
private void DrawBoard()
{
for (var i = 0; i <= board.GetUpperBound(0); i++)
{
for (var j = 0; j <= board.GetUpperBound(1); j++)
{
// for name and text
var name = string.Format("{0}, {1}", i, j);
var label = new Label()
{
Name = name, // name of label
Size = new Size(55, 55),
BorderStyle = BorderStyle.FixedSingle,
Location = new Point(i * 55, j * 55), // location depends on iteration
Text = name
};
label.Click += ClickLabel; // subscribe the Click event handler
pictureBox1.Controls.Add(label); // add label to a container
}
}
}
// this event handler will handle all the labels click event
private void ClickLabel(object sender, EventArgs e)
{
var label = (Label)sender; // this is the label that you click
var x = Convert.ToInt32(label.Name.Split(',')[0]);
var y = Convert.ToInt32(label.Name.Split(',')[1]);
// change the color
if (radPlayerBlack.Checked)
{
// Player Black
label.ForeColor = Color.White;
label.BackColor = Color.Black;
board[x, y] = Player.Black;
}
else
{
// Player White
label.ForeColor = Color.Black;
label.BackColor = Color.White;
board[x, y] = Player.White;
}
}
}
}
You can check the value of the 2D array for black or white. Here's the value when I QuickWatch it in Visual Studio.
I am working on a Winform app using .net 4.5.
In my project, I have a window that contains only a button.
WHen the window is displayed, I add lots of controls dynamically to the point that a scrollbar is needed.
Once the controls are added, I move the single button to the bottom of the controls in the window.
This step of moving the button to the bottom of the controls makes the window scroll to the bottom. I have tested this by not moving the button to the bottom of the form, and the scroll stays at the top.
I have tried "this.VerticalScroll.Value = 0;" both before and after setting the position of the button.
Here is the code so you can get a clearer idea of what I am attempting to do:
public SignoffSurvey(int task_id)
{
InitializeComponent();
this.task_id = task_id;
int form_top = 10;
int question_num = 0;
XmlDocument doc = new XmlDocument();
doc.Load(DBHandler.getSetting("files_directory") + "\\" + "questionaire.xml");
foreach (XmlNode node in doc.SelectNodes("/Questions/Question"))
{
question_num++;
string type = node.Attributes["type"].Value;
int top = 0;
Panel pnl = new Panel();
pnl.AutoSize = true;
pnl.Top = form_top;
pnl.Left = 10;
Label text_lbl = new Label();
text_lbl.Top = top;
text_lbl.AutoSize = true;
text_lbl.Text = node["text"].InnerText;
pnl.Controls.Add(text_lbl);
top += text_lbl.Height + 5;
if (type == "mc" || type == "mct")
{
XmlNode choices = node["choices"];
Boolean fc = false;
foreach (XmlNode choice in choices.ChildNodes)
{
RadioButton rb = new RadioButton();
rb.AutoSize = true;
rb.Text = choice.InnerText;
rb.Top = top;
rb.Left = 10;
top += rb.Height + 5;
pnl.Controls.Add(rb);
if (!fc) // check first item.
{
fc = true;
rb.Checked = true;
}
}
}
if (type == "mct" || type == "txt")
{
TextBox tb = new TextBox();
tb.Multiline = true;
tb.Width = 500;
tb.Height = 250;
tb.Top = top;
tb.Left = 10;
pnl.Controls.Add(tb);
top += tb.Height + 5;
}
pnl.Height = top;
this.Controls.Add(pnl);
form_top += pnl.Height + 10;
}
this.VerticalScroll.Value = 0;
this.save_btn.Top = form_top;
}
HOw can I force the window vertical scroll to the top regardless of where this 1 button is moved to?
Okie. For anyone with a similar issue, here is the reason for the problem and how to overcome it.
The problem appears to be that the single button is 'selected' by default on the window. So when the window launches, it scrolls to the position of the button.
I added this:
this.Controls[1].Select();
after adding all my controls.
You have to use [1] because the button already on the form is [0].