简体   繁体   English

更改文本框的字体样式

[英]changing font style of textbox

I want to change font style for a textbox when I click the button.For this my code is below and it is okay; 单击按钮时,我想更改文本框的字体样式。为此,我的代码在下面,没关系;

 protected void Button1_Click(object sender, System.EventArgs e)  
{  
    TextBox1.Font.Size = FontUnit.XLarge;  
    TextBox1.ForeColor = System.Drawing.Color.Crimson;  
    TextBox1.BackColor = System.Drawing.Color.Snow;  
    TextBox1.BorderColor = System.Drawing.Color.HotPink;  

}  

But Can I do where I select of write which is in the textbox it changes only that part.For example textbox1.Text="Computer Programs" and the user select only "Computer" part of textbox1.Only "Computer" part must be changed. 但是我可以在文本框中选择要写入的位置吗?它仅更改该部分。例如,textbox1.Text =“ Computer Programs”,而用户仅选择textbox1的“ Computer”部分。仅“ Computer”部分必须更改。

From the sounds of it you want to format only part of the text? 您想从声音中仅格式化部分文本吗? For that you'll need to look at a RichTextBox control. 为此,您需要查看RichTextBox控件。

With a RichTextBox you can make use of text selections and set formatting on only those areas: 使用RichTextBox您可以使用文本选择并仅在这些区域上设置格式:

RichTextBox rich = new RichTextBox();
rich.Text = "Here is some text for the Rich Text Box";
rich.SelectionStart = 0;
rich.SelectionLength = 4;
rich.SelectionFont = new Font(rich.Font, FontStyle.Bold);

After you're done setting your font to your selection you should set the font immediately afterwards back to the original so that you don't continue the style outside the area you wanted to apply it: 在将字体设置为所选内容之后,应立即将字体设置回原始字体,以免在想要应用的区域之外继续使用该样式:

rich.SelectionStart = rich.SelectionStart + rich.SelectionLength;
rich.SelectionLength = 0;
rich.SelectionFont = rich.Font;

This should result in "Here is some text for the Rich Text Box" changing to look like: " Here is some text for the Rich Text Box". 这将导致“此处为RTF文本框的一些文本”更改为如下所示:“ 是对于RTF文本框的一些文本”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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