开发者

Character count using cursor in C#

开发者 https://www.devze.com 2023-02-10 14:21 出处:网络
I am attempting to count the characters in a text area using the cursor in C#. For example, I have a string of characters in the text area: \"Please count my characters\". If I highlight \" charact开发

I am attempting to count the characters in a text area using the cursor in C#. For example, I have a string of characters in the text area: "Please count my characters". If I highlight " charact开发者_如何学Cers", it should tell me that it has 11 characters including the whitespace.

Thanks who viewed and answered my inquiry.


In Winforms, just call textbox.SelectedText.Length. Same goes for WPF


private void richTextBox1_SelectionChanged(object sender, EventArgs e)
{
        label1.Text = richTextBox1.SelectedText.Length.ToString();
}

EDIT:

for textboxes use this but I will prefer using richTextbox

textBox1.SelectionLength.ToString();
0

精彩评论

暂无评论...
验证码 换一张
取 消