[英]How can i set label Font in C# with multiple checkbox
我是C Sharp的新手,我已經嘗試了很多設置,但沒有幫助。 我想用一個按鈕,3個復選框和一個標簽創建表單。 如果我勾選,當我單擊“開始”按鈕時,第一個復選框會將我的標簽設置為粗體。
當我檢查后,第二個復選框將我的標簽設置為斜體,並且當我單擊“執行”按鈕時加粗。
最后,當我檢查時,第三個復選框將我的標簽設置為斜體,並加粗並加下划線。
它可以工作,但是當我取消選中復選框時,是否要刪除這些字體設置? 如何刪除它們?
private void btnGo_Click(object sender, EventArgs e)
{
if (cbunderline.Checked == true)
{
lbltext.Font = new Font(lbltext.Font.Name, lbltext.Font.Size, lbltext.Font.Style | FontStyle.Underline);
}
if (cbitalic.Checked ==true )
{
lbltext.Font = new Font(lbltext.Font.Name, lbltext.Font.Size, lbltext.Font.Style | FontStyle.Italic);
}
if (cbbold.Checked==true)
{
lbltext.Font = new Font(lbltext.Font.Name, lbltext.Font.Size, lbltext.Font.Style | FontStyle.Bold);
}
}
要刪除樣式,請始終從基本樣式開始,然后僅添加已選中的樣式:
private void btnGo_Click(object sender, EventArgs e)
{
// start with the base font, then add in each selected style
Font fnt = new Font(lblFontSample.Font.FontFamily, lblFontSample.Font.Size, FontStyle.Regular);
if (cbBold.Checked)
{
fnt = new Font(lblFontSample.Font, fnt.Style | FontStyle.Bold);
}
if (cbItalic.Checked)
{
fnt = new Font(lblFontSample.Font, fnt.Style | FontStyle.Italic);
}
if (cbUnderline.Checked)
{
fnt = new Font(lblFontSample.Font, fnt.Style | FontStyle.Underline);
}
lblFontSample.Font = fnt;
}
private void btnGo_Click(object sender, EventArgs e)
{
if (cbunderline.Checked == true)
{ lbltext.Font = new Font(lbltext.Font.Name, lbltext.Font.Size, lbltext.Font.Style | FontStyle.Underline);
}
if (cbitalic.Checked ==true )
{
lbltext.Font = new Font(lbltext.Font.Name, lbltext.Font.Size, lbltext.Font.Style | FontStyle.Italic);
}
if
(cbbold.Checked==true)
{
lbltext.Font = new Font(lbltext.Font.Name, lbltext.Font.Size, lbltext.Font.Style | FontStyle.Bold);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.