简体   繁体   中英

How to store user input in a 2D array?

So I have this GUI application that is supposed to calculate shipping costs. First I need to enter the number of packages that is being shipped (already done), then choose package # from a domainUpDown tool(also complete), and then input dimensions for selected package in a text box. This is where I'm stuck. This is what I have thus far.

    const int WEIGHT = 0
    const int NMBR_OF_PROPETIES = 7;
    const int MAX_PACKAGES = 10;
    const double FLAT_RATE = 4.99;
    int numOfPackages, packageNumber;


    double[,] packagesArray = new double[MAX_PACKAGES, NMBR_OF_PROPETIES];

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        //label texts get initialized
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (!int.TryParse(numOfPackagesTextBox.Text, out numOfPackages))
        {
            //message box tells that number cant be parsed
        }
        else if(numOfPackages > 0 && numOfPackages < 10)
        {
            //everything in the program is enabled       
        }
        else
        {
            //message box saying that you can't ship more than 10 packages
        }

        //adding package numbers to the domainUpDown tool
        DomainUpDown.DomainUpDownItemCollection items = packageUpDown.Items;
        for (int packageNumber = 1; packageNumber <= numOfPackages; packageNumber++)
        {
            items.Add(packageNumber);
        }

    }

    private void weightBox_TextChanged(object sender, EventArgs e)
    {
        packagesArray[(int)packageUpDown.SelectedItem, WEIGHT] = Convert.ToInt32(weightBox.Text); 
        //also here goes the rest of the dimension entry
    }//from here i don't know what to do, and im sure that this isn't right..

The thing I'm confused about is how to save entered text if user somehow changes package number from domainUpDown tool.

Have you tried registering to the OnChanged or SelectedItemChanged events of the DomainUpDown ?

Perform saving on the OnChanged event handler.

Example:

     myDomainUpDown.OnChanged += new EventHandler(myDomainUpDown_OnChanged);

     void OnChanged(object sender, EventArgs e) 
      {
          //save all the information you need in whatever collection/file/db you want 
      }

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