Задать числа для построения диаграммы в коде

Всем привет. Программа рисует диаграмму введите сюда описание изображения

Как задать числа не через textbox а прописать в коде? т.е чтобы при запуске программы диарамма уже строилась с заданными числами Код программы:

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 Diagramm
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Graphics graphics;
        float[] array_of_values = new float[4];
        float[] array_of_heights = new float[4];

        List<Rectangle> rectList = new List<Rectangle>();

        private void DrawDiag()
        {
            graphics = panel1.CreateGraphics();

            float columnWidth = panel1.Width / 3;

            for (int i = 0; i < array_of_values.Length; i++)
            {
                array_of_heights[i] = panel1.Height / 100f * array_of_values[i];
                rectList.Add(new Rectangle((int)(i * columnWidth), (int)(panel1.Height - array_of_heights[i]), (int)(columnWidth - (columnWidth / 10)), (int)array_of_heights[i]));
                graphics.FillRectangle(new SolidBrush(Color.ForestGreen), rectList[i]);
            }
        }

        private void DrawHorizontalMark()
        {
            graphics = panel1.CreateGraphics();
            float height_panel = panel1.Height / 3f;

            for (int i = 0; i < 3; i++)
            {
                graphics.DrawLine(new Pen(Color.Black, 2), 0, height_panel * i, panel1.Width, height_panel * i);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (rectList.Count > 0)
            {
                Array.Clear(array_of_values, 0, array_of_values.Length);
                rectList.Clear();
            }

            array_of_values[0] = Convert.ToInt32(textBox1.Text);
            array_of_values[1] = Convert.ToInt32(textBox2.Text);
            array_of_values[2] = Convert.ToInt32(textBox3.Text);


            if (textBox1.Text == "")
            {
                array_of_values[0] = 0;
            }
            else
            {
                array_of_values[0] = (Convert.ToInt32(textBox1.Text));
            }
            if (textBox2.Text == "")
            {
                array_of_values[1] = 0;
            }
            else
            {
                array_of_values[1] = (Convert.ToInt32(textBox2.Text));
            }
            if (textBox2.Text == "")
            {
                array_of_values[1] = 0;
            }
            else
            {
                array_of_values[1] = (Convert.ToInt32(textBox2.Text));
            }


            panel1.Refresh();
            DrawHorizontalMark();
            DrawDiag();
        }

    }
}

Ответы (1 шт):

Автор решения: aepot

Вынести код из обработчика Click в отдельный метод, добавить ему аргументы, и вызвать с нужными аргументами из обработчика события Form.Load.

Получится примерно так

private void DrawHistogram(float value1, float value2, float value3)
{
    if (rectList.Count > 0)
    {
        Array.Clear(array_of_values, 0, array_of_values.Length);
        rectList.Clear();
    }

    array_of_values[0] = value1;
    array_of_values[1] = value2;
    array_of_values[2] = value3;
            
    panel1.Refresh();
    DrawHorizontalMark();
    DrawDiag();
}

private void button1_Click(object sender, EventArgs e)
{
    float value1 = float.TryParse(textBox1.Text, out float v1) ? v1 : 0;
    float value2 = float.TryParse(textBox2.Text, out float v2) ? v2 : 0;
    float value3 = float.TryParse(textBox3.Text, out float v3) ? v3 : 0;
    DrawHistogram(value1, value2, value3);
}

private void Form1_Load(object sender, EventArgs e)
{
    float value1 = 10;
    float value2 = 20;
    float value3 = 30;
    textBox1.Text = value1;
    textBox2.Text = value2;
    textBox3.Text = value3;
    DrawHistogram(value1, value2, value3);
}
→ Ссылка