Library code snippets
Create Controls At Runtime
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);
Related articles
Related discussion
-
how to select multiple files at a time using Ctrl+select and upload the attachments in C# .Net 1.0?
by vasanta (0 replies)
-
Help please for student
by mandy130 (0 replies)
-
Loop help needed
by BKRoberts (5 replies)
-
Problem after strong naming an assembly
by rinkurathor1 (0 replies)
-
Regarding User control creation in C# .Net(Windows Application)
by porchelvi (0 replies)
Related podcasts
-
Looking into the C# Crystal Ball with Charlie Calvert and Bill Wagner
One of the most exciting announcements from PDC was the news about C# 4.0 and Visual Studio 2010. With all the excitement and discussion throughout the event about these new developer tools, we reached out to two experts in the fields. Charlie Calvert and Bill Wagner sat down with Keith and Woody...
Events coming up
-
Dec
6
Developing AJAX Web Applications with Castle Monorail
London, United Kingdom
Monorail is the model-view-controller engine of the Castle Project, bringing many of the best ideas of Ruby on Rails to the .NET world. In this talk, David De Florinier and Gojko Adzic show how Monorail makes it easy to develop .NET based AJAX applications, and how to use the Castle Project to build Web 2.0 applications effectively. Come to this session if you are a .NET web developer. Everyone is welcome!
It was very good one.
But i am facing a problem when i am creating runtime controls on button click.
event of runtime controls are not working.Plz tell me its urgent
This thread is for discussions of Create Controls At Runtime.