Windows Develop Bookmark and Share   
 index > Windows Forms General > Programatically adding linklabel
 

Programatically adding linklabel

Hi i am trying to add linklabels to a panel at run time. i want to set each link name to a value in a list, like

foreach(Customer customer in customers)
{
add linklabel and set its properties.

e.g. linklabel.Text = customer.Name
etc........
}


Thanks.


nadim  Wednesday, September 09, 2009 12:01 PM
Hi,
I hope it will help you
 int y = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            Panel pnl = new Panel();
            pnl.Width=this.Width;
            pnl.Height=this.Height;
            pnl.BackColor = Color.Bisque;
            this.Controls.Add(pnl);
            for (int i = 0; i < 10; i++)
            {
                LinkLabel lbl = new LinkLabel();
                lbl.Width = 100;
                lbl.Location = new Point(100, y);
                lbl.Text = "ABC" + " " + i;
                pnl.Controls.Add(lbl);
                y = y + 100;
                
            }
        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Marked As Answer bynadim Wednesday, September 09, 2009 12:57 PM
  •  
Gnanadurai  Wednesday, September 09, 2009 12:18 PM
A bit of try and error and i got it working.

for (int j=0; j < 10; j++)
{
LinkLabel label = new LinkLabel();
label.Width = 200;
label.Height = 15;
label.LinkArea = new LinkArea(0, 50);
label.Location = new Point(10, y);
label.TextAlign = ContentAlignment.TopLeft;
label.Text = "ABC" + j

panel1.Controls.Add(label);
y = y + 15;
}

i think u have to specify the height to the value of the Y position offset.
  • Marked As Answer bynadim Wednesday, September 09, 2009 12:57 PM
  •  
nadim  Wednesday, September 09, 2009 12:57 PM
Hi,
I hope it will help you
 int y = 0;
        private void Form1_Load(object sender, EventArgs e)
        {
            Panel pnl = new Panel();
            pnl.Width=this.Width;
            pnl.Height=this.Height;
            pnl.BackColor = Color.Bisque;
            this.Controls.Add(pnl);
            for (int i = 0; i < 10; i++)
            {
                LinkLabel lbl = new LinkLabel();
                lbl.Width = 100;
                lbl.Location = new Point(100, y);
                lbl.Text = "ABC" + " " + i;
                pnl.Controls.Add(lbl);
                y = y + 100;
                
            }
        }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
  • Marked As Answer bynadim Wednesday, September 09, 2009 12:57 PM
  •  
Gnanadurai  Wednesday, September 09, 2009 12:18 PM

Thanks......sort of works, but it only shows the first item.
nadim  Wednesday, September 09, 2009 12:32 PM
A bit of try and error and i got it working.

for (int j=0; j < 10; j++)
{
LinkLabel label = new LinkLabel();
label.Width = 200;
label.Height = 15;
label.LinkArea = new LinkArea(0, 50);
label.Location = new Point(10, y);
label.TextAlign = ContentAlignment.TopLeft;
label.Text = "ABC" + j

panel1.Controls.Add(label);
y = y + 15;
}

i think u have to specify the height to the value of the Y position offset.
  • Marked As Answer bynadim Wednesday, September 09, 2009 12:57 PM
  •  
nadim  Wednesday, September 09, 2009 12:57 PM

Hi,
Modified one
just tryout

  int y = 0;      
        int i = 0;
        private void Form1_Load(object sender, EventArgs e)
        {           
            CreateControls();
        }
        private void CreateControls()
        {
              Panel pnl = new Panel();
            pnl.Width=this.Width;
            pnl.Height=this.Height;
            pnl.BackColor = Color.Bisque;
            this.Controls.Add(pnl);
            List<Customer> cust = CreateCustomer();            
            foreach(Customer customer in cust)
            {
                LinkLabel lbl = new LinkLabel();
                lbl.Width = 100;
                lbl.Name ="custName"+i;
                lbl.Location = new Point(150, y);
                lbl.Text = customer.CustName;  
               
                LinkLabel lbl1 = new LinkLabel();
                lbl1.Width = 100;
                lbl1.Name = "custAdd" + i;
                lbl1.Location = new Point(100, y);
                lbl1.Text = customer.CustAddress;
              
              
                LinkLabel lbl2 = new LinkLabel();
                lbl2.Width = 100;
                lbl2.Name = "custCurr" + i;
                lbl2.Location = new Point(50, y);
                lbl2.Text = customer.CustCurrency;
                pnl.Controls.Add(lbl);
                pnl.Controls.Add(lbl1);
                pnl.Controls.Add(lbl2);
                y = y + 100;
                i = i + 1;
            }
        }
        private List<Customer> CreateCustomer()
        {
            List<Customer> cust = new List<Customer>();
            cust.Add(new Customer("ABC","DFD","HGFJ"));
            cust.Add(new Customer("DEF", "REW", "FGGH"));
            cust.Add(new Customer("DFS", "GGF", "ASAZ"));
            cust.Add(new Customer("FSS", "JKH", "ADF"));
            cust.Add(new Customer("DFF", "WER", ";JK"));
            cust.Add(new Customer("FDF", "JGH", "FDGD"));
            return cust;
        }
    }
    public class Customer
    {
        public Customer(string name, string address, string currency)
        {
            custName = name;
            custAddress = address;
            custCurrency = currency;
        }
        private string custName;

        public string CustName
        {
            get { return custName; }
            set { custName = value; }
        }
        private string custAddress;

        public string CustAddress
        {
            get { return custAddress; }
            set { custAddress = value; }
        }
        private string custCurrency;

        public string CustCurrency
        {
            get { return custCurrency; }
            set { custCurrency = value; }
        }

    }

Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, September 09, 2009 1:14 PM

You can use google to search for other answers

Custom Search

More Threads

• Richtextbox and control keys
• Inherited Form Resize problem
• how to share memory variable within two forms?
• Popup Window not Responding
• Multithreading and and "encroachments"
• Crystal Report:Logon Failed
• Doubt on how to implement comboxes in a datagridview column.
• Why g.DrawImage(myImage) image quality is not good?
• Block resizing of windows form on double click title bar.
• Multi-dimensional array property