在文本框中键入时,单词开头的自动大写字母

时间:2014-10-22 15:44:45

标签: c# winforms vb.net-to-c#

我想在文本框中输入时创建一个函数,每个句子的开头会自动变大。

我尝试使用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 = False         TextEdit1.Select(current_point,0)     结束子

4 个答案:

答案 0 :(得分:0)

你的问题有点困惑,但据我所知,你试图自动大写文本框中输入的任何内容的第一个字母。

以下是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将是您表单上声明的私人bool

public void MyTextbox_TextChanged(object sender, EventArgs e)
{
    if (!_isCapitalizaingMyTextbox)
    {
        _isCapitalizaingMyTextbox = true;
        MyTextbox.Text = AutoCapitalize(MyTextbox.Text);
        _isCapitalizaingMyTextbox = false;
    }
}

编辑:基于MikeH评论我添加了一些代码以防止TextChanged处理程序中的无限循环

答案 1 :(得分:0)

如果您将以下代码放入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;
}

答案 2 :(得分:0)

我想我明白你在找什么。您希望文本框自动将每个句子的第一个字母大写。我有一个解决方案,应该符合您的预期愿望。首先,您必须创建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);                    
        }

    }

}

这将监视在文本框中输入的所有字符,并按句点标识句子的结尾。为标点符号添加更多字符会很容易。我还将用户的第一个字符合并为enter以保持一致性。这是支持功能:

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;

答案 3 :(得分: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);
}