C# 使用一种形式声明的变量,另一种形式是c

C# 使用一种形式声明的变量,另一种形式是c,c#,forms,C#,Forms,我在这里有一个小项目,我声明了一个名为dir的变量,其中一个目录是在文件夹浏览器对话框中以main.cs的形式选择的: namespace XmoSupportTools { public partial class Main : Form { public string dir; public FolderBrowserDialog xmodialog { get; private set; } public DialogResult xmodialogresult

我在这里有一个小项目,我声明了一个名为dir的变量,其中一个目录是在文件夹浏览器对话框中以main.cs的形式选择的:

namespace XmoSupportTools
{
public partial class Main : Form
{
    public string dir;

    public FolderBrowserDialog xmodialog { get; private set; }
    public DialogResult xmodialogresult { get; private set; }

    //metoder der bliver brugt senere
    public void startxmo()
    {
        string startfile = dir + "\\xmo.exe";
        Process xmoappli = new Process();

        if (File.Exists(startfile))
        {
            xmoappli.StartInfo.FileName = startfile;
            xmoappli.Start();
        }
        else
        {
            MessageBox.Show("XMO.exe blev ikke fundet på den valgte lokation!");
            File.Delete(dir + "\\xmo.ini");
            dialog();
        }

    }

    public void dialog()
    {

        xmodialog = new FolderBrowserDialog();
        xmodialog.Description = "Find dit XMO Directory:";
        xmodialogresult = xmodialog.ShowDialog();
        if (xmodialogresult == DialogResult.OK)
        {
            dir = xmodialog.SelectedPath;

        }
我想在第二种形式Kunde.cs中使用dir变量:

public void startxmo()
    {
        string startfile = dir + "\\xmo.exe";
        Process xmoappli = new Process();

        if (File.Exists(startfile))
        {
            xmoappli.StartInfo.FileName = startfile;
            xmoappli.Start();
        }
        else
        {
            MessageBox.Show("XMO.exe blev ikke fundet på den valgte lokation!");
            File.Delete(dir + "\\xmo.ini");
            dialog();
        }

    }

您可以尝试使用form 2 Kunde.cs上的参数创建一个公共类,并将调用的代码插入该类中。从主窗体调用该函数并传递参数。例如:

//this should be on 2nd form
public void sampleFunction (string dir)
{
   //paste your code here
}

//this is how you will call it on main form
form2.sampleFunction(dir);

您可以尝试使用form 2 Kunde.cs上的参数创建一个公共类,并将调用的代码插入该类中。从主窗体调用该函数并传递参数。例如:

//this should be on 2nd form
public void sampleFunction (string dir)
{
   //paste your code here
}

//this is how you will call it on main form
form2.sampleFunction(dir);

您在哪里创建Kunde.cs表单的实例?也许您可以将dir传递给kunde.cs的自定义custructor的参数;有点像,虽然它是来自稍微不同的上下文的代码,但它使用相同的样式-

private void button_login(object sender, EventArgs e)
{
   MainMenu ss= new MainMenu(textBox1.Text);
   this.Hide();            
   ss.Show();
}

class MainMenu : Form
{
    // This is an "Auto-Implemented Property".
    // Auto-Implemented Properties are used as a shortcut of what you have done.
    // Google them for more information.
    public string UserName { get; set; }

    private void MainMenu(string userName)
    {
        this.UserName = userName;
    }
}

您在哪里创建Kunde.cs表单的实例?也许您可以将dir传递给kunde.cs的自定义custructor的参数;有点像,虽然它是来自稍微不同的上下文的代码,但它使用相同的样式-

private void button_login(object sender, EventArgs e)
{
   MainMenu ss= new MainMenu(textBox1.Text);
   this.Hide();            
   ss.Show();
}

class MainMenu : Form
{
    // This is an "Auto-Implemented Property".
    // Auto-Implemented Properties are used as a shortcut of what you have done.
    // Google them for more information.
    public string UserName { get; set; }

    private void MainMenu(string userName)
    {
        this.UserName = userName;
    }
}

这在很大程度上取决于应用程序的生命周期,我并不精通winforms,但是,如果您可以将Kunde类与Main类连接起来,例如:

public partial class Main : Form
{
  private Kunde _kunde;

  public Main(Kunde kunde)
  {
     _kunde = kunde;
  }

  public void dialog()
  {
    xmodialog = new FolderBrowserDialog();
    xmodialog.Description = "Find dit XMO Directory:";
    xmodialogresult = xmodialog.ShowDialog();
    if (xmodialogresult == DialogResult.OK)
    {
        dir = xmodialog.SelectedPath;
        _kunde.Dir = dir;
    }
  }

  // rest of your code...
}

public class Kunde 
{
  public string Dir { get; set; }

  public void startxmo()
  {
    string startfile = Dir + "\\xmo.exe";
    Process xmoappli = new Process();

    if (File.Exists(startfile))
    {
        xmoappli.StartInfo.FileName = startfile;
        xmoappli.Start();
    }
    else
    {
        MessageBox.Show("XMO.exe blev ikke fundet på den valgte lokation!");
        File.Delete(dir + "\\xmo.ini");
        dialog();
    }
  }
}
然后使用,例如,或某种其他类型的DI框架将Kunde对象注入表单


本质上,您正在寻找一个观察者模式的简单实现,其中Kunde对象观察主窗体,简单的方式不需要动态注册或注销,因此您可以通过构造函数注入修复依赖项。

这在很大程度上取决于应用程序生命周期,我并不精通winforms,但是,如果你能将Kunde类与Main类连接起来,例如:

public partial class Main : Form
{
  private Kunde _kunde;

  public Main(Kunde kunde)
  {
     _kunde = kunde;
  }

  public void dialog()
  {
    xmodialog = new FolderBrowserDialog();
    xmodialog.Description = "Find dit XMO Directory:";
    xmodialogresult = xmodialog.ShowDialog();
    if (xmodialogresult == DialogResult.OK)
    {
        dir = xmodialog.SelectedPath;
        _kunde.Dir = dir;
    }
  }

  // rest of your code...
}

public class Kunde 
{
  public string Dir { get; set; }

  public void startxmo()
  {
    string startfile = Dir + "\\xmo.exe";
    Process xmoappli = new Process();

    if (File.Exists(startfile))
    {
        xmoappli.StartInfo.FileName = startfile;
        xmoappli.Start();
    }
    else
    {
        MessageBox.Show("XMO.exe blev ikke fundet på den valgte lokation!");
        File.Delete(dir + "\\xmo.ini");
        dialog();
    }
  }
}
然后使用,例如,或某种其他类型的DI框架将Kunde对象注入表单

本质上,您正在寻找一个观察者模式的简单实现,其中Kunde对象观察主表单,简单的方式不需要动态注册或注销,因此您可以通过构造函数注入修复依赖关系

在program.cs中,将mainform实例设置为public static

在你的另一节课上:

现在,您可以通过在每个类中访问main中的string dir Program.cs mainform实例

在program.cs中,将mainform实例设置为public static

在你的另一节课上:

现在,您可以通过在每个类中访问main中的string dir Program.cs mainform实例


您只需在Kunde.cs中编写

System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["MainForm"]; 
//Whatever is the Name of the Form
//than
string kundedir = ((MainForm)f).dir;
另一种方法是,您可以创建一个参数化构造函数来调用Kunde,然后在MainForm中调用

Kunde k = new Kunde(dir);
在昆德

string kundedir;
Kunde(string dirval)
{
    kundedir = dirval;
}

您只需在Kunde.cs中编写

System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["MainForm"]; 
//Whatever is the Name of the Form
//than
string kundedir = ((MainForm)f).dir;
另一种方法是,您可以创建一个参数化构造函数来调用Kunde,然后在MainForm中调用

Kunde k = new Kunde(dir);
在昆德

string kundedir;
Kunde(string dirval)
{
    kundedir = dirval;
}
您可以将create dir设置为静态变量,并将保护级别设置为您希望应用程序中的其他表单可以访问变量dir的范围

{access specifier} static int dir = "path";
{access specifier}可以是public、protected internal、protected等,具体取决于它

然后通过点符号调用dir:

Main.dir.ToString();
e、 g

您可以将create dir设置为静态变量,并将保护级别设置为您希望应用程序中的其他表单可以访问变量dir的范围

{access specifier} static int dir = "path";
{access specifier}可以是public、protected internal、protected等,具体取决于它

然后通过点符号调用dir:

Main.dir.ToString();
e、 g


在名为Kunde的表单中创建一个公共变量,然后像var\u myForm=new Kunde一样初始化表单;然后您可以执行_myForm.PublicVariableName=dir,其中PublicVariableName是您为变量指定的名称创建一个名为Kunde的公共变量,然后初始化您的表单,如var _myForm=new Kunde;然后您可以执行_myForm.PublicVariableName=dir,其中PublicVariableName是您为变量指定的名称为什么公共静态字符串dir中的静态;?哦,对不起,我没有看到我改变了它为什么在公共静态字符串dir;?哦,很抱歉,我没有看到我更改了它。在您建议的第一个选项中,将创建一个MainForm的新实例。这是不必要的过度使用。在您建议的第一个选项中,将创建MainForm的新实例。那是不必要的过度消耗。