简体   繁体   English

将字符串值添加到数组

[英]Adding String Values to an Array

I have this piece of code and this may seem like a very stupid question but it is they way I was taught to do it and it is not working correctly for me. 我有这段代码,这似乎是一个非常愚蠢的问题,但这是他们被教给我的方式,而且对我而言它无法正常工作。

I wanted it to be that when I entered text into the txtEnterWord text box it would be added to the array words and displayed in the list box lstWords . 我希望当我在txtEnterWord文本框中输入文本时,它将被添加到数组words并显示在列表框lstWords

But every time I go to add a second word it clears the previous word and replaces it with the new one. 但是,每次我添加第二个单词时,它都会清除前一个单词,并将其替换为新单词。 Anyone know how to fix this? 有人知道怎么修这个东西吗? :) :)

 private void btnEnter_Click(object sender, EventArgs e)
        {
            //string[] words = new string[6];
            //words[6] = txtEnterWord.Text;

            //for (int i = 0; i < words.Length; i++)
            //{
            //    lstWords.Items.Add(words[i]);
            //}

            lstWords.Items.Clear();
            string[] words = new string[6];

            //words[0] = txtEnterWord.Text;
            //words[1] = txtEnterWord.Text;
            //words[2] = txtEnterWord.Text;
            //words[3] = txtEnterWord.Text;
            //words[4] = txtEnterWord.Text;
            //words[5] = txtEnterWord.Text;

            for (int i = 0; i < words.Length;i++)
            {
                //words[i] = txtEnterWord.Text;
                lstWords.Items.Add(txtEnterWord.Text);
                txtEnterWord.Clear();
                //lstWords.ToString() = lstWords.ToString() + words[i].ToString();
            }

You're clearing the array every time you enter btnEnter_Click method, you should declare int as a variable for your page, like a "global" variable and in the btnEnter_Click method you should only add the new value. 每次输入btnEnter_Click方法时都要清除数组,应将int声明为页面的变量,例如“全局”变量,并且在btnEnter_Click方法中应仅添加新值。 Sorry for bad english. 对不起,英语不好。

It should just be the below code. 它应该只是下面的代码。 No need for a for loop and you shouldn't be clearing lstWords.Items because that is why your data is disappearing. 不需要for循环,并且您不应该清除lstWords.Items,因为这就是您的数据消失的原因。 Just add the textbox Text to your list and then clear the textbox. 只需将文本框“文本”添加到列表中,然后清除文本框即可。 You also don't need the string[] words because it is doing nothing as a local variable. 您也不需要string []单词,因为它作为局部变量没有任何作用。 If you really wanted to add it to a string[] then it would need to be a member of the class this method is in. I think just doing lstWords.Items.Add should be good though if that's where you need the text to be saved. 如果您真的想将其添加到string []中,那么它将需要是该方法所在的类的成员。我认为仅做lstWords.Items.Add应该会很好,但是如果那是您需要在其中添加文本的地方已保存。

private void btnEnter_Click(object sender, EventArgs e)
{
    lstWords.Items.Add(txtEnterWord.Text);
    txtEnterWord.Clear();
}
private void button1_Click(object sender, EventArgs e)
{
    string words;
    words = textBox1.Text;
    listBox1.Items.Add(words);
    textBox1.Clear();
}

If you are trying to add continuously a new item to the list, you should not clear the lstWords.Items if you want to keep them visually. 如果试图将新项目连续添加到列表中,则如果要保持视觉效果,则不应清除lstWords.Items I recommend you to create a List<string> myWordsToDisplay = new List<string>() globaly and add an item to the list at the time you click on the button add. 我建议您创建一个List<string> myWordsToDisplay = new List<string>()全局,并在单击添加按钮时将一个项目添加到列表中。

You should have your logic of clearing the list box on the event "OnPaint" or "OnLoad" of your application that depends clearly on which kind of project you have, something like this 您应该具有清除应用程序事件“ OnPaint”或“ OnLoad”上的列表框的逻辑,该事件明显取决于您所拥有的项目类型,例如:

OnPaint(EventArgs e)
{
    lstWords.Items.Clear();
    lstWords.Items.AddRange(myWordsToDisplay); //if the AddRange method is not present is the same thing as this myWordsToDisplay.ForEach(x => lstWords.Items.Add(x)
}

got the idea? 有主意吗?

If you want every item to add to the listbox you should NOT call the method beneath. 如果要将每个项目添加到列表框中,则不应调用下面的方法。 This function will clear all the items you have currently in the list thus making it appear empty. 此功能将清除列表中当前包含的所有项目,从而使其显示为空。

lstWords.Items.Clear();

Instead you should just add the text from the textbox (txtEnterWord) to the listbox items. 相反,您应该只将文本框(txtEnterWord)中的文本添加到列表框项中。

 lstWords.Items.Add(txtEnterWord.Text);

If you want the value to also be stored somewhere else you should make this variable outside the button click function. 如果要将值也存储在其他位置,则应在按钮单击功能之外设置此变量。 If you want to be able to easily add items to an array, consider using a list. 如果您希望能够轻松地将项目添加到数组,请考虑使用列表。 More information can be found here: https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx 可以在这里找到更多信息: https : //msdn.microsoft.com/zh-cn/library/6sh2ey19(v=vs.110).aspx

Your complete code should look something like this. 您的完整代码应如下所示。

private List<string> words = new List<string>();
private void btnEnter_Click(object sender, EventArgs e)
{
     words.Add(txtEnterWord.Text);
     lstWords.Items.Add(txtEnterWord.Text);
     txtEnterWord.Clear();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM