New to C# - struggling with links + functions + ... etc etc !
-
Dear all I am brand new to C# and have previously only written programs in Javascript, so go easy on me ! I have written an "app launcher" program which reads a text file line by line. Each line is just a path to a program e.g. C:\Users\Matt\Desktop\Gravity.exe So far, my program can successfully read each line and produce a list of links. As intended, each link appears as the path itself. The problem I am having is that these links will not work. However they WILL work if they are all just given the same fixed path. I would like each link to use its .Text property as the destination. (please see the comments "works" and "does not work" in my code below). The only error I get is "cannot find the file specified". I would really appreciate any help on this as I am finding C a lot harder than Javascript ! Thank you Jim
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; //for reading a text file namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) //on form load { int counter = 0; string line; string myfile = @"c:\users\matt\desktop\file.txt"; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(myfile); while ((line = file.ReadLine()) != null) { //MessageBox.Show(line); //check whats on each line LinkLabel mylinklabel = new LinkLabel(); //LinkLabel tells us the type of the object e.g. string mystring ="hello"; mylinklabel.Text = line; this.Controls.Add(mylinklabel); mylinklabel.Location = new Point(0, 30 + counter * 30); mylinklabel.Click += new System.EventHandler(LinkClick); counter++; } file.Close(); } private void LinkClick(object sender, System.EventArgs e) { System.Diagnostics.Process.Start(this.Text); //doesn't work //System.Diagnostics.Process.Start(@"C:\Users\Matt\Desktop\gravity.exe"); //works } } }
-
Dear all I am brand new to C# and have previously only written programs in Javascript, so go easy on me ! I have written an "app launcher" program which reads a text file line by line. Each line is just a path to a program e.g. C:\Users\Matt\Desktop\Gravity.exe So far, my program can successfully read each line and produce a list of links. As intended, each link appears as the path itself. The problem I am having is that these links will not work. However they WILL work if they are all just given the same fixed path. I would like each link to use its .Text property as the destination. (please see the comments "works" and "does not work" in my code below). The only error I get is "cannot find the file specified". I would really appreciate any help on this as I am finding C a lot harder than Javascript ! Thank you Jim
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; //for reading a text file namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) //on form load { int counter = 0; string line; string myfile = @"c:\users\matt\desktop\file.txt"; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(myfile); while ((line = file.ReadLine()) != null) { //MessageBox.Show(line); //check whats on each line LinkLabel mylinklabel = new LinkLabel(); //LinkLabel tells us the type of the object e.g. string mystring ="hello"; mylinklabel.Text = line; this.Controls.Add(mylinklabel); mylinklabel.Location = new Point(0, 30 + counter * 30); mylinklabel.Click += new System.EventHandler(LinkClick); counter++; } file.Close(); } private void LinkClick(object sender, System.EventArgs e) { System.Diagnostics.Process.Start(this.Text); //doesn't work //System.Diagnostics.Process.Start(@"C:\Users\Matt\Desktop\gravity.exe"); //works } } }
Hi, welcome to CodeProject. Please use PRE tags correctly to show code snippets. You could still edit the existing message. Your problem is this: in
System.Diagnostics.Process.Start(this.Text);
this refers to the instance of the class it is in, i.e. to your Form, and not to the specific LinkLabel you are hoping it refers to. To get that, try this:LinkLabel link=sender as LinkLabel;
if (link!=null) System.Diagnostics.Process.Start(link.Text);I also suggest you add some error handling, so you get a decent response when the file doesn't exist. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
Dear all I am brand new to C# and have previously only written programs in Javascript, so go easy on me ! I have written an "app launcher" program which reads a text file line by line. Each line is just a path to a program e.g. C:\Users\Matt\Desktop\Gravity.exe So far, my program can successfully read each line and produce a list of links. As intended, each link appears as the path itself. The problem I am having is that these links will not work. However they WILL work if they are all just given the same fixed path. I would like each link to use its .Text property as the destination. (please see the comments "works" and "does not work" in my code below). The only error I get is "cannot find the file specified". I would really appreciate any help on this as I am finding C a lot harder than Javascript ! Thank you Jim
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.IO; //for reading a text file namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) //on form load { int counter = 0; string line; string myfile = @"c:\users\matt\desktop\file.txt"; // Read the file and display it line by line. System.IO.StreamReader file = new System.IO.StreamReader(myfile); while ((line = file.ReadLine()) != null) { //MessageBox.Show(line); //check whats on each line LinkLabel mylinklabel = new LinkLabel(); //LinkLabel tells us the type of the object e.g. string mystring ="hello"; mylinklabel.Text = line; this.Controls.Add(mylinklabel); mylinklabel.Location = new Point(0, 30 + counter * 30); mylinklabel.Click += new System.EventHandler(LinkClick); counter++; } file.Close(); } private void LinkClick(object sender, System.EventArgs e) { System.Diagnostics.Process.Start(this.Text); //doesn't work //System.Diagnostics.Process.Start(@"C:\Users\Matt\Desktop\gravity.exe"); //works } } }
See this wud work for sure System.Diagnostics.Process.Start(((LinkLabel)sender).Text); :cool:
modified on Thursday, May 5, 2011 5:58 AM
-
See this wud work for sure System.Diagnostics.Process.Start(((LinkLabel)sender).Text); :cool:
modified on Thursday, May 5, 2011 5:58 AM
It's bad enough having questions in txtspeak. Answers should be in grammatical English. :mad:
Software rusts. Simon Stephenson, ca 1994.
-
Hi, welcome to CodeProject. Please use PRE tags correctly to show code snippets. You could still edit the existing message. Your problem is this: in
System.Diagnostics.Process.Start(this.Text);
this refers to the instance of the class it is in, i.e. to your Form, and not to the specific LinkLabel you are hoping it refers to. To get that, try this:LinkLabel link=sender as LinkLabel;
if (link!=null) System.Diagnostics.Process.Start(link.Text);I also suggest you add some error handling, so you get a decent response when the file doesn't exist. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Good answer. To provide a little more help to the newbie who asked the question: In C#, the convention (and what IDEs produce) is that event handlers are written in the form class, so this refers to the form and not to the source of the event. The sender parameter is the source of the event (e.target, to make a JavaScript analogy), but you will need to cast it to a type that you know it must be. In addition, for event handlers which are fired only off one control, you can simply refer to that control in the event handler. E.g.
LinkLabel myLinkLabel;
Form1(){
// ...
myLinkLabel = new LinkLabel();
myLinkLabel.Text = "/thing.exe";
// ... etc
myLinkLabel.Click += (s,e) => {
// Within the handler: 'this' = the form
// 's' = the event source, but as object type
// 'e' = the event arguments, sometimes contains useful information like click location
Process.Start(((LinkLabel)s).Text);
// Equivalently: Process.Start(myLinkLabel.Text)
// But only because this event is only hooked to myLinkLabel
};
}(I wrote the event handler as a lambda delegate, for .Net 3.0 and up, but the same things apply if you write it as a normal method.) Edit: ... but missed out the semicolon at the end of the delegate assignment.
modified on Tuesday, May 3, 2011 10:40 AM
-
It's bad enough having questions in txtspeak. Answers should be in grammatical English. :mad:
Software rusts. Simon Stephenson, ca 1994.
Sorry Sir i am just new at it
modified on Thursday, May 5, 2011 6:11 AM