如何让文本框中的一个词结束一个程序?

How do I make one word in a text box end a program?

所以我必须创建的表单之一是输入名字和姓氏,然后将这两个名字分开并将它们放在适当的标签旁边,表单设计:https://gyazo.com/9b34dca0c1cd464fd865830390fcb743 但是当以任何方式输入单词停止,例如Stop、StOp、sToP 等它需要结束。

        private void btnSeparate_Click(object sender, EventArgs e)
    {
        string strfullname, strgivenname, strfamilyname, strfirstname;
        int int_space_location_one, int_space_location_two;

        strfullname = txtFullName.Text;


        int_space_location_one = strfullname.IndexOf(" ");
        strgivenname = strfullname.Substring(0, int_space_location_one);


        lblGivenEntered.Text = strgivenname;


        strfirstname = strfullname.Remove(0, int_space_location_one + 1);
        int_space_location_two = strfirstname.IndexOf(" ");


        strfamilyname = strfirstname.Substring(int_space_location_two + 1);
        lblFamilyEntered.Text = strfamilyname;
    }

这是我当前的代码,我尝试了很多不同的方法来让单词停止来结束它,但它不起作用,所以这就是为什么目前没有代码试图停止程序,我遇到的主要问题是因为它正在搜索名字和姓氏之间的 space 并且它显然没有一对一的单词它只是崩溃了。

在此先感谢您的任何帮助。

只需连接 TextChanged 事件并像这样进行:

private void TextChanged(Object sender, EventArgs e)
{
    // If text, converted to lower-characters contains "stop" -> Exit
    if(txtFullName.Text.ToLower().Contains("stop"))
    {
        // What I understand as "stopping it".
        Application.Exit();
    }
}

IF with "stop it" 你的意思是取消操作:

private void btnSeparate_Click(object sender, EventArgs e)
{
    // If text, converted to lower-characters contains "stop" -> Exit
    if (txtFullName.Text.ToLower().Contains("stop"))
    {
        // What I understand as "stopping it".
        Application.Exit();
    }
    else
    {
        // Your code inside the else block
    }
}

所有内容的简短版本: 还涵盖了 no spaces 问题

private void btnSeparate_Click(object sender, EventArgs e)
{
    // Save how many words are inside
    int wordsInText = txtFullName.Text.Split(' ').Length;
    // Save if "stop" was typed into the textbox
    bool stopExisting = !txtFullName.Text.ToLower().Contains("stop");

    // If text has exactly 3 words and "stop" is NOT existing
    if (wordsInText == 3 && !stopExisting)
    {
        // Save array of splitted parts
        string[] nameParts = txtFullName.Text.Split(' ');

        // This is never used??
        string strfirstname = nameParts[1];

        // Set name-parts to labels
        lblGivenEntered.Text = nameParts[0];
        lblFamilyEntered.Text = nameParts[2];
    }
    // If text has NOT exactly 3 words and "stop" is NOT existing
    else if(wordsInText != 3 && !stopExisting)
    {
        // If there are no 3 words, handle it here - MessageBox?
    }
    // If "stop" IS existing
    else if(stopExisting)
    {
        // If text contains "stop" handle it here
        // Application.Exit(); <-- if you really want to exit
    }
}

您可以检查输入的单词之一是否等于单词 "stop"。在那里你需要一个忽略大小写的 StringComparions 。或者您可以将输入的单词解析为 lower/upper-cases.

因此,如果检查为真,您可以使用

结束程序

Environment.Exit(0);

您可以检查输入的单词之一是否等于单词 "stop"。在那里你需要一个忽略大小写的 StringComparions 。或者您可以将输入的单词解析为 lower/upper-cases.

因此,如果检查为真,您可以使用

结束程序

Environment.Exit(0);

代码:

if (strfullname.ToLowerInvariant().Contains("stop"))
{
    Environment.Exit(0);
}