Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

trying to set link on the fly 1

Status
Not open for further replies.

G00GLER

Instructor
May 17, 2005
57
US
I can create a linklabels on the fly but can't seem to pass what I want to run when linklabel clicked as it depends on which linklabel was clicked

OBJECTIVE: create a link to a video for each video file in a folder.

CODE THAT DOESN'T WORK:
Code:
			// Set the directory to the sample picture directory.
			System.IO.DirectoryInfo dirInfo = 
				new System.IO.DirectoryInfo(
				"c:\\Documents and Settings\\Delora\\Desktop");
    

			// Get the .jpg files from the directory
			System.IO.FileInfo[] files = dirInfo.GetFiles("*.avi");

			// Add each file name and full name including path
			// to the ListView.
			i=5;
			if (files != null)
				foreach ( System.IO.FileInfo file in files )
				{
					linkLabel = new LinkLabel();
					linkLabel.Size = new Size( 180, 20 );
					linkLabel.Location = new Point(10,i);
					linkLabel.Text = file.Name;
					linkLabel.Links.Add(0,file.Name.Length,dirInfo.Name + file.FullName);
					linkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(this.OnClickVid(dirInfo.Name + file.FullName));
					this.Controls.Add(linkLabel);
					i=i+20;
				}
			}
		public void OnClickVid (string filename)
		{
			linkLabel.LinkVisited = true;
			MessageBox.Show(filename);
			//System.Diagnostics.Process.Start( filename );
		}
 
Try this out i have 10 htm files or so on my desktop the code below appears to generate 10 linklabels, but when i click on any of the 10 linklabels, 10th linklabel is the one always assumed was clicked and thus MessageBox show that value.

Is it safe to assume I can't use linkLabels for this purpose and instead need to create ListBoxes or something.

Code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;



namespace AddLinkLabel
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.ComponentModel.Container components = null;
		private Label myLabel;
		private LinkLabel linkLabel;
		private int i;
		private string myPath;


		//			InitializeComponent();

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			// 
			// Form1
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
			this.BackColor = System.Drawing.Color.IndianRed;
			this.ClientSize = new System.Drawing.Size(292, 266);
			this.Name = "Form1";
			this.Text = "Form1";

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>
		[STAThread]
		static void Main() 
		{
			Application.Run(new Form1());
		}

		public Form1()
		{
			// Set up Form
			this.Size = new Size( 800, 600 );
			this.MaximizeBox = false;
			this.FormBorderStyle = FormBorderStyle.FixedSingle;
			this.Text = "C&M Traing OnDemand";

			// Set up myLabel
			myLabel = new Label();
			myLabel.Text = "C&M Traing OnDemand";
			myLabel.Size = new Size( 180, 20 );
			myLabel.Location = new Point(10,210);
			myLabel.ForeColor = Color.SteelBlue;
			myLabel.Font = new Font("Microsoft Sans Serif" , 12f , FontStyle.Bold);
			this.Controls.Add(myLabel);

			//		Directory fl = new Directory("c:\\Documents and settings\\Delora\\Desktop\\");
			//		File [] childfiles = fl.GetFiles();
			//		foreach (File childfile in childfiles)
			//		{
	
			// Set the directory to the sample picture directory.
			System.IO.DirectoryInfo dirInfo = 
				new System.IO.DirectoryInfo(
				"c:\\Documents and Settings\\Delora\\Desktop");
    

			// Get the htm files from the directory
			System.IO.FileInfo[] files = dirInfo.GetFiles("*.htm");

			// Add each file name and full name including path
			// to the ListView.
			myPath="";
			i=5;
			if (files != null)
				foreach ( System.IO.FileInfo file in files )
				{
					myPath = dirInfo.Name + file.FullName;
					linkLabel = new LinkLabel();
					linkLabel.Size = new Size(500, 25 );
					linkLabel.Location = new Point(10,i);
					linkLabel.Text = file.Name;
					linkLabel.Name= myPath;
					linkLabel.Links.Add(0,file.Name.Length,myPath);
					linkLabel.LinkClicked += new LinkLabelLinkClickedEventHandler(this.OnClickVideo);
					this.Controls.Add(linkLabel);
					i=i+30;
				}
			}
		protected void OnClickVideo (object sender, LinkLabelLinkClickedEventArgs e)
			{
				linkLabel.LinkVisited = true;
				MessageBox.Show(linkLabel.Name);
				//System.Diagnostics.Process.Start( e.ToString );
			}
		}
	}
 
Actually, I think it your code works correctly, but the way you have your OnClickVideo method setup is wrong. After you are done creating your linkLabels and adding them to the Controls collection of the form, your global linkLabel variable is still equal to the last linkLabel you added. So when the OnClickVideo event fires, it will always show the information pertaining to THAT one, and not the one that was clicked.
 
Since objects are referenced from the heap, you can manipulate the object that called the event using and boxing the passed sender object:

Code:
 protected void OnClickVideo (object sender, LinkLabelLinkClickedEventArgs e)
            {
                LinkLabel tempLabel = (LinkLabel)sender;
                tempLabel.LinkVisited = true;
                MessageBox.Show(tempLabel.Name);
                //System.Diagnostics.Process.Start( e.ToString );
            }
 
THANKYOU so much.

I actually had abandoned approach and inputed a web browser control into my winform to read an html file with the links.

Now going to switch back thank you so much for pointing out what was causing it.
 
No problem. Sometimes all it takes is another pair of eyes. Glad I could help!

--Brad
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top