简体   繁体   中英

Winform image toggle button

Well after a long search on web and downloading few projects I landed here for an overall easy to use and small code to implement a toggle button on c# winform project.

I want to have my button to be toggled ie two different image possibly with two different text, even I can make the images to have text on it.

Any quick suggestion?

The usual way is to use a CheckBox with Appearance=Button .

You can toggle its ImageIndex and Text in the CheckedChanged event.

You need to associate it with a well-prepared ImageList of the right ImageSize an ColorDepth .

You can get away with ca 3 lines of code:

private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
    if (checkBox1.Checked)
      { checkBox1.ImageIndex = 1; checkBox1.Text = "Sue"; }
    else
      { checkBox1.ImageIndex = 2; checkBox1.Text = "Ellen"; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM