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;
}
Related
How to get value of checkboxes (and the textbox upon change in text) in real time with form particulars that are all generated via code?
This following code produces a form upon button press, the form has checkboxes and a richtextbox. Ideally I want any interaction to have an effect, so if I paste in a grid of ones and zeros the checkboxes update, and once a checkbox gets click, the corresponding one or zero in the textarea will update (So that I can then copy the grid (matrix) out and into another program.
I know how to get the events using the visual studio GUI maker, but not from a programmatically created form like this.
private void begin_button_Click(object sender, EventArgs e)
{
// Build the child form
Form check_box = new Form();
check_box.FormBorderStyle = FormBorderStyle.FixedSingle;
// Get the values from the textboxes
int height = Convert.ToInt16(this.height_input.Text);
int width = Convert.ToInt16(this.width_input.Text);
// Set the dimensions of the form
check_box.Width = width * 15 + 40;
check_box.Height = ( height * 15 + 40 ) * 3;
// Build checkboxes for the checkbox form
CheckBox[] chk;
chk = new CheckBox[height * width];
int count = 0;
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
chk[count] = new CheckBox();
chk[count].Name = count.ToString();
chk[count].Width = 15; // because the default is 100px for text
chk[count].Height = 15;
chk[count].Location = new Point(j * 15, i * 15);
chk[count].CheckedChanged += new EventHandler(this.CheckedChanged);
check_box.Controls.Add(chk[count]);
count += 1;
//Console.WriteLine(" i: " + i + " j: " + j + " Count: " + count);
}
}
RichTextBox output_area;
output_area = new RichTextBox();
output_area.Location = new Point(chk[0].Location.X, chk[count-1].Location.Y + 30);
check_box.Controls.Add(output_area);
output_area.Text = "hello world\n1,1,1,1,1,1,1,1,1\n0,0,0,0,0,1,0,1\nthese ones and zeros are meant to update in real time!";
output_area.Width = check_box.Width - 40;
output_area.Height = check_box.Height / 2;
// Run the form
check_box.ShowDialog();
}
EDIT:
I have added the event handler and it's working, however I can't access the checkbox form, only the main form.
private void CheckedChanged(object sender, EventArgs e)
{
//throw new NotImplementedException();
CheckBox x = (CheckBox)sender;
Console.WriteLine(x);
Console.WriteLine(x.Name.ToString());
}
Have a look at the .Designer file that the form designer generates for you!
Anyway, in your loop, try something like this:
chk[count].CheckedChanged += MyFancyHandler;
And the handler itself will look just like a normal handler:
private void MyFancyHandler( object sender, EventArgs e )
{
// ...
}
Also notice that the sender argument there will contain a reference to whichever checkbox/control the event refers to.
Below code updates matrix text in the rich text box when check box check state changed.
RichTextBox output_area;
CheckBox[] chk;
Size MatrixSize;
private void begin_button_Click()
{
// Build the child form
Form check_box = new Form();
check_box.FormBorderStyle = FormBorderStyle.FixedSingle;
check_box.StartPosition = FormStartPosition.CenterScreen;
// Get the values from the textboxes
int height = Convert.ToInt16("10");
int width = Convert.ToInt16("7");
MatrixSize = new Size(width, height);
// Set the dimensions of the form
check_box.Width = width * 15 + 40;
check_box.Height = (height * 15 + 40) * 3;
// Build checkboxes for the checkbox form
chk = new CheckBox[height * width];
int count = 0;
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
{
chk[count] = new CheckBox();
chk[count].Name = count.ToString();
chk[count].Width = 15; // because the default is 100px for text
chk[count].Height = 15;
chk[count].Location = new Point(j * 15, i * 15);
check_box.Controls.Add(chk[count]);
chk[count].CheckedChanged += new EventHandler(CheckBox1_CheckedChanged);
count += 1;
//Console.WriteLine(" i: " + i + " j: " + j + " Count: " + count);
}
}
output_area = new RichTextBox();
output_area.Location = new Point(chk[0].Location.X, chk[count - 1].Location.Y + 30);
check_box.Controls.Add(output_area);
output_area.Text = "hello world\n1,1,1,1,1,1,1,1,1\n0,0,0,0,0,1,0,1\nthese ones and zeros are meant to update in real time!";
output_area.Width = check_box.Width - 40;
output_area.Height = check_box.Height / 2;
// Run the form
check_box.ShowDialog();
}
private void CheckBox1_CheckedChanged(Object sender, EventArgs e)
{
CheckBox c = (CheckBox)sender;
Debug.WriteLine(c.Name);
StringBuilder sb = new StringBuilder();
int count = 0;
for (int i = 1; i <= MatrixSize.Height; i++)
{
for (int j = 1; j <= MatrixSize.Width; j++)
{
if (chk[count].Checked)
{
sb.Append("1,");
}
else
{
sb.Append("0,");
}
count += 1;
}
sb.Append("\r\n");
}
output_area.Text = sb.ToString();
}
on startup I'm generating a lot of controls 90 to be exact and everything is working ok EXCEPT for the labels they are not being drawn or something? they are there because I can click them and they show proper ID (click event) here's the genereation code
private static bool ClientsLoaded = false;
private static WebBrowser[] Clients = new WebBrowser[45];
private static Label[] ClientLabel = new Label[45];
private static int MaximizedClient = -1;
public Form1()
{
InitializeComponent();
int WBoffsetX = 0;
int WBoffsetY = 0;
int lbloffsetX = 0;
int lbloffsetY = 0;
for (int i = 0; i < 45; i++)
{
var wb = new WebBrowser();
Clients[i] = wb;
wb.ScrollBarsEnabled = false;
wb.Height = 12;
wb.Width = 12;
wb.Location = new Point(2 + WBoffsetX, 2 + WBoffsetY);
WBoffsetX += 13;
wb.ScriptErrorsSuppressed = true;
this.Controls.Add(wb);
ClientLabel[i] = new Label();
ClientLabel[i].Name = "lbl_" + i;
ClientLabel[i].Font = new Font("Arial", 12);
ClientLabel[i].ForeColor = System.Drawing.Color.White;
ClientLabel[i].Location = new Point(12 + lbloffsetX, 450 + lbloffsetY);
lbloffsetX += 22;
ClientLabel[i].Click += new EventHandler(lbl_click);
ClientLabel[i].Text = "C" + i + ": o";
this.Controls.Add(ClientLabel[i]);
}
}
I've tried adding a button with for(45) clientlabel[i].Refresh() and it did nothing I tried changing the visibilty of them all to false and then back to true and nothing however I did find 1 thing interesting if I hide lbl_1 label 2 text will appear if I had label 2 label 3 text will appear but if I change the previous label back to visible they stay invisible textwise
I can click in a line on the form and
private void lbl_click(object sender, EventArgs e)
{
int id = -1;
var s = sender.ToString();
for(int i = 0; i<=45; i++)
{
if (s.Contains("C" + i + ":"))
{
id = i;
}
}
MessageBox.Show("Hello label, " + id);
}
will pop up the proper ids etc
does anyone know what's causing this maybe? or how to fix it
Well, I don't know what is the problem. This code works well enough and it has only marginal differences with the original(AutoSize property, explicit statement of Height and Width, and minor Location adjustment):
for (int i = 0; i < ClientLabel.Length; i++)
{
// Web browsers
WebBrowser wb = new WebBrowser()
{
ScrollBarsEnabled = false,
Height = 12,
Width = 12,
Location = new Point(2 + WBoffsetX, 2 + WBoffsetY),
ScriptErrorsSuppressed = true
};
WBoffsetX += 13;
Clients[i] = wb;
// Labels
Label label = new Label()
{
Name = "label_" + i,
Text = "Data",
AutoSize = true,
Location = new Point(50 + lbloffsetX, 50 + lbloffsetY),
Width = 100,
Height = 20,
Font = new Font("Arial", 12),
ForeColor = System.Drawing.Color.White,
};
label.Click += new EventHandler(lbl_click);
ClientLabel[i] = label;
lbloffsetX += 30;
}
this.Controls.AddRange(Clients);
this.Controls.AddRange(ClientLabel);
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.
My program is currently set up to just add one row of controls below the original. How can I make it so the user can add as many lines of controls as they would like?
private void surfaceAddButton_Click(object sender, EventArgs e)
{
//Adds new set of controls on button click
for (int i = 1; i < 2; i++)
{
ComboBox surfaceCombo2 = new ComboBox();
TextBox sideWallsText2 = new TextBox();
TextBox backWallsText2 = new TextBox();
TextBox floorText2 = new TextBox();
Label newLabel = new Label();
Label newLabel2 = new Label();
Label newLabel3 = new Label();
Absorption_Coefficients alpha = new Absorption_Coefficients(); //Adds surfaces to combobox
List<string> materialslist = alpha.listLoad();
materialslist.Sort();
surfaceCombo2.Items.AddRange(materialslist.ToArray());
surfaceCombo2.DropDownStyle = ComboBoxStyle.DropDownList;
//Sets locations and sizes of new row of options, then displays them
surfaceCombo2.Location = new Point(1, 150 * i + 30);
sideWallsText2.Location = new Point(393, 153 * i + 30);
backWallsText2.Location = new Point(561, 153 * i + 30);
floorText2.Location = new Point(711, 153 * i + 30);
newLabel.Location = new Point(321, 158 * i + 30);
newLabel2.Location = new Point(439, 158 * i + 30);
newLabel3.Location = new Point(609, 158 * i + 30);
surfaceCombo2.Width = 322;
sideWallsText2.Width = 43;
backWallsText2.Width = 43;
floorText2.Width = 43;
newLabel.Text = "Side Walls, ft²";
newLabel2.Width = 120;
newLabel2.Text = "Back/or Front Walls, ft²";
newLabel3.Text = "Floor/or Ceiling, ft²";
this.Controls.Add(surfaceCombo2);
this.Controls.Add(sideWallsText2);
this.Controls.Add(backWallsText2);
this.Controls.Add(floorText2);
this.Controls.Add(newLabel);
this.Controls.Add(newLabel2);
this.Controls.Add(newLabel3);
this.Size = new Size(769, 209 * i + 30); //Increases form to accomodate new controls
}
}
This is the form with just the one surface added. I want to make it so there is a new row underneath each time the user clicks +Surface.
for (int i = 1; i < 2; i++)
Instesd of 2 in for loop, replace with n, where n will be number gathered from some control
First step is to take your existing code and create a method that creates a row. Ideally as part of this step, you create a custom control that encapsulates much of the logic you already have now.
Second step is to take that new method and require a parameter numberOfRows, use that parameter, and default it to 1. Then make sure it still works.
Final step is to provide a method for the user to input numberOfRows and send that to your method.
There is string array contains some file location.
I am using a foreach loop, in which each loop i want to create a new radio button control.
without foreach code performs, but in loop only one control is adding.
Can anybody tell me why? and how I perform this.
Code:
string[] location =
{
#"C:\Program Files\Skype\Phone\Skype.exe",
#"C:\Program Files\iTunes\iTunes.exe",
#"C:\Program Files\Internet Explorer\iexplore.exe"
};
int i = 10;
foreach (string path in location)
{
if (File.Exists(path))
{
RadioButton rbList = new RadioButton();
rbList.AutoSize = false;
Icon icn;
icn = Icon.ExtractAssociatedIcon(path);
rbList.Image = icn.ToBitmap();
rbList.Height = 100;
rbList.Width = 50;
i = i + 30;
rbList.Location = new Point(100, i);
groupBox1.Controls.Add(rbList);
}
}
You set the height to 100 but increase the position by 30 only.
rbList.Height = 100;
...
i = i + 30;
rbList.Location = new Point(100, i);
You can decrease the height below 30:
rbList.Height = 30; //or smaller
or
increase the "i" more than 100:
i = i + 100; //or more than 100
rbList.Location = new Point(100, i);
Add
rbList.AutoSize = true;
And be sure your groupBox1 is large enough to display all your radio buttons.
Little bit of changes:
i = i + 100;
rbList.Location = new Point(100, i);
groupBox1.Controls.Add(rbList);
int space = 10;
groupBox1.Height += rbList.Height + space;
This work without alignment, alignment is yours.
int i = 10;
var radios = new[] { "", "", "" }
.Where(path => File.Exists(path))
.Select(path => new RadioButton
{
AutoSize = false,
Image = Icon.ExtractAssociatedIcon(path).ToBitmap(),
Height = 100,
Width = 50,
Location = new Point(100, (i = i + 30))
})
.ToArray();
groupBox1.Controls.AddRange(radios);