Connecting Tech Pros Worldwide Forums | Help | Site Map

VB.NET Select Text in Label

Newbie
 
Join Date: Sep 2008
Posts: 7
#1: Sep 30 '08
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's Avatar
Expert
 
Join Date: May 2007
Location: Illinois
Posts: 663
#2: Sep 30 '08

re: VB.NET Select Text in Label


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

Nathan
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#3: Sep 30 '08

re: VB.NET Select Text in Label


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
Expert
 
Join Date: Sep 2008
Location: USA
Posts: 188
#4: Sep 30 '08

re: VB.NET Select Text in Label


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 ContextMenuStrip to the label with a 'Copy' ToolStripItem whose click event is handled by a simple method which copies the Label.Text to the Clipboard.

Expand|Select|Wrap|Line Numbers
  1.  
  2. ///<summary>ContextMenu to allow user to copy label text to clipboard.</summary>
  3. ContextMenuStrip createCopyMenu(EventHandler specificAction) {
  4.   ContextMenuStrip contextMenu = new ContextMenuStrip();
  5.   ToolStripItem copyCmd = contextMenu.Items.Add("Copy");
  6.   copyCmd.Click += specificAction;
  7.   return contextMenu;
  8. }
  9.  
  10. ///<summary>Copy Label text to clipboard when ContextMenu.Copy is clicked.</summary>
  11. void copyAddressCmd_Click(object sender, EventArgs e) {
  12.   Clipboard.SetDataObject(myAddressLabel.Text, false);
  13. }
  14.  
  15. myAddressLabel.ContextMenuStrip = createCopyMenu(copyAddressCmd_Click);
  16.  
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.)
Newbie
 
Join Date: Sep 2008
Posts: 7
#5: Oct 1 '08

re: VB.NET Select Text in Label


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.SetDataObject(AddressLbl.Text, False)

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


Thanks anyway :)
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#6: Oct 1 '08

re: VB.NET Select Text in Label


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

Thanks

-Frinny
Reply