Strange drawing behavior with labels - c#

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);

Related

Dynamic button text align to Left

I need to align button's text, horizontally left on C# WinForms application. All the buttons create dynamically. I tried few ways as bellow. But not worked.
private void dynBtnClick(string btnText) //Dynamic Text List In flp2
{
string[] listData = File.ReadAllLines(filePath + #"\" + btnText + ".txt");
int e=listData.Count();
flp2.FlowDirection = FlowDirection.LeftToRight;
flp2.Controls.Clear();
for ( int j =0; j<e;j++)
{
Guna.UI2.WinForms.Guna2Button iBtn = new Guna.UI2.WinForms.Guna2Button();
iBtn.Tag = j;
iBtn.BorderRadius = 5;
iBtn.Height = 25;
iBtn.Width = 200;
iBtn.AutoSize = false;
iBtn.Animated = true;
iBtn.FillColor = ColorTranslator.FromHtml("#263238");
iBtn.TextAlign = ContentAlignment.MiddleLeft;
string[] btnFacetext = listData[j].Split('~');
iBtn.Text = btnFacetext[0].ToString();
iBtn.Click += (s, c) => { flp2Action(btnFacetext[0]+"~"+ btnFacetext[1], btnText); };
flp2.Controls.Add(iBtn);
}
}
Not working for me.
iBtn.TextAlign = Left;
iBtn.TextAlign = ContentAlignment.MiddleLeft;
Here is the current button state (marked by red lines). Need to texts align left. Please help on this.
Guna.UI2.WinForms.Guna2Button.TextAlign is of type HorizontalAlignment not ContentAlignment
iBtn.TextAlign = HorizontalAlignment.Left;

How to create event from control's that not yet create?

My program is the create datagridview program that user can create dynamic columns like row,column,panel(panel is quantity of the panel) so user can mark it too,
as I know I can mark the cell with CurrentCell.Style.BackColor
when I generate datagridview I have assign name of it But !!!! it cant use the new datagridvieweventhandler command so I cant do any thing with each datagridview
so this is my Datagridview Generate Code
string[] Panelname = { "One","Two","Three","Four","Five"};
for(i=0;i<Panelname.length;i++){
Generate(Panelname[i],a,b)}
DataGridView generate(string name,int columns,int rows)
{
int i;
Control Gen;
Control LB;
LB = new Label();
LB.Text = "Panel : "+name;
LB.Location = new Point(50 + 120 / (c - 1) + 900 / c , 315);
LB.BackColor = Color.Silver;
Gen = new DataGridView();
Gen.Name = name.ToString();
Gen.Size = new Size(900/c,300 );
Gen.Location = new Point(120 / (c ) + 900 / c, 0);
DataGridView CH = (DataGridView)Gen;
CH.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
CH.CellClick += new DataGridViewCellEventHandler(CH_CellClick);
CH.Location = new Point(0+locate, 0);
for (i = 1; i <= columns; i++)
{
CH.Columns.Add("", "");
}
for (i = 1; i < rows; i++)
{
CH.Rows.Add("", "");
}
dataGridView1.Controls.Add(LB);
dataGridView1.Controls.Add(CH);
return null;
}
How can I create the event handler for each datagridview that I'm create it dynamicly ?
thankyou for your kind
Create your datagridview.
for (int i = 0; i < 10; i++)
{
DataGridView d = new DataGridView();
d.MouseClick += dataGridView_MouseClick;
}
Use the add handler method.
private void dataGridView_MouseClick(object sender, MouseEventArgs e)
{
// Use sender to determine which datagridview fired the event
}
The problem that I found is when I'm create datagridview in the datagridview it hard to define it what's datagridview that you're clicking so I have stuck in this problem for a while
And now I found out the way's to through out my problem now, here it is
for(i=0;i
DataGridView generate2(string name, int columns, int rows,int form)
{
Control Gen;
Control LB;
int x = 1;
int runcolumn = columns;
int runrow = rows;
int count=0;
LB = new Label();
LB.Text = "Panel : " + name;
LB.Location = new Point(50 + 120 / (c - 1) + 900 / c, 320);
LB.BackColor = Color.Silver;
Gen = new DataGridView();
Gen.Name = name.ToString();
Gen.Location = new Point(120 / (c) + 900 / c, 0);
DataGridView CH = (DataGridView)Gen;
CH.RowTemplate.Height = 290 / rows;
CH.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
CH.Size = new Size(900 / c, 300);
CH.RowHeadersWidth = 10;
CH.ColumnHeadersHeight = 10;
CH.Location = new Point(0 + locate, 0);
And********* CH.Click += new EventHandler(control_click);********* this is my hero's
private void control_click(object sender, EventArgs e)
{
if (sender is DataGridView)
{
DataGridView A = (DataGridView)sender;
textBox2.Text = A.CurrentCell.RowIndex.ToString();
textBox1.Text = A.CurrentCell.ColumnIndex.ToString();
textBox3.Text = A.Name.ToString();
}
}
in the send control click function you can find what's kind of your control and cast it , so whatever control that you click it you can set it's function now!

Get real time input from programmatically generated elements in C# form

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();
}

Adding controls from foreach loop

I am trying to add controls to a panel from a foreach loop.
When i press the button i want every element from a array to show as checkbox. This is wordking fine, then i want a numeric updown behind the checkbox so users can select a value.
The code for creating the checkboxes works just fine, for every item in my array it displays e checkbox. But it only shows 1 NumericUpDown.
Can anybody tell me why it only shows 1 numeric updown, while it shows all of the checkboxes?
Here is my code:
private void bierButton_Click(object sender, EventArgs e)
{
int height = 1;
int padding = 10;
int i = 0;
int x = 0;
CheckBox[] chk = new CheckBox[10];
NumericUpDown[] nmr = new NumericUpDown[10];
orderBox.Clear();
hideBtn();
foreach (string bieren in Drinks.bier)
{
chk[i] = new CheckBox();
nmr[i] = new NumericUpDown();
chk[i].Name = i.ToString();
chk[i].Text = Drinks.bier[i];
chk[i].TabIndex = i;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(10, 0 + padding + height, 200, 22);
panel1.Controls.Add(chk[i]);
testPanel.Controls.Add(nmr[i]);
height += 22;
i++;
}
}
It appears you are not updating the position of your NumericUpDown controls.
All of them are there, they are just on top of one another.
Consider this change:
private void bierButton_Click(object sender, EventArgs e)
{
int height = 1;
int padding = 10;
int i = 0;
int x = 0;
CheckBox[] chk = new CheckBox[10];
NumericUpDown[] nmr = new NumericUpDown[10];
orderBox.Clear();
hideBtn();
foreach (string bieren in Drinks.bier)
{
chk[i] = new CheckBox();
nmr[i] = new NumericUpDown();
chk[i].Name = i.ToString();
chk[i].Text = bieren; // Drinks.bier[i];
chk[i].TabIndex = i;
chk[i].AutoCheck = true;
chk[i].Bounds = new Rectangle(10, 0 + padding + height, 200, 22);
// Start New Code
nmr[i].Bounds = new Rectangle(10, 0 + padding + height, 200, 22);
// End New Code
panel1.Controls.Add(chk[i]);
testPanel.Controls.Add(nmr[i]);
height += 22;
i++;
}
}
I also changed one line to chk[i].Text = bieren;.

Create Control in foreach loop?

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);

Categories