We are going to create a link control and an image control one next to each other. First we have to set which control we are going to create, so in the form class area place:
private PictureBox PicBox;
private LinkLabel Blue;
Now when you want to create the link and picturebox call this //CREATES AN EVENT HANDLER FOR THE LINK
EventHandler handler = new EventHandler(LinkLabel_Click);
//BUILD CONTROL LINK
Blue = new LinkLabel();
Blue.Text = "Link Control";
Blue.Location = new Point(30, 20);
Blue.Size = new Size(150, 20);
Blue.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
Blue.Click +=handler;
//CREATES AN NEW PICTUREBOX AND FILLS IT WITH A ICON
PicBox = new PictureBox();
PicBox.Image = Image.FromFile(@"c:\pic.ico");
PicBox.Top = 20;
PicBox.Width = 16;
PicBox.Height = 16;
PicBox.Left = 10;
//ADD CONTROLS
Controls.Add(PicBox);
Controls.Add(Blue);
You created a handler for the click event of the link button so you could now could this in: private void LinkLabel_Click(object sender, EventArgs e)
{
//SPLITS THE DATA SO U CAN GET THE TEXT , U COULD PARSE IT THROUGH A CASE STATMENT FOR DIFFERENT SELECTIONS
string Data = Convert.ToString(sender);
string [] Split = Data.Split(new Char [] {':'});
MessageBox.Show("Link Clicked :" + Convert.ToString(Split[1]));
}
If you wanted to create a list of controls you could use the panel control and set the autoscroll property to true so everytime another control is added with a larger top value the scrollbar would appear giving the user the impression of a list control. You can add controls to a panel control simply by putting
panel1.Controls.Add(PicBox);
panel1.Controls.Add(Blue);
Create Controls At Runtime
By Colin Harman MACITP, published on 04 Feb 2004
| Filed in
You might also like...
C# forum discussion
-
USB Drive Activity Logger!
by coque0912 (7 replies)
-
Exporting data from a repeater to an Excel sheet
by TomBlack (28 replies)
-
Calculate Date of Birth from Age
by k.prashanth (7 replies)
-
Windows Service, getting outOf memory exception
by programmerdude2341 (3 replies)
-
How to Make Default Button?
by puspitaonlinebooks (24 replies)
C# podcasts
-
.NET Rocks: Stories from CodeMash!
Published 8 years ago, running time 0h56m
While at CodeMash, Carl and Richard collected some great stories. First up is Evan Huack, who digs into cudafy, a library on codeplex that allows you to run C# code (any IL code actually) on your GPU. The second story is about Jessie Shternshus and her company The Improv Effect. Jessie talks abou.
C# jobs
-
.NET, C#, MVC Senior Software Developer
Frontline Technologies, Inc. in Exton, United States
-
C#, JQuery, KnockoutJs. Nhibernate. Unique 3-6 month opportunity in Kitesurfing/Windsurfing heaven
RWI in Cabarete, Dominican Republic
$12,000-12,000 USD per year (Contract) -
C# / .NET Programmer
Mews Systems in Prague, Czech Republic
-
Experienced C# Developer
Zenco Systems Ltd in Rugby, United Kingdom
Annual Bonus, Annual Salary Review, Perfomance Related Bonus
Comments