Move label right to avoid over lap - c#

I have two labels next to each other. The values of these labels are changed at run time.
Now if the text of first label is long then it overlaps the second label.
What i want is the second label to shift right to avoid overlaping.
Is this possible?
Here is my code:
//
// labelName
//
this.labelName.AutoSize = true;
this.labelName.BackColor = System.Drawing.Color.Transparent;
this.labelName.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelName.ForeColor = System.Drawing.Color.White;
this.labelName.Location = new System.Drawing.Point(6, 1);
this.labelName.Name = "labelName";
this.labelName.Size = new System.Drawing.Size(93, 16);
this.labelName.TabIndex = 55;
this.labelName.Tag = "useHeaderImage Core";
this.labelName.Text = "Name";
//
// labelShareSize
//
this.labelShareSize.AutoSize = true;
this.labelShareSize.BackColor = System.Drawing.Color.Transparent;
this.labelShareSize.Font = new System.Drawing.Font("Tahoma", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Pixel, ((byte)(0)));
this.labelShareSize.ForeColor = System.Drawing.Color.White;
this.labelShareSize.Location = new System.Drawing.Point(206, 3);
this.labelShareSize.Name = "labelShareSize";
this.labelShareSize.Size = new System.Drawing.Size(46, 11);
this.labelShareSize.TabIndex = 56;
this.labelShareSize.Tag = "useHeaderImage Core";
this.labelShareSize.Text = "ShareSize";
Thanks

One approach could be to adjust the position of labelShareSize when the size of labelName changes. Here's some example code using the SizeChanged event to do this.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// attach this event handler before the text/size changes
labelName.SizeChanged += labelName_SizeChanged;
labelName.Text = "really really really really long text gets set here.........................";
}
void labelName_SizeChanged(object sender, EventArgs e)
{
AdjustLabelPosition();
}
private void AdjustLabelPosition()
{
if (labelShareSize.Left < labelName.Location.X + labelName.Width)
labelShareSize.Left = labelName.Location.X + labelName.Width;
}
}

Related

How to store certain variable values in a dynamically added control

I have a button which I added programmatically and I want to store certain unique information with every button. I have saved the Name and Text but is there a way to store another string to access later. Below is the code for my button and it's click event.
for (int i = bankaccountsDatagridview.Rows.Count - 1; i >= 0; i--)
{
string buttonName = "individualDepartmentBtn-" + i;
FontAwesome.Sharp.IconButton individualDepartmentBtn = new FontAwesome.Sharp.IconButton();
individualDepartmentBtn.BackColor = System.Drawing.Color.White;
individualDepartmentBtn.Cursor = System.Windows.Forms.Cursors.Hand;
individualDepartmentBtn.Dock = System.Windows.Forms.DockStyle.Top;
individualDepartmentBtn.FlatAppearance.BorderSize = 0;
individualDepartmentBtn.FlatAppearance.MouseDownBackColor = System.Drawing.Color.Silver;
individualDepartmentBtn.FlatAppearance.MouseOverBackColor = System.Drawing.Color.FromArgb(((int)(((byte)(224)))), ((int)(((byte)(224)))), ((int)(((byte)(224)))));
individualDepartmentBtn.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
individualDepartmentBtn.Flip = FontAwesome.Sharp.FlipOrientation.Normal;
individualDepartmentBtn.Font = new System.Drawing.Font("Roboto", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
individualDepartmentBtn.ForeColor = System.Drawing.Color.DimGray;
individualDepartmentBtn.IconChar = FontAwesome.Sharp.IconChar.None;
individualDepartmentBtn.IconColor = System.Drawing.Color.DimGray;
individualDepartmentBtn.IconSize = 25;
individualDepartmentBtn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
individualDepartmentBtn.Margin = new System.Windows.Forms.Padding(10);
individualDepartmentBtn.Padding = new System.Windows.Forms.Padding(30, 0, 0, 0);
individualDepartmentBtn.Name = bankaccountsDatagridview.Rows[i].Cells[1].Value.ToString();
individualDepartmentBtn.Rotation = 0D;
individualDepartmentBtn.Size = new System.Drawing.Size(192, 30);
individualDepartmentBtn.TabIndex = 1;
individualDepartmentBtn.Text = bankaccountsDatagridview.Rows[i].Cells[1].Value.ToString();
individualDepartmentBtn.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
individualDepartmentBtn.UseVisualStyleBackColor = false;
navigationPanel.Controls.Add(individualDepartmentBtn);
individualDepartmentBtn.MouseDown += new System.Windows.Forms.MouseEventHandler(individualBankBtnDown);
}
Click Event:
private void individualBankBtnDown(object sender, MouseEventArgs e)
{
bankTitle = ((FontAwesome.Sharp.IconButton)sender).Name.ToString();
}
You can use the Tag property. For example:
individualDepartmentBtn.Tag = "My String";
You can store any object within Tag and not only strings.
Note that you could also use the var keyword and an object initializer to make your code a little bit more readable:
var individualDepartmentBtn = new FontAwesome.Sharp.IconButton
{
BackColor = System.Drawing.Color.White,
Cursor = System.Windows.Forms.Cursors.Hand,
Dock = System.Windows.Forms.DockStyle.Top,
...
};

Make dynamically created panels in winform responsive (same size as my flowlayoutPanel) - C#

So I'm creating some panels with the following code
private void button1_Click(object sender, EventArgs e)
{
int xlocation = 5;
for (int i = 0; i < 10; i++)
{
Panel panel = new Panel();
{
panel.Name = string.Format("{0}", i);
panel.Text = string.Format(i.ToString());
panel.BackColor = Color.White;
panel.Location = new System.Drawing.Point(xlocation, 30);
panel.Width = flowLayoutPanel1.Width;
panel.Height = 50;
panel.MouseEnter += new System.EventHandler(this.panel_MouseEnter);
flowLayoutPanel1.Controls.Add(panel);
Label label = new Label();
label.Location = new System.Drawing.Point(15, 10);
label.AutoSize = true;
label.Font = new System.Drawing.Font("Calibri", 13F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
label.Text = string.Format("{0}", "GNU" + i);
panel.Controls.Add(label);
Label label10 = new Label();
label10.Location = new System.Drawing.Point(15, 35);
label10.AutoSize = true;
label10.Font = new System.Drawing.Font("Calibri", 8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
label10.ForeColor = System.Drawing.Color.FromArgb(64, 64, 64);
label10.Text = string.Format("{0}", "hest");
panel.Controls.Add(label10);
}
xlocation = xlocation + 85;
}
}
The problem is when I resize my form, my flowLayoutPanel obviously gets bigger, but the panels inside doesn't.
I have tried using the
private void Form1_Resize(object sender, EventArgs e)
{
}
But it doesn't seem to work.
Any answer is much appreciated!
Thanks!
You can either use the Anchor property and set it to Right and Left, or can dock each panel to parent control. Something like this should work:
Panel panel = new Panel();
panel.Dock = DockStyle.Top; // Docks the panel to top, and fills to the width of Parent control
panel.Anchor = (AnchorStyles.Right | AnchorStyles.Left); // Set anchor to right and left
I found a solution. I just created a list with my panels like so
List<Panel> pnls = new List<Panel>();
And just did like so to make them follow the width of my flowlayoutpanel, so you can actually use a flowlayoutpanel ;-)
private void Kontrol_Resize(object sender, EventArgs e)
{
for (int i = 0; i < pnls.Count; i++)
{
pnls[i].Width = activityData.Width - 24;
pnls[i].Height = activityData.Height / 4;
}
}

Dynamically created c# label not visible

I'm attempting to display some dynamically generated labels next to dynamically generated text boxes. The text boxes appear but the labels do not.
I've looked at several solutions and have tried to make sure I defined all the label properties. I looked at some threading related solution that seem unnecessary because i'm not changing visibility state, I would just like to pop up the labels next to the text boxes.
TextBox[] channelNames = new TextBox[numOfChannels];
GroupBox channelBox = new GroupBox();
Label[] labelNames = new Label[numOfChannels];
for (int currentChannelIndex = 0; currentChannelIndex < numOfChannels; currentChannelIndex++)
{
var txt = new TextBox();
channelNames[currentChannelIndex] = txt;
txt.Name = channelCollection[currentChannelIndex].PhysicalName;
txt.Text = "ben";
txt.Location = new Point(200, 32 + (currentChannelIndex * 28));
txt.Visible = true;
this.channelBox.Controls.Add(channelNames[currentChannelIndex]);
var lbl = new Label();
labelNames[currentChannelIndex] = lbl;
lbl.AutoSize = true;
lbl.Name = channelCollection[currentChannelIndex].PhysicalName;
lbl.Size = new Size(55, 13);
lbl.TabIndex = 69;
lbl.Text = channelCollection[currentChannelIndex].PhysicalName;
lbl.Location = new Point(175, 32 + (currentChannelIndex * 28));
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.channelBox.Controls.Add(labelNames[currentChannelIndex]);
}
Here are a few things to check:
1) You are setting "AutoSize" and "Size". Trying removing "Size"
2) The default font is likely too large for the (55,13) size.
Try explicitly setting the font to something small to see if it shows up.
Does channelCollection[currentChannelIndex].PhysicalName actually contain a non-Empty string? e.g.:
class Something
{
public string PhysicalName { get; set; }
}
private void AddLabels()
{
Something[] channelCollection = new Something[]
{
//Applying this to Label.Text makes it "invisible"
new Something() { PhysicalName = "" }
};
var currentChannelIndex = 0;
var txt = new TextBox();
txt.Name = channelCollection[currentChannelIndex].PhysicalName;
txt.Text = "ben";
txt.Location = new Point(200, 32);
txt.Visible = true;
this.Controls.Add(txt);
var lbl = new Label();
lbl.AutoSize = true;
lbl.Name = channelCollection[currentChannelIndex].PhysicalName;
lbl.Size = new Size(55, 13);
lbl.TabIndex = 69;
lbl.Text = channelCollection[currentChannelIndex].PhysicalName;
lbl.Location = new Point(175, 32);
lbl.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.Controls.Add(lbl);
}
I actually had an exception that downstream of this code block that was causing the issue. I assumed that i would see the labels since the exception was thrown after the call to the label. Thanks for your suggestions.

How to add control to a panel during runtime?

I want to add some buttons during run time to a existing panel in my form so that I can scroll them by my panel when the number of the buttons increases and they do not fit the form.
I use the this.panel1.Controls.Add(this.button50[number]); but it does not show the button on the panel
How can I show the control on the panel after adding it?
Here is my code:
static int number = 49;
static int size = 163;
public void initialize()
{
this.button50= new System.Windows.Forms.Button[50];
}
public void makeButton()
{
if (number > 0)
{
this.button50[number] = new System.Windows.Forms.Button();
this.button50[number].Font = new System.Drawing.Font("Nazanin"
,12F,System.Drawing.FontStyle.Bold,
System.Drawing.GraphicsUnit.Point, ((byte)(178)));
size = 41 + size;
this.button50[number].Location = new System.Drawing.Point(194, size);
this.button50[number].Name = "buttonPlus"+number;
this.button50[number].Size = new System.Drawing.Size(151, 34);
this.button50[number].TabIndex = 24;
this.button50[number].Text = "انتخاب دستی دروس";
this.button50[number].UseVisualStyleBackColor = true;
this.panel1.Controls.Add(this.button50[number]);
number--;
}
}
//// a part of InitializeComponent() function
//
// panel1
//
this.panel1.Controls.Add(this.button50[number]);
this.panel1.AutoScroll = true;
this.panel1.Controls.Add(this.button10);
this.panel1.Controls.Add(this.button3);
this.panel1.Controls.Add(this.button8);
this.panel1.Controls.Add(this.button9);
this.panel1.Controls.Add(this.button5);
this.panel1.Controls.Add(this.button7);
this.panel1.Controls.Add(this.button6);
this.panel1.Controls.Add(this.label2);
this.panel1.Controls.Add(this.button4);
this.panel1.Controls.Add(this.button2);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.bindingNavigator1);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.lable4);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(549, 306);
this.panel1.TabIndex = 0;

How do I make a Control Array in C# 2010.NET?

I recently moved from Visual Basic 6 to C# 2010 .NET.
In Visual Basic 6 there was an option to put how many control arrays you would like to use by changing the "index" on it.
I am wondering if this is possible in C#, if so how would I go about doing it with a class like:
func fc = new func();
But with more than just one array in fc, is this possible?
And to be more clarified,
Visual Basic 6 when you load a control like a text box or user control it has in the properties window a option for "Index" and if you change that to 0, 1, etc... it'll allow you to use all of those indexes, without loading multiple controls 50 times.
I think it might have something to do with an arraylist but I'm not entirely sure.
Thanks for any help.
That code snippet isn't going to get you very far. Creating a control array is no problem, just initialize it in the form constructor. You can then expose it as a property, although that's generally a bad idea since you don't want to expose implementation details. Something like this:
public partial class Form1 : Form {
private TextBox[] textBoxes;
public Form1() {
InitializeComponent();
textBoxes = new TextBox[] { textBox1, textBox2, textBox3 };
}
public ICollection<TextBox> TextBoxes {
get { return textBoxes; }
}
}
Which then lets you write:
var form = new Form1();
form.TextBoxes[0].Text = "hello";
form.Show();
But don't, let the form manage its own text boxes.
In .NET you would create an array of controls, then you would instance a TextBox control for each element of the array, setting the properties of the control and positioning it on the form:
TextBox[] txtArray = new TextBox[500];
for (int i = 0; i < txtArray.length; i++)
{
// instance the control
txtArray[i] = new TextBox();
// set some initial properties
txtArray[i].Name = "txt" + i.ToString();
txtArray[i].Text = "";
// add to form
Form1.Controls.Add(txtArray[i]);
txtArray[i].Parent = Form1;
// set position and size
txtArray[i].Location = new Point(50, 50);
txtArray[i].Size = new Size(200, 25);
}
.
.
.
Form1.txt1.text = "Hello World!";
Unless your layout is more simplistic (i.e. rows and columns of textboxes) you may find using the designer to be easier, less time consuming and more maintainable.
Not exactly like VB6, but it is quite easy to write the code your self in c#.
If you create a control, like a Button in the designer you can copy the code from the *.Designer.cs file
It typically looks like this
private System.Windows.Forms.Button button1;
...
this.button1.Location = new System.Drawing.Point(40, 294);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 14;
this.button1.Text = "Button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
...
this.Controls.Add(this.button1);
Cut that code out and paste in a method instead, returning the Button
private Button CreateButton()
{
private System.Windows.Forms.Button button1;
this.button1.Location = new System.Drawing.Point(40, 294); // <-- change location for each
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 14; // <-- increase tab index or remove this line
this.button1.Text = "Button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.Controls.Add(this.button1);
return button;
}
then call this method like this
List<Button> buttons = new List<Button>();
for(int i = 0; i < 10; i++)
{
buttons.Add(CreateButton());
}
Read this transition guide from MS: http://msdn.microsoft.com/en-us/library/kxt4418a%28v=vs.80%29.aspx
Based on Trigo's template:
Here an example handling 2 dimensional textBox array
panel1 has to be created with the designer
(I had Autoscroll=true, Size=858;525)
public partial class Form1 : Form
{
TextBox[,] txtBoxArray = new TextBox[2,100];
public Form1()
{
InitializeComponent();
for (int i = 0; i < txtBoxArray.GetLength(0); i++)
{
for (int j = 0; j < txtBoxArray.GetLength(1); j++)
{
// instance the control
txtBoxArray[i, j] = new TextBox();
// set some initial properties
txtBoxArray[i, j].Name = "txtBox_" + i.ToString() + "_" + j.ToString();
txtBoxArray[i, j].Text = txtBoxArray[i, j].Name; //"";
// add to form
this.Controls.Add(txtBoxArray[i,j]);
txtBoxArray[i, j].Parent = panel1;
// set position and size
txtBoxArray[i, j].Location = new Point(50+i*333, 50 + j * 25);
txtBoxArray[i, j].Size = new Size(200, 25);
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
//...
}

Categories

Resources