i have an array label name title[i], and link[i].
Label[] title = new Label[100];
Label[] link = new Label[100];
for (int i = 0; i < 10; i++)
{
title[i] = new Label();
link[i] = new Label();
}
when i click the label title, i can get the link label information too.
title[i].MouseClick += new EventHandler(hover_title);
i try this code doesnt work.
public void hover_title(object sender, EventArgs e)
{
title[i].text=link[i].text;
}
how i can get the label link text when i click the title label.
Something like following should solve your problem.
public void hover_title(object sender, EventArgs e)
{
var label = sender as Label;
int i = (title as IList).IndexOf(label);
label.Text = link[i].Text;
}
And remember, after you create a control you must give it a new location, in case of Label set a text, a new size (in case of Label you can set AutoSize property to true), and add it to parent control's Controls collection.
You can do this:
Label[] title = new Label[100];
Label[] link = new Label[100];
for (int i = 0; i < 10; i++)
{
var j = i;
title[j] = new Label();
link[j] = new Label();
title[j].MouseClick += (s, e) => title[j].Text = link[j].Text;
}
Related
Say if I want to create a multiple button on my form based on a loop value of 3 then 3 buttons should be created into that form, in my case I have this textbox input that should determine the loop value on button click. What I've tried:
void Button4Click(object sender, EventArgs e)
{
int get_col_range = Convert.ToInt32(textBox3.Text);
for(int i=0; i<get_col_range; i++) {
Button btn = new Button();
btn.Text = Convert.ToString(i);
this.Controls.Add(btn);
}
}
I put on value of 2 on the TextBox input as test value but turned out that nothing happened why?.
Try the following on a form with no controls in a button click event.
int top = 10;
int heightPadding = 30;
for (int index = 0; index < 3; index++)
{
var button = new Button()
{
Text = $"Button {index}",
Name = $"Button{index}",
Location = new Point(10, top)
};
Controls.Add(button);
top += heightPadding;
}
I am unable to get the controls count on the newly created form on the newly created button, I have created 5 controls but only one is showing. If I cannot get the total control count then I cannot also get the control type, name etc.
private void button2_Click(object sender, EventArgs e)
{
Form frm = new Form();
frm.Text = "new form";
TableLayoutPanel tlp = new TableLayoutPanel();
tlp.AutoSize = true;
Button btn = new Button();
btn.Text = "ok";
tlp.Controls.Add(btn, 0, 4);
frm.Controls.Add(tlp);
for (int i = 3, ii = 0; i >= 0; i--, ii++)
{
TextBox tbx = new TextBox();
tlp.Controls.Add(tbx, 0, ii);
}
frm.Show();
string str = frm.Controls.Count.ToString();
btn.Click += (s, args) =>
{
MessageBox.Show(frm.Text);
MessageBox.Show(ActiveForm.Text);
MessageBox.Show(str);
};
}
In your code the only Control that you've added to your form is a TableLayoutPanel that contains TextBox controls. That is why the count is 1.
I want to get all items from CheckedBoxList and add it to a new Form so for every checked item I want a new Label with checkedItem name and a TextBox. So far I'm doing that but when I open the form I got no results at all. I don't know how to get the checked item name and I'm doing this:
labels[i].Text = i.ToString();
private void Button4_Click(object sender, EventArgs e)
{
testForm = new Test();
TableLayoutPanel tableLayoutPanel = new TableLayoutPanel() { AutoSize = true };
tableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
int n = 0;
for (int i=0;i<checkedListBox1.CheckedIndices.Count;i++)
{
txtBox = new TextBox[checkedListBox1.CheckedIndices.Count];
labels = new Label[checkedListBox1.CheckedIndices.Count];
labels[i] = new Label();
labels[i].Text = i.ToString();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(labels[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(labels[i]);
txtBox[i] = new TextBox();
tableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
tableLayoutPanel.SetCellPosition(txtBox[i], new TableLayoutPanelCellPosition(0, n++));
tableLayoutPanel.Controls.Add(txtBox[i]);
}
Controls.Add(tableLayoutPanel);
testForm.ShowDialog();
}
Any suggestions?
Thank you, for the invested time.
The solution was to change the code to:
testForm.Controls.Add(tableLayoutPanel);
I have nested controls inside RadPageView control on a Winform application. RadPageView has a child RadPageViewPage. These two controls are on the form however a tab control and inside that tab control some other controls are added dynamically. How can I find and change the value of the Textbox inside the dynamically generated tab control on a click of the button.
public Form1()
{
InitializeComponent();
TabControl tb = new TabControl();
tb.Width = 500;
TabPage tp = new TabPage("Tab 1");
Label lb = new Label();
lb.Text = "Test";
lb.Location = new Point(10, 10);
TextBox txt = new TextBox();
txt.Text = "Textbox";
txt.Location = new Point(200, 10);
tp.Controls.Add(lb);
tp.Controls.Add(txt);
tb.Controls.Add(tp);
radPageViewPage1.Controls.Add(tb);
}
private void button1_Click(object sender, EventArgs e)
{
}
I have found this example over the internet and its working perfectly.
public Form1()
{
InitializeComponent();
TabControl tb = new TabControl();
tb.Width = 500;
TabPage tp = new TabPage("Tab 1");
Label lb = new Label();
lb.Text = "Test";
lb.Name = "lblTest";
lb.Location = new Point(10, 10);
TextBox txt = new TextBox();
txt.Text = "Textbox";
txt.Name = "txtName";
txt.Location = new Point(200, 10);
tp.Controls.Add(lb);
tp.Controls.Add(txt);
tb.Controls.Add(tp);
radPageViewPage1.Controls.Add(tb);
}
private void button1_Click(object sender, EventArgs e)
{
var crl = FindControl("txtName");
MessageBox.Show(crl.Text);
}
Control FindControl(string target)
{
return FindControl(this, target);
}
static Control FindControl(Control root, string target)
{
if (root.Name.Equals(target))
return root;
for (var i = 0; i < root.Controls.Count; ++i)
{
if (root.Controls[i].Name.Equals(target))
return root.Controls[i];
}
for (var i = 0; i < root.Controls.Count; ++i)
{
Control result;
for (var k = 0; k < root.Controls[i].Controls.Count; ++k)
{
result = FindControl(root.Controls[i].Controls[k], target);
if (result != null)
return result;
}
}
return null;
}
I am trying to build a grid of companies generated from my database.
I set my flowlayout as topdown. Is it possible to put a line between rows like this http://data.worldbank.org/country
If needed, my code posted below.
public void createLinks(string[] groupNames)
{
for (int i = 0; i < groupNames.Length; i++)
{
LinkLabel obj = new LinkLabel();
obj.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
obj.LinkColor = Color.Black;
obj.Name = groupNames[i];
obj.Text = groupNames[i];
obj.Click += delegate(object sender, EventArgs e)
{LinkLabel ss = sender as LinkLabel;
frmCompanyReport test = new frmCompanyReport(ss.Name);
test.Show();
};
flowLayoutPanel1.Controls.Add(obj);
}
}
One solution is to use a Label to act as a line. Set AutoSize to False, Height to 1, and BorderStyle to FixedSingle. Then set the Width to the same as the FlowLayoutPanel.
Something like:
public void createLinks(string[] groupNames)
{
for (int i = 0; i < groupNames.Length; i++)
{
LinkLabel obj = new LinkLabel();
obj.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
obj.LinkColor = Color.Black;
obj.Name = groupNames[i];
obj.Text = groupNames[i];
obj.Click += delegate(object sender, EventArgs e)
{
LinkLabel ss = sender as LinkLabel;
frmCompanyReport test = new frmCompanyReport(ss.Name);
test.Show();
};
flowLayoutPanel1.Controls.Add(obj);
Label line = new Label();
line.AutoSize = false;
line.BorderStyle = BorderStyle.FixedSingle;
line.Height = 1;
line.Width = flowLayoutPanel1.Width;
flowLayoutPanel1.Controls.Add(line);
}
}