C# 从 static void 访问控制属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/595125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# accessing control propertes from static void
提问by Woland
I have form with button and checkbox. if i hit button static void is called which call non static void which shows messagebox with the checkbox.checked.toString() The problem is if i change the checkbox value it always shows false
我有带有按钮和复选框的表单。如果我点击按钮 static void 被调用,它调用非静态 void 它显示带有 checkbox.checked.toString() 的消息框问题是如果我更改复选框值,它总是显示 false
Code is
代码是
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void y()
{
MessageBox.Show(checkBox1.Checked.ToString());
}
static void x()
{
Form1 f = new Form1();
f.y();
}
private void button1_Click(object sender, EventArgs e)
{
x();
}
}
}
采纳答案by Ray Lu
Try
尝试
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void y()
{
MessageBox.Show(checkBox1.Checked.ToString());
}
static void x(Form f)
{
f.y();
}
private void button1_Click(object sender, EventArgs e)
{
x(this);
}
}
回答by chakrit
I guess you have probably came from a Visual Basic background like I do.
我猜你可能和我一样有 Visual Basic 背景。
In C#, form are just instances of a Form class, they does not have special status like in the days of VB6.
在 C# 中,form 只是一个 Form 类的实例,它们没有像 VB6 那样的特殊地位。
When you call new Form1()
you are basically creating a newform notaccessing the same form. As anyone form can have multiple instances because it really is just a C# class underneath.
当你打电话new Form1()
你基本上是创建一个新的形式不访问相同的形式。任何形式都可以有多个实例,因为它实际上只是下面的一个 C# 类。
You can fix this by having the x() method takes the current form as a parameter
您可以通过让 x() 方法将当前表单作为参数来解决此问题
static void x(Form1 theForm)
{
theForm.y();
}
private void button1_Click(object sender, EventArgs e)
{
x(this);
}
The this parameter inside a form class points to the form instance itself.
表单类中的 this 参数指向表单实例本身。
You should now gets the correct value instead of the default value when the form is being created.
创建表单时,您现在应该获得正确的值而不是默认值。
I suppose you have a need for x()
to be static
, no? But if that isn't the case, removing static
from x()
might be a better solution.
我想你需要x()
成为static
,不是吗?但如果情况并非如此,static
则从其中删除x()
可能是更好的解决方案。
void x()
{
this.y();
// or you can just omit the this qualifier and call just y();
}
回答by Adam Naylor
It's because your instantiating a new Form1 inside x(), try passing 'this' to the x method as a parameter.
这是因为您在 x() 中实例化了一个新的 Form1,请尝试将“this”作为参数传递给 x 方法。
回答by BFree
You're creating a new Form1 instance. Why are you doing that? When you create a new form, a new checkbox is created as well. The checkbox on the form looks like is set to false (not checked) by default, therefore, every time you create a new instance of the form, it comes up as false.
您正在创建一个新的 Form1 实例。你为什么这样做?创建新表单时,也会创建一个新复选框。表单上的复选框默认设置为 false(未选中),因此,每次创建表单的新实例时,它都会显示为 false。
回答by ng5000
Method x instantiates a new form. The check box on the new form will also be new (created with the form) and will have a default value of false.
方法 x 实例化一个新表单。新表单上的复选框也将是新的(使用表单创建)并且默认值为 false。
What exactly are you trying to do? Why create a new form when the button is pressed? If you really want to do this then you need to set the new form's check box state after you call Form f = new Form1();
你到底想做什么?为什么按下按钮时创建一个新表单?如果您真的想这样做,那么您需要在调用 Form f = new Form1(); 之后设置新表单的复选框状态;
回答by Marc Gravell
The checkbox1
in y()
is on a completely different Form1
- the one you created in x
. Simply get rid of x()
and it should work:
该checkbox1
中y()
是一个完全不同的Form1
-你创建的x
。简单地摆脱x()
它应该可以工作:
private void button1_Click(object sender, EventArgs e)
{
y(); // not x();
}
回答by Marc Gravell
your issues are in the x() method, what you're doing there is actually making an entirely new form and checking that forms check box, which would obviously be instantiating as false. Rather then calling x() you should call y().
您的问题出在 x() 方法中,您在那里所做的实际上是制作一个全新的表单并选中该表单复选框,这显然会实例化为 false。与其调用 x(),不如调用 y()。
Or, alternatively, put the messagebox.show in the buttonclick method itself.
或者,将 messagebox.show 放在 buttonclick 方法本身中。
回答by abatishchev
If you need to access some form instance from static method, you need to save somewhere a reference to that form.
如果您需要从静态方法访问某个表单实例,则需要在某处保存对该表单的引用。
class Program
{
public static Form thatForm;
public static void Main(string[] args)
{
MyForm form = new MyForm();
thatForm = form;
Application.Run(form);
}
}
class MyForm : Form
{
void Foo()
{
Program.thatForm.somethingPublic();
}
}