c# - cara menentukan maximum dan minimum value dari list

Halo semuanya. Jadi disini saya mempunyai tugas dimana saya disuruh membuat project yang menggunakan multiforms.

Pada project saya, memiliki 3 form dan 1 class:

FormMenu, FormAddData, FormMaxMin, dan class TempDate.

Pada FormMenu, isinya hanya

 menuStrip

dan terdapat menu "Add Data" dan "Find Maximum and Minimum".

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Welson_TemperatureResearchMultiForms
{
    public partial class FormMenu : Form
    {
        public List<TempDate> listOfTempDate = new List<TempDate>();

        public FormMenu()
        {
            InitializeComponent();
        }

        private void addDataToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormAddData formAddData = new FormAddData();
            formAddData.Owner = this;
            formAddData.ShowDialog();
        }

        private void fromCertainRangeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormMaxMinRange formMaxMinRange = new FormMaxMinRange();
            formMaxMinRange.Owner = this;
            formMaxMinRange.ShowDialog();
        }
    }
}

Code dari class TempDate:

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Welson_TemperatureResearchMultiForms
{
    public class TempDate
    {
        public double Temp { get; set; }

        public DateTime Date { get; set; }
    }
}

Code dari FormAddData:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Welson_TemperatureResearchMultiForms
{
    public partial class FormAddData : Form
    {
        public FormAddData()
        {
            InitializeComponent();
        }

        private void buttonSubmit_Click(object sender, EventArgs e)
        {
            FormMenu formMenu = (FormMenu)this.Owner;

            DateTime date = dateTimePickerDate.Value.Date;
            double temp = double.Parse(textBoxTemp.Text);

            TempDate tempDate = new TempDate();

            tempDate.Date = date;
            tempDate.Temp = temp;

            formMenu.listOfTempDate.Add(tempDate);
            listBoxInfo.Items.Add(date + "\t" + temp + "°C");
        }
    }
}

Code dari FormMaxMin:

 using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Welson_TemperatureResearchMultiForms
{
    public partial class FormMaxMinRange : Form
    {
        public FormMaxMinRange()
        {
            InitializeComponent();
        }

        private void buttonMaxMin_Click(object sender, EventArgs e)
        {
            FormMenu formMenu = (FormMenu)this.Owner;

            DateTime start = dateTimePickerStart.Value.Date;
            DateTime end = dateTimePickerEnd.Value.Date;
            int highest = 0;
            double max = formMenu.listOfTempDate[0].Temp;
            int lowest = 0;
            double min = formMenu.listOfTempDate[0].Temp;

            for (int i = 0; i < formMenu.listOfTempDate.Count; i++)
            {
                if (formMenu.listOfTempDate[i].Date >= start
                    && formMenu.listOfTempDate[i].Date <= end)
                {
                    if (formMenu.listOfTempDate[i].Temp > max)
                    {
                        highest = i;
                        max = formMenu.listOfTempDate[i].Temp;
                    }

                    if (formMenu.listOfTempDate[i].Temp < min)
                    {
                        lowest = i;
                        min = formMenu.listOfTempDate[i].Temp;
                    }
                }
            }
            listBoxMaxMin.Items.Add("");
            listBoxMaxMin.Items.Add("Lowest temp: " + min + ", on " + formMenu.listOfTempDate[lowest].Date);
            listBoxMaxMin.Items.Add("Highest temp: " + max + ", on " + formMenu.listOfTempDate[highest].Date);
        }

        private void buttonRedisp_Click(object sender, EventArgs e)
        {

        }
    }
}

Nah permasalahannya, pada FormMaxMin, saya menambahkan 2 buah dateTimePicker, yang gunanya untuk membatasi tanggal awal dan batas akhir. Guna dari 2 buah dateTimePicker ini untuk memilih item2 yang berada pada range tersebut. Namun, ketika saya menjalankan aplikasi, minimum valuenya tidak berdasarkan range tersebut, tetapi berdasarkan semua item yang tersimpan dalam list. Tolong bantuannya. Terima Kasih

avatar welsonoktario
@welsonoktario

1 Kontribusi 0 Poin

Diperbarui 6 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban