C# 如何大写每个句子的第一个字母,并让用户有机会将字母改为小写?
我在Interpunction之后实现了大写。但是如何实现它,用户可以返回并删除第一个单词或字符,因为他想继续使用小写C# 如何大写每个句子的第一个字母,并让用户有机会将字母改为小写?,c#,console-application,capitalize,sentence,C#,Console Application,Capitalize,Sentence,我在Interpunction之后实现了大写。但是如何实现它,用户可以返回并删除第一个单词或字符,因为他想继续使用小写 KeyPress(object sender, KeyPressEventArgs e) { if(EndOfSentence()) { e.KeyChar = Char.ToUpper(e.Keychar); } } // private bool EndOfSentence() { //return true if end of sent
KeyPress(object sender, KeyPressEventArgs e)
{
if(EndOfSentence())
{
e.KeyChar = Char.ToUpper(e.Keychar);
}
}
//
private bool EndOfSentence()
{
//return true if end of sentence found
}
如果我写这个句子。我不能回去把I换成I!我不能把A改成A,但我想!如何编写此代码
此处的示例项目:
我看到的唯一解决方案是保存当前和上一个键,并检查是否按了backspace或delete键,如下所示:
if (!EndOfSentence())
{
previousKeyChar = e.KeyChar;
return;
}
//
if(previousKeyChar.Equals('\b')) return;
else
e.KeyChar = Char.ToUpper(e.KeyChar);
//
//
// And in the EndOfSentence I Check
// if the cursor is at the end of the text
if(textbox1.Text.Length != textbox1.SelectionStart)
return false; //allow editing in the middle of the text
可能有更好的方法,但有一种方法是将整个句子存储在集合中,并在再次修改之前对照集合进行检查 或者您可以尝试更改EndOfSentence函数,使其仅在输入结束时返回true。这样,它应该只利用输入结束时键入的字符,如果你在文本中间编辑一些东西,它应该保持不变。可能有更好的方法,我恳求这样一种解决方案,而不需要一个集合来检查。如何在MS Office Word或Outlook中解决此问题?我以前尝试过此方法,但如果用户编写一个句子并编辑其结尾,则失败。这是一个句子。A | nd如果用户返回,她可以将A更改为A。但如果她尝试将下面的A改为A,则此操作将失败。A |@user1338270没有,这是一个句子。你能再解释一遍吗?让|成为光标。让我之前的全部评论作为测试案例。如果我们在输入的末尾,那么只让endofcentence返回true。然后用户可以将A | nd更改为A | nd。但用户无法将A改为A。