고려하세요:
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//int[] val = { 0, 0};
int val;
if (textBox1.Text == "")
{
MessageBox.Show("Input any no");
}
else
{
val = Convert.ToInt32(textBox1.Text);
Thread ot1 = new Thread(new ParameterizedThreadStart(SumData));
ot1.Start(val);
}
}
private static void ReadData(object state)
{
System.Windows.Forms.Application.Run();
}
void setTextboxText(int result)
{
if (this.InvokeRequired)
{
this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
}
else
{
SetTextboxTextSafe(result);
}
}
void SetTextboxTextSafe(int result)
{
label1.Text = result.ToString();
}
private static void SumData(object state)
{
int result;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{
result += i;
System.Threading.Thread.Sleep(1000);
}
setTextboxText(result);
}
delegate void IntDelegate(int result);
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
이 오류가 발생하는 이유는 무엇인가요?
정적이 아닌 필드, 메서드 또는 속성에 대한 개체 참조가 필요합니다. 'WindowsApplication1.Form1.setTextboxText(int)
정적 메서드에서 정적이 아닌 속성을 호출하고 있는 것 같습니다. 프로퍼티를 정적으로 만들거나 Form1의 인스턴스를 만들어야 합니다.
static void SetTextboxTextSafe(int result)
{
label1.Text = result.ToString();
}
또는
private static void SumData(object state)
{
int result;
//int[] icount = (int[])state;
int icount = (int)state;
for (int i = icount; i > 0; i--)
{
result += i;
System.Threading.Thread.Sleep(1000);
}
Form1 frm1 = new Form1();
frm1.setTextboxText(result);
}
이 오류에 대한 자세한 내용은 MSDN에서 확인할 수 있습니다.
이 경우, where you want to get a 제어할 수 있으며, 이 오류가 발생한 후 양식 나도 좀 우회 당신꺼에요.
Gnu. 데이터베이스에구성원을 Program.cs 및 변경
Application.Run(new Form1());
수
public static Form1 form1 = new Form1(); // Place this var out of the constructor
Application.Run(form1);
이제 제어점 액세스하려면 함께
Program.form1.<Your control>
있다. # 39, t forget 설정하려면 don& 액세스 제어 수준 공개할 예정이다.
그리고 나도 알고 있어, 이 질문에 대한 답을 맞지 않을 수 있지만, 이 특정 문제를 사용는 구글러 요청자에게 부응합니다 통제서.
방법을 정적임 있어야 합니다.
static void setTextboxText(int result)
{
if (this.InvokeRequired)
{
this.Invoke(new IntDelegate(SetTextboxTextSafe), new object[] { result });
}
else
{
SetTextboxTextSafe(result);
}
}
나를 위한 @COOLGAMETUBE credit 을 어떻게 됐지 팁핑을 오프하도록 날 위해 일하는. 하지만 난 때 그의 아이디어를 좋은분이었나요 아프리카션.세콤파트리브레티스트레네링데포트 양식 이미 생성된 후 문제가 있었다) 라고 불렸다. 그래서 조금만 바꼈소, 이는 working for me:
< pre> < code>;;; 정적 클래스 프로그램 { 공용 정적 Form1 form1. // = new Form1 (); 이 var 빼냅니다 구성자를 배치하십시오 //
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(form1 = new Form1());
}
}
제가 보기에는 텍스트 상자에 널 값을 지정하고 정적 메서드이므로 ToString()
으로 반환합니다. 널 값을 사용할 수 있는 Convert.ToString()
으로 대체할 수 있습니다.