简体   繁体   中英

C# integers are not adding successfully to list box

I have a system which includes adding integers to list box. However, when I am inputting a number into the text box and then clicking 'insert integer' the integer is not adding to the list box and the error message is being displayed instead saying 'number already exists in file'. This is my code for the Add integer button -

private void btnInsert_Click(object sender, EventArgs e)
{
    int acceptedNum = 0;
    if (txtInsert.Text != "")
    {
        if (lstIntegers.Items.Contains(txtInsert.Text))
        {
            if (!(int.TryParse(txtInsert.Text, out acceptedNum) && acceptedNum < 0 || acceptedNum >= 100))
            {
                lstIntegers.Items.Add(txtInsert.Text);
                txtInsert.Clear();
                txtInsert.Focus();
                bubbleSort();

            }
            else
            {
                MessageBox.Show("Please input value between 1-100", "error", MessageBoxButtons.OK);
                txtInsert.Text = "";
            }
        }
        else
        {
            MessageBox.Show("Number already exists in list", "error", MessageBoxButtons.OK);
        }
    }
    else
    {
        MessageBox.Show("Please input value between 1-100", "error", MessageBoxButtons.OK);
    }
    if (lstIntegers.Items.Count == 30)
    {
        MessageBox.Show("Maximum number of entries exceeded", "error", MessageBoxButtons.OK);
        //button enabled was false however couldn't then add another 
        btnInsert.Enabled = true;
    }
}

any suggestions?? thank you

Haley I have shown you something that you can add to avoid having to do all the checks, If you are checking something in a conditional statement for example, if the txtInsert.Text; is empty, check that first, then return; if you are learning and starting out, it's not bad to create individual if(){} statements for readability. look at how I made changes and pay attention to the new things I added, use C# MSDN documentation for the examples and explanation. I hope this helps.

private void btnInsert_Click(object sender, EventArgs e)
{
    if (!string.IsNullOrEmpty(txtInsert.Text))
    {
        if (lstIntegers.Items.Contains(txtInsert.Text))
        {
            MessageBox.Show("Number already exists in list", "error", MessageBoxButtons.OK);
            txtInsert.Text = string.Empty;
            txtInsert.Focus();
            return;
        }
        else
        {
            var x = Convert.ToInt32(txtInsert.Text);
            if (Enumerable.Range(1,100).Contains(x))
            {
                lstIntegers.Items.Add(txtInsert.Text);
                txtInsert.Clear();
                txtInsert.Focus();
                bubbleSort();
            }
            else
            {
                MessageBox.Show("Please input value between 1-100", "error", MessageBoxButtons.OK);
                txtInsert.Text = string.Empty;
                txtInsert.Focus();
                return;
            }
        }

    }
    else
    {
        MessageBox.Show("Please input value between 1-100", "error", MessageBoxButtons.OK);
        txtInsert.Text = string.Empty;
        return;
    }

    if (lstIntegers.Items.Count == 30)
    {
        MessageBox.Show("Maximum number of entries exceeded", "error", MessageBoxButtons.OK);
        //button enabled was false however couldn't then add another 
        btnInsert.Enabled = true;
    }
}   

此行不应该不是NOT,以便在不存在的情况下添加该项目吗?

if (lstIntegers.Items.Contains(txtInsert.Text))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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