i am trying to make macrosubstitution in c#. Actually i'm trying to learn c#. I am experienced in Visual fox.
Anyway this what i need:
let's say i am trying to add a new control programmaticaly:
ProgressBar progressBar1 = new ProgressBar();
progressBar1.Size = new System.Drawing.Size(516, 23);
progressBar1.Location = new Point(10, 36);
groupBox4.Controls.Add(progressBar1);
now i need to know a way how to replace the name o f the control (progressBar1) and tke the name for an string variable so i could create using a for statement more then one progress bar
?
any ideea?
progressBar1 is not the name of the control. Is the name of a variable of type ProgressBar used to work with the current instance of a ProgressBar.
Every class derived from Control has a property called Name of string type.
int xPos = 10;
int yPos = 36;
// Add 10 progressbars to the groupbox control collection
for(x = 0; x < 10; x++)
{
yPos = yPos + (x * 30);
ProgressBar progressBar1 = new ProgressBar();
progressBar1.Name = "pgb" + x.ToString();
progressBar1.Size = new System.Drawing.Size(516, 23);
progressBar1.Location = new Point(10, yPos);
groupBox4.Controls.Add(progressBar1);
}
To retrieve one of your added progress bars you can simply use the Controls object collection of the groupbox.
ProgresBar pb = groubBox4.Controls["pgb1"] as ProgressBar;
pb.Increment(1);
You have to put the code inside a for loop to create more than one instance of the progress bar, if you want to keep a reference of the items you can store them into a Dictionary, for example :
Dictionary<int, ProgressBar> progressBars = new Dictionary<int, ProgressBar>();
for(int i = 0; i < someValue; i++) {
ProgressBar progressBar = new ProgressBar();
progressBar.Size = new System.Drawing.Size(516, 23);
progressBar.Location = new Point(10, 36);
groupBox4.Controls.Add(progressBar);
progressBars.Add(i, progressBar);
}
So, now you can get back a desired progressbar looking in the dictionary, by the index(or by something else you define as key) ...
Example of getting back the progressbar with index 3(through the indexer) :
ProgressBar bar = progressBars[3];
bar.Value = //Assign some value
Related
I'm trying to make dynamicly created radiobuttons in my dynamicly created panel, but I'm not recieving what I'm trying to accomplish.
Here is my code:
private void Form1_Load(object sender, EventArgs e)
{
//Creating 3 panels
int counTer = 3;
for (int x = 0; x <= counTer; x++)
{
Panel panel = new Panel();
panel.Name = "panel" + x;
panel.Location = new Point(10 * (5 * x), 10);
panel.Size = new Size(150, 275);
//panel.BackColor = Color.Black; <-- Only for checking if they exist
panel.Controls.Add(panel);
//Creating 10 RadioButtons
int hoeveelHeid = 10;
for (int i = 0; i <= hoeveelHeid; i++)
{
RadioButton iets= new RadioButton();
iets.Name = "Waarde" + i;
iets.Text = "Waarde " + i;
iets.Location = new Point(5, 20 * i);
panel.Controls.Add(iets);
}
}
}
I'm not recieving any panels nor radiobuttons, does anyone see the mistake i made?
Thanks.
You are trying to add the panel you created to it's OWN control collection:
panel.Controls.Add(panel);
which means add the panel to the panel.
To add the panel to the form use:
this.Controls.Add (panel);
or even just:
Controls.Add (panel);
As suggested by Sinatr, you have to add the panel to your form like that:
this.Controls.Add (panel);
Otherwise your panel does exist, but it's not on your form.
For anyone who wants to hate with the reason that I only want to gain reputation, this answer's marked as community wiki.
I have a for loop which I'm attempting to use to generate and lay out the contents of a Form.
This is what I've gotten to so far:
public void RefreshSkillDialog()
{
Point nPt = new Point(25, 25);
for (int x = 0; x < Enum.GetNames(typeof(Character.Skill)).Length; x++ )
{
GenerateFields(x,nPt);
Console.Write(this.Controls.Count + "\n");
Console.WriteLine(this.Controls[x].ToString() + "\n");
}
}
public void GenerateFields(int it, Point pt)
{
Label tLbl = new Label();
tLbl.Location = new Point(pt.X + (it * _vSpace), pt.Y);
tLbl.Name = Enum.GetName(typeof(Character.Skill), it);
tLbl.Text = this.Controls.Count.ToString();
this.Controls.Add(tLbl);
}
_vSpace is an integer initialized to 10 in the constructor.
The result of this code is:
I thought the issue was in reusing the tLbl variable, but as far as I can tell, it should work fine since I'm re-initializing it at the beginning of every iteration.
When you create a new Label, it is 100px wide by default.
You're changing the location (x-coordinate) of each new Label, but not by enough. Each new Label is overlapping the previous one, such that's it's covering up the text.
You can fix this by setting AutoSize = true on each Label:
Label tLbl = new Label();
tLbl.AutoSize = true;
...
Or by resizing it so that it's not as wide:
Label tLbl = new Label();
tLbl.Size = new Size(10, 23);
...
You might also consider just using a FlowLayoutPanel, since it will handle the layout for you. Add each new control to it, and don't bother with setting a Location.
Label tLbl = new Label();
tLbl.Name = Enum.GetName(typeof(Character.Skill), it);
tLbl.Text = flowLayoutPanel1.Controls.Count.ToString();
flowLayoutPanel1.Controls.Add(tLbl);
If you do that, you'll have to change your Console.WriteLine statement in the other method too:
Console.WriteLine(flowLayoutPanel1.Controls[x].ToString() + "\n");
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.
I am having some issues making my label show up in the gui... any thoughts?
private void addNewExcerciseButton_Click(object sender, EventArgs e)
{
int y = 305;
int x= 61;
string tempExcercise = excerciseTextBox.Text;
excerciseTextBox.Clear();
Label[] excerciseLabels = new Label[numExercises];
for (int i = 0; i < numExercises; ++i)
{
excerciseLabels[i] = new Label();
excerciseLabels[i].Text = ToString("{0}. {1}", i + 1, tempExcercise);;
excerciseLabels[i].Location = new System.Drawing.Point(x, y);
x += 10;
y += 10;
++numExercises;
}
}
thanks in advance.
numExercises is global.
You have to add each new Label to the collection of Controls contained by a visible Control (such as your Form). You're creating and setting them up, but they aren't part of the GUI yet until they're in the control hierarchy.
Add the following line after setting the location of the label:
this.Controls.Add(exerciseLabels[i]);
You need to add the label to the GUI:
this.Controls.Add(excersizeLabels[i]);
As a side note, there is no point in using an array.
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