在文本框中键入时,单词开头的自动大写字母
本文关键字:开头 单词 大写字母 文本 | 更新日期: 2023-09-27 18:27:38
我想创建一个函数,当在文本框中键入时,每个句子的开头都会自动变大。
我一直在尝试用VB.Net制作,但没有成功,但当我尝试用C#时,无法正常运行。
我不知道我的错误在哪里,我希望你们中的任何人都能帮助改进我的C#代码,谢谢。
我在C#类中的函数(失败):
class ClsText
{
public int Current_Point = 0;
public bool Remove_Handle = false;
public string tulis(string text)
{
string[] validasi_char = new string[] { " ", ".", "(", ")", "!", "@", "$", "%", "&", "*", "/", "?", "+", "-", ",", ">", "<", "'", "~", "`" };
string str_temp = "";
foreach (string vchar in validasi_char)
{
string[] split_temp = text.Split(validasi_char,StringSplitOptions.);
str_temp = "";
foreach (string txt in split_temp)
{
str_temp = str_temp + txt.Substring(0,0).ToUpper() + txt.Substring(1, txt.Length) + vchar;
}
text = str_temp.Substring(0, str_temp.Length - 1);
}
text = text.Substring(0, 0).ToUpper() + text.Substring(1, text.Length);
return text;
}
}
我在C#文本框中的代码(失败):
ClsText asd = new ClsText();
if (asd.Remove_Handle == true)
{
asd.Current_Point = textEdit1.SelectionStart;
asd.Remove_Handle = true;
textEdit1.Text = asd.tulis(textEdit1.Text);
}
textEdit1.Select(asd.Current_Point, 2);
我在VB.NET中的函数(成功):
Public current_point As Integer = 0
Public remove_handle As Boolean = False
Public Function Tulis(ByVal Text As String) As String
Dim validasi_char() As String = {" ", ".", "(", ")", "!", "@", "$", "%", "&", "*", "/", "?", "+", "-", ",", ">", "<", """", "'", "~", "`"}
Dim str_temp As String = ""
For Each vchar In validasi_char
Dim split_temp() As String = Split(Text, vchar)
str_temp = ""
For Each txt In split_temp
str_temp = str_temp + Mid(txt, 1, 1).ToUpper + Mid(txt, 2, txt.Length) + vchar
Next
Text = Mid(str_temp, 1, str_temp.Length - 1)
Next
Text = Mid(Text, 1, 1).ToUpper + Mid(Text, 2, Text.Length)
Return Text
End Function
我在VB.Net文本框中的代码(成功):
Private Sub TextEdit1_EditValueChanging(sender As Object, e As DevExpress.XtraEditors.Controls.ChangingEventArgs) Handles TextEdit1.EditValueChanging
If remove_handle = True Then GoTo DoCurrentPoint
current_point = TextEdit1.SelectionStart
remove_handle = True
TextEdit1.Text = Tulis(TextEdit1.Text)
DoCurrentPoint:remove_handle=错误文本编辑1.选择(current_point,0)结束子
使用CurrentCulture.TextInfo 中的ToTitleCase
public void textB_TextChanged(object sender, EventArgs e)
{
MyTextbox.Text = ToTitleCase(MyTextbox.Text);
}
public static string ToTitleCase(string stringToConvert)
{
return Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(stringToConvert);
}
我想我明白你在找什么。您希望文本框自动将每个句子的第一个字母大写。我有一个解决方案,应该能满足你的预期愿望。首先,您必须创建OnTextBoxChanged事件:
private void OnTextChanged(object sender, EventArgs e)
{
TextBox myTextArea = (sender as TextBox);
if (myTextArea.Text.Length > 3)
{
char period = myTextArea.Text.ElementAtOrDefault(myTextArea.Text.Length - 3);
char space = myTextArea.Text.ElementAtOrDefault(myTextArea.Text.Length - 2);
char newestCharAdded = myTextArea.Text.Last();
if(period == '.' && space == ' ' && char.IsLetter(newestCharAdded))
{
CapitalizeFunction(myTextArea, newestCharAdded);
}
}
else if (myTextArea.Text.Length == 1)
{
char newestCharAdded = myTextArea.Text.Last();
if(char.IsLetter(newestCharAdded))
{
CapitalizeFunction(myTextArea, newestCharAdded);
}
}
}
这将监视文本框中输入的所有字符,并通过句点识别句子的结尾。在标点符号中加入更多的字符是很容易的。为了保持一致性,我还加入了用户输入的第一个字符。以下是支持功能:
private void CapitalizeFunction(TextBox myTextArea, char newestCharAdded)
{
// Take out the lowercase letter
myTextArea.Text = myTextArea.Text.Remove(myTextArea.Text.LastIndexOf(newestCharAdded), 1) + char.ToUpper(newestCharAdded);
// Returns the focus to the end of the string
myTextArea.SelectionStart = myTextArea.Text.Length;
myTextArea.SelectionLength = 0;
}
这段代码的好处在于,只需更改适当的类型,就可以将其用于RichTextBox。现在你有了这个事件,你只需要把它绑在想要的文本框上,如下所示:
MyTextBox.TextChanged += OnTextChanged;
您的问题有点困惑,但据我所知,您试图自动大写文本框中键入的任何内容的第一个字母。
以下是c#中一个函数的一些代码,该函数接受一个字符串并返回一个格式化为的字符串
public string AutoCapitalize(string text)
{
return string.IsNullOrEmpty(text) ? "" : text.Length > 1 ? text[0].ToString().ToUpper() + text.Substring(1) : text[0].ToString().ToUpper();
}
然后在你的文本框的TextChanged事件上,你可以这样称呼它:
编辑:_isCapitalizaingMyTextbox将是在表单上声明的私有布尔
public void MyTextbox_TextChanged(object sender, EventArgs e)
{
if (!_isCapitalizaingMyTextbox)
{
_isCapitalizaingMyTextbox = true;
MyTextbox.Text = AutoCapitalize(MyTextbox.Text);
_isCapitalizaingMyTextbox = false;
}
}
编辑:基于MikeH的评论,我添加了一些代码来防止TextChanged处理程序中出现无休止的循环
如果将以下代码放入TextBox的TextChanged
事件处理程序中,它将为您提供所需的结果:
Private Sub YourTextBox_TextChanged(sender As Object, e As EventArgs)
Dim Pos = YourTextBox.SelectionStart
Dim Len = YourTextBox.SelectionLength
YourTextBox.Text = System.Text.RegularExpressions.Regex.Replace(YourTextBox.Text, "(?<G1>[,.,(,),!,,$,%,&,*,/,?,+,-,,,>,<,,',~,`] *)(?<G2>[a-z])", Function(m) m.Groups(1).Value & m.Groups(2).Value.ToUpper())
YourTextBox.SelectionStart = Pos
YourTextBox.SelectionLength = Len
End Sub
在C#中:
private void YourTextBox_TextChanged(object sender, EventArgs e)
{
var Pos = YourTextBox.SelectionStart;
var Len = YourTextBox.SelectionLength;
YourTextBox.Text = System.Text.RegularExpressions.Regex.Replace(YourTextBox.Text, "(?<G1>[,.,(,),!,,$,%,&,*,/,?,+,-,,,>,<,,',~,`] *)(?<G2>[a-z])", m => m.Groups(1).Value + m.Groups(2).Value.ToUpper());
YourTextBox.SelectionStart = Pos;
YourTextBox.SelectionLength = Len;
}