如何从 form.cs 文件调用变量到 program.cs 文件

发布于 2024-10-16 04:20:21 字数 1092 浏览 6 评论 0原文

我在访问两个变量时遇到问题。我在互联网上查看,发现我需要使用类似 form.dlg.selectedpath 的东西来调用它,但我收到三个错误。一个说 form.dlg 无法访问,另一个说需要对象引用。我还尝试访问另一个,它说表单不包含 dlg2 的定义。

这是我想要变量的代码。

var di = new DirectoryInfo(Form1.dlg.SelectedPath);  
           di.CopyTo(Form1.dlg2.SelectedPath, true);

这是我的代码,我从中获取一个变量

 public partial class Form1 : Form    
    {  
        FolderBrowserDialog dlg = new FolderBrowserDialog();


        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            if (dlg.ShowDialog() == DialogResult.OK)

,第二个变量是从此处引用的。

private void button1_Click(object sender, EventArgs e)  
        {  
            FolderBrowserDialog dlg2 = new FolderBrowserDialog();  
            if (dlg2.ShowDialog() == DialogResult.OK)  
            //do whatever with dlg.SelectedPath  
            {  
                backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath);  
            }  
        }  

任何帮助将不胜感激。

I am having trouble accessing two of my variables. I have looked on the internet and found that I need to use something like form.dlg.selectedpath to call it but I get three errors. One says form.dlg is inaccessible the next says an object reference is required. I also try to access another one and that says form does not contain definition for dlg2.

This is the code I want the variables in.

var di = new DirectoryInfo(Form1.dlg.SelectedPath);  
           di.CopyTo(Form1.dlg2.SelectedPath, true);

This is my code I am geting one variable from

 public partial class Form1 : Form    
    {  
        FolderBrowserDialog dlg = new FolderBrowserDialog();


        public Form1()
        {
            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)
        {

            if (dlg.ShowDialog() == DialogResult.OK)

And the second variable is referenced from here.

private void button1_Click(object sender, EventArgs e)  
        {  
            FolderBrowserDialog dlg2 = new FolderBrowserDialog();  
            if (dlg2.ShowDialog() == DialogResult.OK)  
            //do whatever with dlg.SelectedPath  
            {  
                backgroundWorker1.RunWorkerAsync(dlg2.SelectedPath);  
            }  
        }  

Any help will be appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泪冰清 2024-10-23 04:20:21

字段不应该真正直接暴露;相反,在表单上添加一个属性:

public string FolderPath {
    get { return dlg.SelectedPath; }
}

然后从表单实例访问:

string path = myFormInstance.FolderPath;

另外 - 确保 dlg 与表单一起处理;就个人而言,我根本不会将其保留为一个字段 - 我有一个强大的字段,我将其分配在一个小块中,用于创建、使用和处置对话框:

public string FolderPath { get; private set; }

private void SelectPath() {
    using(var dlg = new FolderBrowserDialog()) { // or whatever type
        if(dlg.ShowDialog() == DialogResult.OK) {
            FolderPath = dlg.SelectedPath;
        }
    }
}

Fields should not really be directy exposed; instead add a property on the form:

public string FolderPath {
    get { return dlg.SelectedPath; }
}

Then access from your form instance:

string path = myFormInstance.FolderPath;

Also - make sure that dlg is disposed with the form; in pact personally I wouldnt keep it as a field at all - I'd have a strong field that I assign in a small block that creates, uses and disposes the dialog:

public string FolderPath { get; private set; }

private void SelectPath() {
    using(var dlg = new FolderBrowserDialog()) { // or whatever type
        if(dlg.ShowDialog() == DialogResult.OK) {
            FolderPath = dlg.SelectedPath;
        }
    }
}
游魂 2024-10-23 04:20:21
var di = new DirectoryInfo(Form1.dlg.SelectedPath);      
di.CopyTo(Form1.dlg2.SelectedPath, true); 

上面代码的问题是您尝试访问表单实例的成员变量,就好像它是类成员一样。

您编写了 Form1.dlg.SelectedPath 但 Form1 是您的类的名称,可能不是实例的名称。

您可能遇到的另一个错误是编译器表示该成员由于其保护级别而无法访问。这是因为您的成员未声明为公共(dlg-变量)。这是正确的,但不是将其公开,而是提供用于访问类成员的属性。如果您尝试从另一个类访问该成员(我认为您尝试这样做),就会出现此问题。

public FolderBrowserDialog Dlg{
   get{return dlg;}
}

然而,更好的是像 Marc Gravell 发布的解决方案,不提供对话框,而是提供其选择的值。

var di = new DirectoryInfo(Form1.dlg.SelectedPath);      
di.CopyTo(Form1.dlg2.SelectedPath, true); 

The problem with the above code is that you try to access member variables of a form instance as if it were a class member.

You write Form1.dlg.SelectedPath but Form1 is the name of your class and probably not of the instance.

Another error you probably encounter is that the compiler says that the member is inaccessible due to its protection level. This is because your members are not declared public (the dlg-variable). This is right, but instead of making it public, provide a property for accessing members of a class. This problem occurs if you try to access the member from another class (I thinK you try to do this).

public FolderBrowserDialog Dlg{
   get{return dlg;}
}

Better however for this is a solution like Marc Gravell posted, not providing the dialog but its selected value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文