简体   繁体   中英

Need a little help on where to put the conversions for the textboxes

I am creating an inventory system and have everything lined up, I just need help converting textboxes to the proper variable type. This is only a portion of the project, I just need one example to go by.

//saves part private void Button1_Click(object sender, EventArgs e) {
if (apMinBox > apMaxBox) { MessageBox.Show("Minimum cannot be greater than the Maximum."); return; }

        if (addPartIHRadio.Checked)
        {
            Inhouse inHouse = new Inhouse((Inventory.Parts.Count + 1), apNameBox, apInvBox, apPpuBox, apMinBox, apMaxBox, int.Parse(apMachineBox));
            Inventory.AddPart(inHouse);
        }
        else
        {
            Outsourced outsourced = new Outsourced((Inventory.Parts.Count + 1), apNameBox, apInvBox, apPpuBox, apMinBox, apMaxBox, apMachineBox);
            Inventory.AddPart(outsourced);
        }
        this.Close();
    }

    private void Button2_Click(object sender, EventArgs e)
    {
        //Closes add part form
        mainFormObject.Show();
        Close();    
    }

If targeting the current .NET Framework eg 4.7 or higher then consider asserting the two TextBox controls have valid types eg

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (int.TryParse(apMinBoxTexBox.Text, out var minBoxResult) && int.TryParse(apMinBoxTexBox.Text, out var maxBoxResult))
            {
                if (maxBoxResult > minBoxResult)
                {
                    MessageBox.Show("Minimum cannot be greater than the Maximum.");
                }
                else
                {
                    // in range
                }
            }
            else
            {
                MessageBox.Show("Please enter numbers");
            }
        }
    }
}

While the above works you could create events eg KeyPress to disallow unwanted values such as no "." or letters etc for a int TextBox or create custom TextBox controls that only accept the type you want.

Then there is Data Annotations which works in Window forms project also.

See also http://www.reza-aghaei.com/dataannotations-validation-attributes-in-windows-forms/

If you want to convert the textboxes to other type, you could refer to Convert Class .

The following is a code example that convert textboxes(string type) to int type.

  private void button1_Click(object sender, EventArgs e)
    {
        int min = Convert.ToInt32(txtMin.Text);
        int max = Convert.ToInt32(txtMax.Text);
        if (min > max)
        {
            MessageBox.Show("Minimum cannot be greater than the Maximum.");
            return;
        }

        if (addPartIHRadio.Checked)
        {
            Inhouse inHouse = new Inhouse((Inventory.Parts.Count + 1), apNameBox, apInvBox, apPpuBox, apMinBox, apMaxBox, int.Parse(apMachineBox));
            Inventory.AddPart(inHouse);
        }
        else
        {
            Outsourced outsourced = new Outsourced((Inventory.Parts.Count + 1), apNameBox, apInvBox, apPpuBox, apMinBox, apMaxBox, apMachineBox);
            Inventory.AddPart(outsourced);
        }
        this.Close();
    }

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