Is it possible to let string same width? - c#

I use Winform, and I have many Label line up as ListView.
The Label have three contents in a row, I hope I can have some method to let each content same width.
I have tried PadRight, but it isn't really solve the problem, if the content is Full-width, then calculate the string.length isn't useful.
My Label is dynamically created, like this:
int lineheight=24;
for(int i=0; i<40;i++){
Label test = new Label();
test.Name = "test" + i;
test.Text = padLength(str1, 16) + padLength(str2, 27) + padLength(str3, 10); // str1~str3 is data that user input in another user control
test.AutoSize = false;
test.Top = 10 + lineheight * i;
test.Left = lineheight;
test.Width = panel1.Width - lineheight;
test.Height = lineheight;
test.Font = new System.Drawing.Font("Arial", 14F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
panel1.Controls.Add(test);
}
and the function padLength is what I need to let the string be same width, like:
private string padLength(string str,int len)
{
string tmp = str;
for (int i = 0; i < len; i++)
{
if (tmp.Width != len)
{
tmp = tmp + " ";
}
}
return tmp;
}
Because I need to move the control( by a custom slide bar), I only use one Label to present the three contents( I have tried use three Label for each other, and the result is lower the speed of control moving).
I know there is a way to measure the string width use MeasureString(), but I'm not sure how to use it( because it use the Graphic class)......
Any advice appreciate.

Related

C# remove last dynamically created textbox

Almost totally noob here. Im stucked. I know how to delete all dc textboxes but have no idea how to delete only last created one. Thx
piece of my code:
int B = 1;
public void NovIntRazd()
{
TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Top = B * 30;
txt.Left = 26;
txt.Height = 20;
txt.Width = 65;
txt.Name = "txtIntRazd" + this.B.ToString();
B++;
}
If you only need to ever remove one textbox, you can just keep the reference to the last one:
int B = 1;
TextBox txt;
public void NovIntRazd()
{
txt = new TextBox();
this.Controls.Add(txt);
txt.Top = B * 30;
txt.Left = 26;
txt.Height = 20;
txt.Width = 65;
txt.Name = "txtIntRazd" + this.B.ToString();
B++;
}
public void RemoveLast()
{
if (txt == null) return;
Controls.Remove(txt);
txt = null;
}
Alternatively, a good practice is to put dynamically created controls in their own container, like a panel. It sounds like your case is a perfect use for a FlowLayoutPanel, which will handle control positioning too. When you remove any control, all the others are automatically moved around to the right places.
Then to remove the last added control, you can just do
if (pnl.Controls.Count > 0) pnl.Controls.RemoveAt(pnl.Controls.Count - 1);
If you really want to keep the controls mixed up in the top-level form, you can easily organize the dynamic textboxes by using a Stack:
Stack<TextBox> textboxes = new Stack<TextBox>();
public void NovIntRazd()
{
var txt = new TextBox()
{
Top = (textboxes.Count + 1) * 30,
Left = 26,
Height = 20,
Width = 65,
Name = "txtIntRazd" + (textboxes.Count + 1).ToString()
};
this.Controls.Add(txt);
textboxes.Push(txt);
}
public void RemoveLast()
{
if (textboxes.Count == 0) return;
Controls.Remove(textboxes.Pop());
}

How to create many labels and textboxes dynamically depending on the value of an integer variable?

Is there any way to dynamically create and display 'n' Labels with 'n' corresponding Textboxs when we know value of 'n' after for example, clicking "Display" button.
Let me know if anything make you don't understand my question. Thank you!
I am working with VS C# Express 2010 Windows Form.
I would create a user control which holds a Label and a Text Box in it and simply create instances of that user control 'n' times. If you want to know a better way to do it and use properties to get access to the values of Label and Text Box from the user control, please let me know.
Simple way to do it would be:
int n = 4; // Or whatever value - n has to be global so that the event handler can access it
private void btnDisplay_Click(object sender, EventArgs e)
{
TextBox[] textBoxes = new TextBox[n];
Label[] labels = new Label[n];
for (int i = 0; i < n; i++)
{
textBoxes[i] = new TextBox();
// Here you can modify the value of the textbox which is at textBoxes[i]
labels[i] = new Label();
// Here you can modify the value of the label which is at labels[i]
}
// This adds the controls to the form (you will need to specify thier co-ordinates etc. first)
for (int i = 0; i < n; i++)
{
this.Controls.Add(textBoxes[i]);
this.Controls.Add(labels[i]);
}
}
The code above assumes that you have a button btnDisplay and it has a onClick event assigned to btnDisplay_Click event handler. You also need to know the value of n and need a way of figuring out where to place all controls. Controls should have a width and height specified as well.
To do it using a User Control simply do this.
Okay, first of all go and create a new user control and put a text box and label in it.
Lets say they are called txtSomeTextBox and lblSomeLabel. In the code behind add this code:
public string GetTextBoxValue()
{
return this.txtSomeTextBox.Text;
}
public string GetLabelValue()
{
return this.lblSomeLabel.Text;
}
public void SetTextBoxValue(string newText)
{
this.txtSomeTextBox.Text = newText;
}
public void SetLabelValue(string newText)
{
this.lblSomeLabel.Text = newText;
}
Now the code to generate the user control will look like this (MyUserControl is the name you have give to your user control):
private void btnDisplay_Click(object sender, EventArgs e)
{
MyUserControl[] controls = new MyUserControl[n];
for (int i = 0; i < n; i++)
{
controls[i] = new MyUserControl();
controls[i].setTextBoxValue("some value to display in text");
controls[i].setLabelValue("some value to display in label");
// Now if you write controls[i].getTextBoxValue() it will return "some value to display in text" and controls[i].getLabelValue() will return "some value to display in label". These value will also be displayed in the user control.
}
// This adds the controls to the form (you will need to specify thier co-ordinates etc. first)
for (int i = 0; i < n; i++)
{
this.Controls.Add(controls[i]);
}
}
Of course you can create more methods in the usercontrol to access properties and set them. Or simply if you have to access a lot, just put in these two variables and you can access the textbox and label directly:
public TextBox myTextBox;
public Label myLabel;
In the constructor of the user control do this:
myTextBox = this.txtSomeTextBox;
myLabel = this.lblSomeLabel;
Then in your program if you want to modify the text value of either just do this.
control[i].myTextBox.Text = "some random text"; // Same applies to myLabel
Hope it helped :)
Here is a simple example that should let you keep going add somethink that would act as a placeholder to your winform can be TableLayoutPanel
and then just add controls to it
for ( int i = 0; i < COUNT; i++ ) {
Label lblTitle = new Label();
lblTitle.Text = i+"Your Text";
youlayOut.Controls.Add( lblTitle, 0, i );
TextBox txtValue = new TextBox();
youlayOut.Controls.Add( txtValue, 2, i );
}
Suppose you have a button that when pressed sets n to 5, you could then generate labels and textboxes on your form like so.
var n = 5;
for (int i = 0; i < n; i++)
{
//Create label
Label label = new Label();
label.Text = String.Format("Label {0}", i);
//Position label on screen
label.Left = 10;
label.Top = (i + 1) * 20;
//Create textbox
TextBox textBox = new TextBox();
//Position textbox on screen
textBox.Left = 120;
textBox.Top = (i + 1) * 20;
//Add controls to form
this.Controls.Add(label);
this.Controls.Add(textBox);
}
This will not only add them to the form but position them decently as well.
You can try this:
int cleft = 1;
intaleft = 1;
private void button2_Click(object sender, EventArgs e)
{
TextBox txt = new TextBox();
this.Controls.Add(txt);
txt.Top = cleft * 40;
txt.Size = new Size(200, 16);
txt.Left = 150;
cleft = cleft + 1;
Label lbl = new Label();
this.Controls.Add(lbl);
lbl.Top = aleft * 40;
lbl.Size = new Size(100, 16);
lbl.ForeColor = Color.Blue;
lbl.Text = "BoxNo/CardNo";
lbl.Left = 70;
aleft = aleft + 1;
return;
}
private void btd_Click(object sender, EventArgs e)
{
//Here you Delete Text Box One By One(int ix for Text Box)
for (int ix = this.Controls.Count - 2; ix >= 0; ix--)
//Here you Delete Lable One By One(int ix for Lable)
for (int x = this.Controls.Count - 2; x >= 0; x--)
{
if (this.Controls[ix] is TextBox)
this.Controls[ix].Dispose();
if (this.Controls[x] is Label)
this.Controls[x].Dispose();
return;
}
}

Adding graphics to components created at runtime

For anybody that can help me out here I'd be very grateful!
I've got a very small app which creates several panels at runtime with a for LOOP. At the moment the number of panels to be created is derived from a value entered in a textbox, but will ultimately be determined by an integer read from a database
Each panel has a Label which is created in the same loop
My problem is that I want to draw 120 lines in each panel as it is created (in each iteration of the FOR loop), and I'm doing this with a nested WHILE loop
I can get everything to work fine, the panels are creating along with the Label titles, but for some reason I can't get the lines to draw
It's all in one method for testing, can anybody help me?
The code I currently have is as follows:
public void CreatePanels()
{
int PanelPosX = 50;
int PanelPosY = 500;
int LabelPosX = 10;
int LabelPosY = 10;
for (int i = 0; i < (Convert.ToInt32(textBox2.Text)); i++)
{
Panel pnlOverview = new Panel();
pnlOverview.Name = "InspectorPanel" + i.ToString();
pnlOverview.Text = "Inspector Panel " + i.ToString();
pnlOverview.Location = new Point(PanelPosX, PanelPosY);
pnlOverview.Size = new Size(974, 136);
pnlOverview.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
Controls.Add(pnlOverview);
PanelPosY += 170;
Label lblInspectorName = new Label();
lblInspectorName.Name = "InspectorName" + i.ToString();
lblInspectorName.Text = " Inspector " + i.ToString();
lblInspectorName.Width = 100;
lblInspectorName.Height = 13;
lblInspectorName.Location = new Point(LabelPosX, LabelPosY);
lblInspectorName.Size = new Size(974, 136);
pnlOverview.Controls.Add(lblInspectorName);
// Draw the Overview Chart
int x = 10;
int y = 0;
Pen OVTable = new Pen(Color.Black, 0);
while (y < 120)
{
Graphics mp = pnlOverview.CreateGraphics();
mp.DrawLine(OVTable, x, 40, x, 100);
y++;
x += 8;
}
}
return;
}
Thanks
Ivan
You might need to create an empty Image and draw into it and then add it to the Panel. The other option would be to Derive from Panel and override the OnPaint method, at which point you would draw your chart.
Perhaps there is a C# Chart component that would effect this for you.
Here's a link to a MSDN reference on rendering custom controls, which is what you are pursuing.

Multiple labels

I want to use multiple labels in my form. I am using the following code:
Label[] lblLeftUp = new Label[12];
for (int i = 0; i < 12; i++)
{
lblLeftUp[i] = new Label();
lblLeftUp[i].Location = new Point((100 + (20 * i)), 100);
lblLeftUp[i].Text = Convert.ToString(i + 1);
this.Controls.Add(lblLeftUp[i]);
}
however, I can see only one label. any idea to fix that?
The labels are blocking eachother. So try this:
lblLeftUp[i].AutoSize = true;
This happen because of lblLeftUp[i].Location = new Point((100 + (20 * i)), 100);.
How you can see, you set the wrong location for the following labels. Infact 20 isn't enough. So my recommendation is to set the location dipending on the size of the labels. So if you want the labels oredered in horizontal. Try this:
Label[] lblLeftUp = new Label[12];
int PointX = 100; //100 is the initial distance from the left border of the control
for (int i = 0; i < 12; i++)
{
lblLeftUp[i] = new Label();
lblLeftUp[i].Location = new Point(PointX, 100);
lblLeftUp[i].Text = Convert.ToString(i + 1);
this.Controls.Add(lblLeftUp[i]);
PointX += lblLeftUp[i].Width;
}

Dynamic creation of control

for (int i = 0; i < 200; i++)
{
Control control = new Control();
control = new CheckBox();
Size size = control.Size;
Point point = new Point(20, 22);
control.Location = point;
int width = size.Width + 5;
i += width;
list.Add(control);
}
foreach(Control c in list)
{
}
how do I create a new instance of checkbox? Because this way I am getting just one checkbox each time. I want to get three checkbox in each row.
Is this winforms? A first point: you don't need the new Control() each time (you simly discard it anyway when you new CheckBox(). How exactly do you want the layout to appear? Can you describe it a bit more please?
I imagine TableLayoutPanel might be a reasonable start...
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Form form = new Form();
TableLayoutPanel layout = new TableLayoutPanel();
layout.Dock = DockStyle.Fill;
form.Controls.Add(layout);
layout.AutoScroll = true;
layout.ColumnCount = 3;
// size the columns (choice just to show options, not to be pretty)
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 200));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
layout.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
for (int i = 0; i < 200; i++)
{
CheckBox chk = new CheckBox();
chk.Text = "item " + i;
layout.Controls.Add(chk);
}
Application.Run(form);
}
Otherwise, you'll need to manually set the Location (or Top and Left) of each; not simple.
Your code has problems. Let's work from sample code rather than a lesson. I'll create a Panel first, nice if you want to remove the checkboxes you created. You'd probably be interested in the user clicking a checkbox so lets add an event for that. Start a new WF project and drop a button on the form. Double click it, then paste this code:
private void button1_Click(object sender, EventArgs e) {
// Give the 3 checkboxes a decent spacing
int height = this.Font.Height * 3 / 2;
// Create the panel first, add it to the form
Panel pnl = new Panel();
pnl.Size = new Size(100, 3 * height);
pnl.Location = new Point(10, 5);
this.Controls.Add(pnl);
// Make three checkboxes now
for (int ix = 0; ix < 3; ++ix) {
CheckBox box = new CheckBox();
box.Size = new Size(100, height);
// As pointed out, avoid overlapping them
box.Location = new Point(0, ix * height);
box.Text = "Option #" + (ix + 1).ToString();
box.Tag = ix;
// We want to know when the user checked it
box.CheckedChanged += new EventHandler(box_CheckedChanged);
// The panel is the container
pnl.Controls.Add(box);
}
}
void box_CheckedChanged(object sender, EventArgs e) {
// "sender" tells you which checkbox was checked
CheckBox box = sender as CheckBox;
// I used the Tag property to store contextual info, just the index here
int index = (int)box.Tag;
// Do something more interesting here...
if (box.Checked) {
MessageBox.Show(string.Format("You checked option #{0}", index + 1));
}
}
It looks like you get your 200 instances, all placed at the same point.
Instantiate 3 new checkboxes inside your loop body, set their properties accordingly and add each of them to the list. After the code above is complete, you will have 600 checkboxes.
list.Add(Control1);
list.Add(Control2);
list.Add(Control3);
I am not sure about what you are trying to do, but I cleaned up your code a bit:
for (int i = 0; i < 200; i++)
{
Control control = new CheckBox();
control.Location = new Point(20, 22);
i += control.Size.Width + 5;
list.Add(control);
}
You should not add a new instance to the list if you want to add the control you just made.
Also:
Control control = new Control();
control = new CheckBox();
Is a bit redundant. Also to not get one control at the same spot multiple times you should alter the point. Hope this helps

Categories