Есть ли способ получить доступ к объектам, определенным внутри метода? - PullRequest
0 голосов
/ 21 апреля 2020

Я видел некоторые темы по этой теме c, но решение, похоже, не относится к моему делу. Я динамически создаю 2 метки и 2 сетки из 30 пользовательских элементов управления внутри FlowLayoutPanel. Я хочу запрограммировать кнопку для обновления sh отображаемых данных. Я решил, что могу расположить все внутри панели и снова запустить метод создания, но я определяю эти объекты внутри своего метода, поэтому не могу получить к ним доступ в кнопке. Определение этих объектов вне метода нарушает работу программы. Итак, как мне получить доступ к объектам, определенным в методе, или есть лучший способ создать кнопку refre sh для моего случая? Вот код:

public Meta()
    {
        InitializeComponent();
        RunMeta();
    }

    public void RunMeta()
    {
        flowLayoutPanel1.AutoScroll = true;
        Label mostPickedLabel = new Label();
        mostPickedLabel.Text = "Most picked heroes";
        flowLayoutPanel1.Controls.Add(mostPickedLabel);
        mostPickedLabel.Margin = new Padding(5, 0, 1300, 0);
        mostPickedLabel.Font = new Font("Lucida Sans Unicode", 15);
        mostPickedLabel.ForeColor = Color.DarkCyan;
        mostPickedLabel.Size = new Size(200, 30);
        foreach (var mostPickedHero in FetchDataFromDota2Site.MostUsedHeroesAndImages)
        {
            Test test = new Test();
            test.heroName.Text = mostPickedHero.MostPickedHeroName;
            test.heroName.Font = new Font("Lucida Sans Unicode", 8);
            test.heroName.ForeColor = Color.White;
            test.temp.ImageLocation = mostPickedHero.ImageSource;
            test.temp.SizeMode = PictureBoxSizeMode.StretchImage;
            test.Left = test.Width * flowLayoutPanel1.Controls.Count;
            test.Margin = new Padding(10, 0, 10, 0);
            test.heroStats.Text = mostPickedHero.WinRate;
            test.heroStats.ForeColor = Color.White;
            test.heroStats.Font = new Font("Lucida Sans Unicode", 8, FontStyle.Bold);
            test.heroStats2.Text = mostPickedHero.TotalPicks;
            test.heroStats2.Font = new Font("Lucida Sans Unicode", 7);
            test.heroStats2.ForeColor = Color.DarkCyan;
            test.heroStats3.Text = mostPickedHero.TotalWins;
            test.heroStats3.Font = new Font("Lucida Sans Unicode", 7);
            test.heroStats3.ForeColor = Color.Green;
            test.heroStats4.Text = mostPickedHero.TotalLoses;
            test.heroStats4.Font = new Font("Lucida Sans Unicode", 7);
            test.heroStats4.ForeColor = Color.Red;
            flowLayoutPanel1.Controls.Add(test);
        }
        Label mostSuccessfulLabel = new Label();
        mostSuccessfulLabel.Text = "Most successful heroes";
        flowLayoutPanel1.Controls.Add(mostSuccessfulLabel);
        mostSuccessfulLabel.Margin = new Padding(5, 50, 1300, 0);
        mostSuccessfulLabel.Font = new Font("Lucida Sans Unicode", 15);
        mostSuccessfulLabel.ForeColor = Color.DarkCyan;
        mostSuccessfulLabel.Size = new Size(300, 30);
        foreach (var mostSuccessfulHero in FetchDataFromDota2Site.MostSuccessfulHeroesAndImages)
        {
            Test test = new Test();
            test.heroName.Text = mostSuccessfulHero.MostSuccessfulHeroName;
            test.heroName.Font = new Font("Lucida Sans Unicode", 8);
            test.heroName.ForeColor = Color.White;
            test.temp.ImageLocation = mostSuccessfulHero.ImageSource;
            test.temp.SizeMode = PictureBoxSizeMode.StretchImage;
            test.Left = test.Width * flowLayoutPanel1.Controls.Count;
            test.Margin = new Padding(10, 0, 10, 0);
            test.heroStats.Text = mostSuccessfulHero.WinRate;
            test.heroStats.ForeColor = Color.White;
            test.heroStats.Font = new Font("Lucida Sans Unicode", 8, FontStyle.Bold);
            test.heroStats2.Text = mostSuccessfulHero.TotalPicks;
            test.heroStats2.Font = new Font("Lucida Sans Unicode", 7);
            test.heroStats2.ForeColor = Color.DarkCyan;
            test.heroStats3.Text = mostSuccessfulHero.TotalWins;
            test.heroStats3.Font = new Font("Lucida Sans Unicode", 7);
            test.heroStats3.ForeColor = Color.Green;
            test.heroStats4.Text = mostSuccessfulHero.TotalLoses;
            test.heroStats4.Font = new Font("Lucida Sans Unicode", 7);
            test.heroStats4.ForeColor = Color.Red;
            flowLayoutPanel1.Controls.Add(test);
        }
    }
    private void refreshDataButton_Click(object sender, System.EventArgs e)
    {

    }
}
...