Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. New to C# - struggling with links + functions + ... etc etc !

New to C# - struggling with links + functions + ... etc etc !

Scheduled Pinned Locked Moved C#
helpcsharpjavascriptlinqgraphics
6 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    ruby_murray
    wrote on last edited by
    #1

    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 } } }

    L S 2 Replies Last reply
    0
    • R ruby_murray

      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 } } }

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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.

      B 1 Reply Last reply
      0
      • R ruby_murray

        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 } } }

        S Offline
        S Offline
        Steven Pinto2000
        wrote on last edited by
        #3

        See this wud work for sure System.Diagnostics.Process.Start(((LinkLabel)sender).Text); :cool:

        modified on Thursday, May 5, 2011 5:58 AM

        P 1 Reply Last reply
        0
        • S Steven Pinto2000

          See this wud work for sure System.Diagnostics.Process.Start(((LinkLabel)sender).Text); :cool:

          modified on Thursday, May 5, 2011 5:58 AM

          P Offline
          P Offline
          Peter_in_2780
          wrote on last edited by
          #4

          It's bad enough having questions in txtspeak. Answers should be in grammatical English. :mad:

          Software rusts. Simon Stephenson, ca 1994.

          S 1 Reply Last reply
          0
          • L Luc Pattyn

            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.

            B Offline
            B Offline
            BobJanova
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • P Peter_in_2780

              It's bad enough having questions in txtspeak. Answers should be in grammatical English. :mad:

              Software rusts. Simon Stephenson, ca 1994.

              S Offline
              S Offline
              Steven Pinto2000
              wrote on last edited by
              #6

              Sorry Sir i am just new at it

              modified on Thursday, May 5, 2011 6:11 AM

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups