簡體   English   中英

C#-從另一個.CS文件訪問ToolStripMenuItem控件

[英]C# - Accessing ToolStripMenuItem Control from another .CS File

我有以下代碼行:

checkForUpdatesToolStripMenuItem.Enabled = true;

但是,它給了我錯誤:

名稱“ checkForUpdatesToolStripMenuItem”在當前上下文中不存在

問題是,如何從我正在使用的當前.CS文件訪問項目(checkForUpdatesToolStripMenuItem),其中“ checkForUpdatesToolStripMenuItem”是表單的一部分?

謝謝。

您可以將值傳遞給另一個類:

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
    }
}
namespace SO_Question_win

{//就C#而言,同一個命名空間下的每個人都可能會在銷售文件中。 為了方便起見,我們使用不同的文件。 公共局部類Form1:Form {//這是項目中主要表單的表單類。 程序啟動時線程在此處運行。 在創建它們之前,將不存在其他實例類

    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";
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM