我有这行代码:
checkForUpdatesToolStripMenuItem.Enabled = true;
然而,它给了我错误:
名称'checkForUpdatesToolStripMenuItem'不存在于 当前背景
问题是,如何从我正在使用的当前.CS文件中访问该项目(checkForUpdatesToolStripMenuItem),其中'checkForUpdatesToolStripMenuItem'是表单的一部分?
感谢。
答案 0 :(得分:0)
您可以将值传递给其他类:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Send_ToolStrip_Data_To_Instance_Class()
{
My_Other_CS_File other_File = new My_Other_CS_File();
other_File.Act_On_ToolStrip_Item(checkForUpdatesToolStripMenuItem.Enabled);
}
}
public class My_Other_CS_File
{
public void Act_On_ToolStrip_Item(bool enabled)
{
//do something
}
}
答案 1 :(得分:0)
namespace SO_Question_win
{ //就C#而言,在同一名称空间下的EVERYTHIG也可能出现在销售文件中。我们使用不同的文件以方便。 公共部分类Form1:表格 { //这是项目中主窗体的表单类。它是程序启动时线程所在的位置。在创建
之前,不会存在其他实例类 public Form1()
{
InitializeComponent();
}
private void hello()
{
//I can only call hello from inside this class.
My_Other_CS_File other_File = new My_Other_CS_File();
//you created this instance class here. It will only exist until "hello" finishes running, then it will disappear.
string hello = "hi";
//the only way to get the string hello to the class you created is to pass it.
other_File.SayHello(hello);
//other_file is done. It will disappear now if you want it again you will have to create a new instance
}
}
public class My_Other_CS_File
{
public void SayHello(string hi)
{
//here, the string Hi is passed from the form class
Console.WriteLine(hi);
//even though this class was created by the form class, it has access to any public static classes.
Console.WriteLine(Global.helloString);
}
}
public static class Global
{
//anything marked "public static" here will be visible to any class under the same namespace. This class is niether created nor destroyed - it always exists. hat's the difference bewtween static and instance.
public static string helloString = "howdy";
}
}