简体   繁体   中英

Displaying results in table of data in a ListBox from TextBoxes

I am doing an assignment on the population of organisms where there are three TextBoxes in my application, that would get the user's input on the "Starting number of organisms" (StartingNumOfOrganismsTextBox), "Average daily increase" (DailyIncreaseTextBox), and "Days to multiply" (NumOfDaysTextBox).

        int NumOfOrganisms = 0, DailyIncrease = 0, NumOfDays = 1;

        StartingNumOfOrganisms = int.Parse(StartingNumOfOrganismsTextBox.Text);
        DailyIncrease = int.Parse(DailyIncreaseTextBox.Text);
        NumOfDays = int.Parse(NumOfDaysTextBox.Text);

When the user inputs int numbers in those textboxes, there should be a Calculate Button, when pressed, it should automatically display the users inputs into a separate ListBox named (PopulationsListBox), as a table of data like this:

For example, if a user enters the following inputs in the TextBoxes mentioned: StartingNumOfOrganismsTextBox: 2 DailyIncreaseTextBox: 30% NumOfDaysTextBox: 5

Pressing the calculate button, the application should display the following table of data in a ListBox control in two columns. (Day) column, and (Approximate Population) column.

Day 1, Approximate Population 2. Day 2, Approximate Population 2.6. Day 3, Approximate Population 3.38. Day 4 Approximate Population 4.394. Day 5, Approximate Population 5.7122.

Will someone give me hints on how to take all three of the user inputs, (StartingNumOfOrgranisms, DailyIncrease[%] and then the NumOfDays the organisms will be left to multiply, and display the data of table ina ListBox control? This is very confusing for me, I will be extremely grateful if anyone could help me with this assignment. Thanks.

Also, I have tried to use the ListView code format to add my data:

        PopulationsListBox.Items.Add("1");
        PopulationsListBox.Items[0].SubItems.Add(StartingNumOfOrganisms.ToString());
        for (int i=2;i<=NumOfDays;++i)
        {
           PopulationsListBox.Items.Add(i.ToString());
           PopulationsListBox.Items[PopulationsListBox.Items.Count-1].SubItems.Add((StartingNumOfOrganisms+StartingNumOfOrganisms*DailyIncrease).ToString());

But, "SubItems" is not a property ListBoxes use. Perhaps someone could suggest trying something similar to that for me? I will be thankful.

I haven't tested this but you want to do something like this:

        listbox.Items.Add("Day 1, Approximate Population:" + txtStartingNumberOfOrganisms.Text);
        int ApproximatePopulation = Convert.ToInt16(txtStartingNumberOfOrganisms.Text);             

        for (int i = 2; i <= numOfDays; i++)
        {
            ApproximatePopulation = ApproximatePopulation + ((ApproximatePopulation * Convert.ToDouble(txtDailyIncrease.text)) / 100);
            listbox.Items.Add("Day " + Convert.ToString(i) + ", Approximate Population:" + Convert.ToString(ApproximatePopulation));
        }

Apologies if the sums are all over the place but you can figure those out ;)

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