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);
}
}
Related
I have a form like this. You can see my dynamic groupbox (30 groupbox) in Flowlayout on the right, and a picturebox to show questions on the left.
I'm worndering that How to use Click event for each groupbox to show the question equivalent in Picturebox?
Here's my code of groupbox
public GroupBox gbx(String name, int s, string i, string msch)
{
this.Name = int.Parse(i);
this.sda = s;
this.MsCauHoi = msch;
// gb
//
if (s == 2)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
}
if (s == 3)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
gb.Controls.Add(cb3);
}
if (s == 4)
{
gb.Controls.Add(cb1);
gb.Controls.Add(cb2);
gb.Controls.Add(cb3);
gb.Controls.Add(cb4);
}
gb.Location = new System.Drawing.Point(219, 44);
gb.Margin = new System.Windows.Forms.Padding(1, 1, 1, 1);
gb.Name = name;
gb.Padding = new System.Windows.Forms.Padding(1, 1, 1, 1);
gb.Size = new System.Drawing.Size(120, 45);
gb.TabIndex = 7;
gb.TabStop = false;
gb.Text = i;
gb.BackColor = Color.Silver;
//
// cb1
//
cb1.AutoSize = true;
cb1.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb1.Location = new System.Drawing.Point(15, 13);
cb1.Margin = new System.Windows.Forms.Padding(0);
cb1.Name = "cb1";
cb1.Size = new System.Drawing.Size(17, 31);
cb1.TabIndex = 0;
cb1.Text = "1";
cb1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb1.UseVisualStyleBackColor = true;
cb1.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb2
//
cb2.AutoSize = true;
cb2.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb2.Location = new System.Drawing.Point(35, 13);
cb2.Name = "cb2";
cb2.Size = new System.Drawing.Size(17, 31);
cb2.TabIndex = 1;
cb2.Text = "2";
cb2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb2.UseVisualStyleBackColor = true;
cb2.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb3
//
cb3.AutoSize = true;
cb3.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb3.Location = new System.Drawing.Point(58, 13);
cb3.Name = "cb3";
cb3.Size = new System.Drawing.Size(17, 31);
cb3.TabIndex = 2;
cb3.Text = "3";
cb3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb3.UseVisualStyleBackColor = true;
cb3.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
//
// cb4
//
cb4.AutoSize = true;
cb4.CheckAlign = System.Drawing.ContentAlignment.BottomCenter;
cb4.Location = new System.Drawing.Point(81, 13);
cb4.Name = "cb4";
cb4.Size = new System.Drawing.Size(17, 31);
cb4.TabIndex = 3;
cb4.Text = "4";
cb4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
cb4.UseVisualStyleBackColor = true;
cb4.CheckedChanged += delegate (object sender, EventArgs e)
{
CheckBox b = new CheckBox();
b = (CheckBox)sender;
if (b.Checked == true)
{
gb.BackColor = Color.Turquoise;
}
};
return gb;
}
And here's the code of FormLoad
private void FrmThi_Load(object sender, EventArgs e)
{
droptable();
this.CauDaLam = 0;
if (dethi == null) setdethi();
Screen scr = Screen.PrimaryScreen; //đi lấy màn hình chính
this.Left = (scr.WorkingArea.Width - this.Width) / 2;
this.Top = (scr.WorkingArea.Height - this.Height) / 2;
int i = 0;
foreach (DataRow row in this.dethi.Rows)
{
String myValue = row["SoDA"].ToString();
String msch = row["MaCH"].ToString();
ptl = new FrmPhieuTraLoi();
pn_DeThi.Controls.Add(ptl.gbx("cau" + (i + 1).ToString(), int.Parse(myValue), (i + 1).ToString(), msch));
listptl.Add(ptl);
i++;
}
loadcauhoi(this.CauDangLam);
listptl[CauDangLam].setBackColorCDL();
Random r = new Random();
lbmade1.Text = r.Next(1, 4).ToString();
txt = lbSatHachBangLai.Text;
len = txt.Length;
lbSatHachBangLai.Text = "";
timer1.Start();
this.timer2.Start();
}
This is an example code:
private void Form1_Load(object sender, EventArgs e)
{
for(int idx = 0; idx < 5; idx++)
{
var gBox = new GroupBox();
gBox.Height = 50;
gBox.Width = 50;
gBox.Text = "Box: " + idx;
gBox.Tag = idx;
gBox.MouseClick += GBox_MouseClick;
this.Controls.Add(gBox);
}
}
private void GBox_MouseClick(object sender, MouseEventArgs e)
{
//var ctrl = sender as Control; -- Not required
int questionIdx = (int)(sender as Control).Tag;
}
I would extend the GroupBox control and add a property for the Question ID (Add new Item > Custom Control) and change the class to look like this:
public partial class QuestionGroupBox : GroupBox
{
public string QuestionID;
public QuestionGroupBox()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
}
}
Then populate your flowlayout and set the QuestionID and Click EventHandler, similar to this:
var gb = new QuestionGroupBox{ QuestionID = "44"};
gb.Click += new System.EventHandler(this.questionGroupBox_Click);
//add add to your flow layout
Finally create your event handler to get and display the question
private void questionGroupBox_Click(object sender, EventArgs e)
{
var questionID = ((QuestionGroupBox)sender).QuestionID;
//display your question
}
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;
}
I want to be able to assign each checkbox to it's own richtextbox
I'm making the richtextboxes, then I'm making the checkboxes but how can I "link" them together?
for example :
// richtextbox 1 - > checkbox 1 = false
// richtextbox 2 - > checkbox 2 = true
// richtextbox 3 - > checkbox 3 = true
// richtextbox 4 - > checkbox 4 = false
this is my code:
int n = TodoItems.Count;
RichTextBox[] RichtextBoxes = new RichTextBox[n];
CheckBox[] Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = true;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += new EventHandler(this.CheckedChange);
TodoList.Controls.Add(Checkboxes[i]);
}
I have modified your code please see below, you can access your desired rich text box on checkbox click.
RichTextBox[] RichtextBoxes { get; set; }
CheckBox[] Checkboxes { get; set; }
private void Form1_Load(object sender, EventArgs e)
{
int n = TodoItems.Count;
RichtextBoxes = new RichTextBox[n];
Checkboxes = new CheckBox[n];
for (int i = 0; i < n; i++)
{
//creating the richtextbox
RichtextBoxes[i] = new RichTextBox();
RichtextBoxes[i].Name = "TB-" + i.ToString();
RichtextBoxes[i].Text = TodoItems[i].ToString();
RichtextBoxes[i].Location = new System.Drawing.Point(130, (10 + (60 * i)));
RichtextBoxes[i].Size = new System.Drawing.Size(300, 50);
RichtextBoxes[i].Visible = false;
RichtextBoxes[i].ReadOnly = true;
RichtextBoxes[i].SelectionAlignment = HorizontalAlignment.Center;
RichtextBoxes[i].BackColor = Color.White;
TodoList.Controls.Add(RichtextBoxes[i]);
//creating the checkboxes
Checkboxes[i] = new CheckBox();
Checkboxes[i].Name = "CB-" + i.ToString();
Checkboxes[i].Text = "";
Checkboxes[i].Location = new System.Drawing.Point(440, (30 + (60 * i)));
Checkboxes[i].Size = new System.Drawing.Size(18, 17);
Checkboxes[i].Visible = true;
Checkboxes[i].CheckedChanged += checkBox1_CheckedChanged;
TodoList.Controls.Add(Checkboxes[i]);
}
}
void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
string cbName = cb.Name;
int sbNumber = int.Parse(cbName.Split('-')[1]);
RichtextBoxes[sbNumber].Visible = true; // you can get desired richtextbox here and can any thing with it :)
}
I have found the answere, and I'll post it to make your life easyer
when I made the richtextboxes and the checkboxes I set there names to a number
Checkboxes[i].Name = i.ToString();
then I used the event that was provided for me by Mirza Danish Baig
Create and event void checkBox_CheckedChanged(object sender, EventArgs e) { } and then assign this event name Checkboxes[i].CheckedChanged += checkBox_CheckedChanged; –
and after that I started to try things eventualy I came to this :
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
CheckBox ThisCheckbox = (CheckBox)sender;
if (ThisCheckbox.Checked == true)
{
//finding the richtextbox by id...
RichTextBox ThisRichtextbox = this.Controls.Find("TB" + ThisCheckbox.Name, true).FirstOrDefault() as RichTextBox;
try//try and catch for testing, this can be removed later.
{
MessageBox.Show(ThisRichtextbox.Text);
}
catch (Exception Exc)
{
MessageBox.Show(Exc.Message);
}
}
}
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 have added checkboxes dynamically on a c sharp form....
and they are disabled when the form is loaded...
each checkbox has red color by default... i have assigned it black programmatically ...
but when the form is loaded it is red...i dont know why this is happening....
private void Form2_Load(object sender, EventArgs e)
{
for (int i = 0; i < list.Count; i++)
{
CheckBox c = new CheckBox();
c.Text = i.ToString();
c.Width = 120;
flowLayoutPanel1.Controls.Add(c);
c.ForeColor = Color.Black;
}
flowLayoutPanel1.Enabled = false;
}
It gets Black color only after i enable the flowLayoutPanel....
I want checkboxes have black color even when the form is loaded...
Here is one way. Change your code to this:
for (int i = 0; i < list.Count; i++)
{
CheckBox c = new CheckBox();
c.Text = "";
c.Tag = i.ToString();
c.Width = 120;
flowLayoutPanel1.Controls.Add(c);
c.Paint += new PaintEventHandler(c_Paint);
}
flowLayoutPanel1.Enabled = false;
And in c_Paint method you can draw control's text (saved in Tag property)
void c_Paint(object sender, PaintEventArgs e)
{
Control c = sender as Control;
if (c != null)
{
string text = c.Tag.ToString();
e.Graphics.SmoothingMode =
System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
RectangleF rect = new RectangleF(
new PointF(19, 5),
e.Graphics.DrawString(text, this.Font, Brushes.Black, new PointF(19, 5));
}
}
you should create a function to do that and call it after the InitializeComponent(); in the form constructor. You should enable the panel first too.
public partial class YourForm: Form
{
public YourForm()
{
InitializeComponent();
// put your function here
}