VB.NET Select Text in Label

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlmightyJu
    New Member
    • Sep 2008
    • 7

    VB.NET Select Text in Label

    Hi all,

    I just joined because I've found loads of help on this site but I cant find an answer to this.

    I have a form that i loads data in from a database and stores them in various controls and the address is in a label, is it possible that you can select text in the label just like in a text box and copy the text? I cant find anything that helps :(

    I don't want to change it from a label to a text box since the background color of the form changes depending on a field from the database and the textbox doesn't support a transparent background (i could set the bacground of the textbox to the same as the form but seems silly just to select text!)


    Hope someone can help.
    Julian
  • nateraaaa
    Recognized Expert Contributor
    • May 2007
    • 664

    #2
    Have you tried using the .Text property of the Label control? That should give you the text of the label.

    Nathan

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      If you're developing a web application then you should be able to select the text within the label. If its in a desktop application I don't think this is possible. Maybe provide a button or link that lets the user retrieve the text in these labels...like how nateraaaa was suggesting.

      -Frinny

      Comment

      • mldisibio
        Recognized Expert New Member
        • Sep 2008
        • 191

        #4
        If this is a Windows Form and you want your user to be able to right-click and copy the text, then you can add a ContextMenuStri p to the label with a 'Copy' ToolStripItem whose click event is handled by a simple method which copies the Label.Text to the Clipboard.

        Code:
         
        ///<summary>ContextMenu to allow user to copy label text to clipboard.</summary>
        ContextMenuStrip createCopyMenu(EventHandler specificAction) {
          ContextMenuStrip contextMenu = new ContextMenuStrip();
          ToolStripItem copyCmd = contextMenu.Items.Add("Copy");
          copyCmd.Click += specificAction;
          return contextMenu;
        }
        
        ///<summary>Copy Label text to clipboard when ContextMenu.Copy is clicked.</summary>
        void copyAddressCmd_Click(object sender, EventArgs e) {
          Clipboard.SetDataObject(myAddressLabel.Text, false);
        }
        
        myAddressLabel.ContextMenuStrip = createCopyMenu(copyAddressCmd_Click);
        The above code allows you to apply a context menu to several controls, each with their own EventHandler for clicking on the Copy command.

        (Sorry, just noticed you were looking for VB.Net code. Feel free to post your port if you find it helpful.)

        Comment

        • AlmightyJu
          New Member
          • Sep 2008
          • 7

          #5
          Its a desktop app, I thought about having a right click and copy or a button, was just wondering if its possible to have it selectable as sometimes they want to copy the entire address and others just the postcode.

          Nothing to port really, just added the context strip in the designer and just added

          Clipboard.SetDa taObject(Addres sLbl.Text, False)

          to the handler, I'll just ignore the postcode for now since its not actually that important.


          Thanks anyway :)

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Hey Mldisibio, would you mind turning on your PMs so that I can send you a message :)

            Thanks

            -Frinny

            Comment

            Working...