2016-11-04 98 views
1

我這裏有這個小項目中,我已宣佈了一個名爲「目錄」在我的形式稱爲main.cs一個瀏覽文件夾對話框中選擇一個目錄變量:使用的一種形式聲明的變量,以另一種形式的C#

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在我的第二個形式「目錄」變量:

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(); 
     } 

    } 
+0

在名爲Kunde的窗體中創建一個公共變量,然後像var _myForm = new Kunde()一樣初始化您的窗體。那麼你可以做_myForm.PublicVariableName = dir(其中PublicVariableName是你給變量的名稱) –

+0

http://stackoverflow.com/questions/1293926/c-sharp-winforms-global-variables – Blackstar

回答

1

您可以嘗試在表格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); 
1

你在哪裏創建Kunde.cs表單的實例?也許你可以通過kunde.cs的自定義custructor的參數來傳遞dir;東西有點像(雖然它是從一個稍微不同的上下文中的代碼,但它使用相同的樣式仍然。) -

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

這在很大程度上取決於應用程序生命週期,我不是真正的WinForms精通,但,如果你可以將Main類與Kunde類掛鉤,例如像這樣:

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(); 
    } 
    } 
} 

,然後使用例如AutofacSimple Injector或一些其他類型的DI框架的注入的Kunde物體插入的形式。

實際上,您正在尋找一種簡單的觀察者模式實現,其中Kunde對象觀察主窗體,簡單,不需要動態註冊或註銷,因此您可以通過構造函數注入來修復依賴關係。

1

在Program.cs中設置的MainForm實例公共靜態

public static Lapphantering mainForm;   
/// <summary> 
/// The main entry point for the application. 
/// </summary> 
[STAThread] 

static void Main() 
{ 
    mainForm = new Mainform(); // create instance of Lapphantering 
    Application.Run(mainForm); 
} 

在你的MainForm:

public partial class Main : Form 
    { 
     public string dir; 

     //code 
    } 

在你其他類:

public void startxmo() 
{ 

    //access dir variable from main form instance in program.cs 
    string startfile = program.mainform.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(program.mainform.dir + "\\xmo.ini"); 
     dialog(); 
    } 

} 

所以現在你可以從每個班級都在通過 Program.cs的MainForm的實例訪問字符串中的主目錄。

+0

爲什麼靜態在'公共靜態字符串目錄;'? – netchkin

+0

aw,對不起,我沒有看到我被鎖住了 –

1

您只需通過編寫

System.Windows.Forms.Form f = System.Windows.Forms.Application.OpenForms["MainForm"]; 
//Whatever is the Name of the Form 
//than 
string kundedir = ((MainForm)f).dir; 

做這將是你可以調用昆德創建一個參數的構造函數的另一種方式,然後在MainForm的

Kunde k = new Kunde(dir); 
爲此在Kunde.cs在Kunde的

。CS

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

在你建議的第一個選項中,將創建一個新的MainForm實例。這是不必要的矯枉過正。 – Ron16

0

您可以設置創建dirstatic變量,並設置防護等級達到你想要多少變量dir範圍是accessivle其他形式的應用程序中的

{access specifier} static int dir = "path"; 

{訪問說明符}可以是public,protected internalprotected等,視其而定。

然後經由點符號調用dir

Main.dir.ToString(); 

例如

public partial class Form1 : Form 
{ 
    protected internal static int dir = 54; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     Form2 a = new Form2(); 
     a.Show(); 
    } 
} 

public partial class Form2 : Form 
{ 
    public Form2() 
    { 
     MessageBox.Show(Form1.dir.ToString()); 
     InitializeComponent(); 
    } 
}