C# Process is terminated due to StackOverflowException
Я очень сильно начинающий, помогите пожалуйста.
В обычный Form1 добавляю progressBar. Называю его progressBar1.
Из соседнего файла и класса пытаюсь передать значение value для progressBar1. Я так понял, что из-за этого возникает рекурсия. Подскажите, как правильно получить доступ к value?
Form1.cs
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Class1 cls=new Class1(100);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
class Class1
{
public Class1(int a)
{
Form1 prb = new Form1();
prb.progressBar1.Value = a;
}
}
}
Form.Designer.cs
namespace WindowsFormsApp1
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Код, автоматически созданный конструктором форм Windows
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.ForeColor = System.Drawing.SystemColors.ControlText;
this.progressBar1.Location = new System.Drawing.Point(115, 120);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(493, 66);
this.progressBar1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(12F, 25F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.progressBar1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.ProgressBar progressBar1;
}
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
Form1 создает Class1, а Class1 создает Form1.
Round and round it goes, where it stops nobody knows.
Передавайте progressBar1 объекту Class1 снаружи.
ProgressBar pBar;
public Class1(int a, ProgressBar pBar)
{
this.pBar = pBar;
this.pBar.Value = a;
}
...
public Form1()
{
InitializeComponent();
Class1 cls = new Class1(100, progressBar1);
}