user1414001
user1414001

Reputation: 31

Using a textbox or label like a button in c# visual studio

Is it possible to use a label or textbox in the same way you would use a button in a c# visual studio document?

i.e. can it be selected and have a result similiar to a button being clicked?

Upvotes: 3

Views: 6142

Answers (4)

McAfeeJ
McAfeeJ

Reputation: 45

It would be best to use a link label. That way whenever the mouse hovers over it, the person knows that clicking on it causes an action.

    private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
      {
        MessageBox.Show("you have clicked a link label");
        //or whatever action you want it to do.
      }

Upvotes: 4

Writwick
Writwick

Reputation: 2163

For Click Event :

You can use the Label.Click event or TextBox.Click event


For Event when Some Text is selected in the TextBox :

Although there is no Special Event For this, You can Utilize the TextBox.MouseUp event like this :

private void txtBox_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) 
{
    TextBox txtbx = sender as TextBox;
    if (txtbx != null)
    {
        if (txtbx.SelectionLength > 0)
        {
            string seltxt = txtbx.SelectedText;
            //Do Work Here with 'seltxt' variable!
        }
    }
}

Upvotes: 2

Ceramic Pot
Ceramic Pot

Reputation: 280

They have the Click event also.

Upvotes: 0

blockloop
blockloop

Reputation: 5735

I guess you could use anything as a button if you wanted to, but why would you want to rather than using a button?

http://msdn.microsoft.com/en-us/library/system.windows.forms.label_events

Everything that is a Control object in WinForms has a "Click" event that can be used. Subscribe to that event with a custom method and do what you want to do in that event. If you want the label to have a button look-and-feel I'd suggest putting a border and some hover, press and release decorations using the appropriate events.

Hope this helps.

Upvotes: 1

Related Questions