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. How can i add A Linklable by calling a function in a class?

How can i add A Linklable by calling a function in a class?

Scheduled Pinned Locked Moved C#
questiongraphicshelptutorial
10 Posts 2 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.
  • A Offline
    A Offline
    Arunkumar Koloth
    wrote on last edited by
    #1

    Hi All Is it Possible to Call A Function Like This to add a link label to a particular form public void AddLinklabel(string form,int top, int left, int width, int height,string Link) { LinkLabel linkLabel1 = new LinkLabel(); form.linkLabel1.Location = new System.Drawing.Point(top, left); form.linkLabel1.Size = new System.Drawing.Size(width, height); form.linkLabel1.AutoSize = true; } If Possible Please Show Me A Example Code Please Help Me

    Arunkumar.T ------------------------- p4,3ghz,hdd-500gb,ram-4gb

    P 1 Reply Last reply
    0
    • A Arunkumar Koloth

      Hi All Is it Possible to Call A Function Like This to add a link label to a particular form public void AddLinklabel(string form,int top, int left, int width, int height,string Link) { LinkLabel linkLabel1 = new LinkLabel(); form.linkLabel1.Location = new System.Drawing.Point(top, left); form.linkLabel1.Size = new System.Drawing.Size(width, height); form.linkLabel1.AutoSize = true; } If Possible Please Show Me A Example Code Please Help Me

      Arunkumar.T ------------------------- p4,3ghz,hdd-500gb,ram-4gb

      P Offline
      P Offline
      Pete OHanlon
      wrote on last edited by
      #2

      Why not convert your method into a generic method? BTW, please enclose code samples in pre blocks like this:

      public void AddLinkLabel<T>(T containingForm, int top, int left, int width, int height, string link) where T : Form
      {
      LinkLabel label = new LinkLabel();
      label.Location = new System.Drawing.Point(top, left);
      label.Size = new System.Drawing.Size(width, height);
      containingForm.Controls.Add(label);
      }

      Note that I've just typed this sample into this textbox, so there might be an error or two, you should be able to get the gist from this though.

      Forgive your enemies - it messes with their heads

      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

      A 1 Reply Last reply
      0
      • P Pete OHanlon

        Why not convert your method into a generic method? BTW, please enclose code samples in pre blocks like this:

        public void AddLinkLabel<T>(T containingForm, int top, int left, int width, int height, string link) where T : Form
        {
        LinkLabel label = new LinkLabel();
        label.Location = new System.Drawing.Point(top, left);
        label.Size = new System.Drawing.Size(width, height);
        containingForm.Controls.Add(label);
        }

        Note that I've just typed this sample into this textbox, so there might be an error or two, you should be able to get the gist from this though.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

        A Offline
        A Offline
        Arunkumar Koloth
        wrote on last edited by
        #3

        I Called The Function Like This But There are lot of errors displaying Error 1 The type 'string' must be convertible to 'System.Windows.Forms.Form' in order to use it as parameter 'T' in the generic type or method 'WindowsApplication1.Form1.AddLinkLabel<T>(T, int, int, int, int, string)' C:\Documents and Settings\Agni\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 21 13 WindowsApplication1

        AddLinkLabel("Form1", 20, 20, 100, 30, "Click Here");

        is there any other method to call this function?

        P 1 Reply Last reply
        0
        • A Arunkumar Koloth

          I Called The Function Like This But There are lot of errors displaying Error 1 The type 'string' must be convertible to 'System.Windows.Forms.Form' in order to use it as parameter 'T' in the generic type or method 'WindowsApplication1.Form1.AddLinkLabel<T>(T, int, int, int, int, string)' C:\Documents and Settings\Agni\My Documents\Visual Studio 2005\Projects\WindowsApplication1\WindowsApplication1\Form1.cs 21 13 WindowsApplication1

          AddLinkLabel("Form1", 20, 20, 100, 30, "Click Here");

          is there any other method to call this function?

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          Of course you've got errors, you've given it a string in place of the form instance. You'd need to give it like this:

          AddLinkLabel(myFormInstance, 20, 20, 100, 30, "Click here");

          BTW - why did you not encode your code in C#? I told you to do it before.

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            Of course you've got errors, you've given it a string in place of the form instance. You'd need to give it like this:

            AddLinkLabel(myFormInstance, 20, 20, 100, 30, "Click here");

            BTW - why did you not encode your code in C#? I told you to do it before.

            Forgive your enemies - it messes with their heads

            My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

            A Offline
            A Offline
            Arunkumar Koloth
            wrote on last edited by
            #5

            Thankyou This Time I Dont Get Any Errors The Program Executed Well But I Cant See The Label I Created And Also How Can I fire Click Event? And Where Should I Add The Url? This is My Code

            using System;
            using System.Collections.Generic;
            using System.ComponentModel;
            using System.Data;
            using System.Drawing;
            using System.Text;
            using System.Windows.Forms;

            namespace WindowsApplication1
            {
            public partial class Form1 : Form
            {
            public Form1()
            {
            InitializeComponent();
            }

                private void Form1\_Load(object sender, EventArgs e)
                {
                    Form1 frm1=new Form1();
                    AddLinkLabel(frm1, 20, 20, 100, 30, "Click Here");
                }
                public void AddLinkLabel<T>(T containingForm, int top, int left, int width, int height, string link) where T : Form
                {
                    LinkLabel label = new LinkLabel();
                    label.Location = new System.Drawing.Point(top, left);
                    label.Size = new System.Drawing.Size(width, height);
                    containingForm.Controls.Add(label);
                }
            }
            

            }

            P 1 Reply Last reply
            0
            • A Arunkumar Koloth

              Thankyou This Time I Dont Get Any Errors The Program Executed Well But I Cant See The Label I Created And Also How Can I fire Click Event? And Where Should I Add The Url? This is My Code

              using System;
              using System.Collections.Generic;
              using System.ComponentModel;
              using System.Data;
              using System.Drawing;
              using System.Text;
              using System.Windows.Forms;

              namespace WindowsApplication1
              {
              public partial class Form1 : Form
              {
              public Form1()
              {
              InitializeComponent();
              }

                  private void Form1\_Load(object sender, EventArgs e)
                  {
                      Form1 frm1=new Form1();
                      AddLinkLabel(frm1, 20, 20, 100, 30, "Click Here");
                  }
                  public void AddLinkLabel<T>(T containingForm, int top, int left, int width, int height, string link) where T : Form
                  {
                      LinkLabel label = new LinkLabel();
                      label.Location = new System.Drawing.Point(top, left);
                      label.Size = new System.Drawing.Size(width, height);
                      containingForm.Controls.Add(label);
                  }
              }
              

              }

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              You don't need to create a new instance of Form1. In fact, as you have placed this method inside your Form class, you don't even need containingForm, but assuming you are going to move this method into a separate class, your call to AddLinkLabel would look like this:

              AddLinkLabel(this, 20, 20, 100, 30, "Click Here");

              You have now added the link label to the controls collection of the form that's displayed (and not the form you instantiated in Form1 frm = new Form1();.

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

              A 1 Reply Last reply
              0
              • P Pete OHanlon

                You don't need to create a new instance of Form1. In fact, as you have placed this method inside your Form class, you don't even need containingForm, but assuming you are going to move this method into a separate class, your call to AddLinkLabel would look like this:

                AddLinkLabel(this, 20, 20, 100, 30, "Click Here");

                You have now added the link label to the controls collection of the form that's displayed (and not the form you instantiated in Form1 frm = new Form1();.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                A Offline
                A Offline
                Arunkumar Koloth
                wrote on last edited by
                #7

                Thankyou For That It Work on a form load event I Writed The Same Code to A Class File To Create a library for link label but it faild there are so many errors displaying this is my class library code

                using System;
                using System.Collections.Generic;
                using System.ComponentModel;
                using System.Data;
                using System.Drawing;
                using System.Text;
                using System.Net;
                using System.Windows.Forms;

                namespace linklabel
                {
                public class LinkLabel
                {

                    public void AddLinkLabel<T>(T containingForm, int top, int left, int width, int height, string Text, string link) where T : Form
                    {
                        LinkLabel label = new LinkLabel();
                        label.Location = new System.Drawing.Point(top, left);
                        label.Size = new System.Drawing.Size(width, height);
                        label.Text = Text;
                        containingForm.Controls.Add(label);
                
                
                    }
                }
                

                }

                is it possible to create a library for a control?

                P 1 Reply Last reply
                0
                • A Arunkumar Koloth

                  Thankyou For That It Work on a form load event I Writed The Same Code to A Class File To Create a library for link label but it faild there are so many errors displaying this is my class library code

                  using System;
                  using System.Collections.Generic;
                  using System.ComponentModel;
                  using System.Data;
                  using System.Drawing;
                  using System.Text;
                  using System.Net;
                  using System.Windows.Forms;

                  namespace linklabel
                  {
                  public class LinkLabel
                  {

                      public void AddLinkLabel<T>(T containingForm, int top, int left, int width, int height, string Text, string link) where T : Form
                      {
                          LinkLabel label = new LinkLabel();
                          label.Location = new System.Drawing.Point(top, left);
                          label.Size = new System.Drawing.Size(width, height);
                          label.Text = Text;
                          containingForm.Controls.Add(label);
                  
                  
                      }
                  }
                  

                  }

                  is it possible to create a library for a control?

                  P Offline
                  P Offline
                  Pete OHanlon
                  wrote on last edited by
                  #8

                  Arunkumar.Koloth wrote:

                  is it possible to create a library for a control?

                  Yes, but you have to remember to add the right references to the class library. Here's a hint for you for the future; when I want to create a library for a forms application like this I would normally create it as a windows forms application, remove the generated forms, and change the output type to class library. By doing this, I've got all the basic references that I need. From this answer, you can tell that what you need to do is add in the missing references. Good luck:thumbsup:

                  Forgive your enemies - it messes with their heads

                  My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                  A 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Arunkumar.Koloth wrote:

                    is it possible to create a library for a control?

                    Yes, but you have to remember to add the right references to the class library. Here's a hint for you for the future; when I want to create a library for a forms application like this I would normally create it as a windows forms application, remove the generated forms, and change the output type to class library. By doing this, I've got all the basic references that I need. From this answer, you can tell that what you need to do is add in the missing references. Good luck:thumbsup:

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                    A Offline
                    A Offline
                    Arunkumar Koloth
                    wrote on last edited by
                    #9

                    Thankyou

                    Arunkumar.T

                    P 1 Reply Last reply
                    0
                    • A Arunkumar Koloth

                      Thankyou

                      Arunkumar.T

                      P Offline
                      P Offline
                      Pete OHanlon
                      wrote on last edited by
                      #10

                      You're welcome.

                      Forgive your enemies - it messes with their heads

                      My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility

                      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