当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Changing color of commented text in textbox in windows application

问题描述:

Here is the code for changing the color of commented query:

private void TextBox1_TextChanged(object sender, EventArgs e)

{

if(TextBox1.Text.startswith("--")||TextBox1.Text.startswith("//"))

{

TextBox1.ForeColor = Color.gray; {need to modify for correct functionality)

}

}

Example: -- select * from tab;

select * from table1

The query getting changed its color, but when I try to write another query below that even the next query coming up with the same gray color,

How to change only particular selected text starts with (-- or // )

网友答案:

If you set Textbox attribute the line breaks are preverved:

In code behind:

private void TextBox1_TextChanged(object sender, EventArgs e)
{
    String[] Lines = TextBox1.Split('\n'); //get all lines
}

But you have to remember that when you set

TextBox1.ForeColor = Color.gray 

all Textbox contents will have gray color (you cannot set it for one line only).

分享给朋友:
您可能感兴趣的文章:
随机阅读: