2016-12-17 5 views
0

私はこれをたくさん検索しましたが、うまくいく答えが見つかりませんでした。これは私が探しているものです。フォームのtextbox.textを取得

私にはテキストボックスのあるウィンドウがあります。私はボタンを押すと、私はクラスのインスタンスを作成し、私はクラスにte textbox.textを読んでみたいです。 これは私が試したものです:

テキストボックスの休暇イベント(名テキストボックス= textBox_klantnaam):

public string klantNaam 
{ 
    get { return textBox_klantnaam.Text; } 
    set { textBox_klantnaam.Text = value; } 
} 

onclickのボタン:

klantNaam = textBox_klantnaam.Text; 

同じ形式で私は財産を持っています

private void button1_Click(object sender, EventArgs e) 
{ 
    Class_licentiemanager SchrijfLicentieBestand = new Class_licentiemanager(); 
    SchrijfLicentieBestand.schrijfLicBestand(); 
} 

textbox.textを読み込み、それをfiに書き込むクラス。 le プロパティ "klantNaam"は空白に見えますか?

namespace Opzet_Leeg_Framework 
{ 
    class Class_licentiemanager 
    { 
     Class_Logging logging = new Class_Logging(); 
     public static Form_Licentiemanager Licentiemanager = new Form_Licentiemanager(); 

     public void schrijfLicBestand() 
     { 

      using (StreamWriter w = new StreamWriter(Settings.applicatiePad + Form1.SettingsMap + Form1.Applicatienaam + ".lic")) 
       try 
       { 
        try 
        { 
         w.WriteLine("test line, works fine"); 
         w.WriteLine("Naam klant : " + Licentiemanager.klantNaam); //Empty , no line ??? 
        } 
        catch (Exception e) 
        { 
         logging.witeToLog("FOUT", "Het opslaan van het licentiebestand is mislukt", 1); 
         logging.witeToLog("FOUT", "Melding : ", 1); 
         logging.witeToLog("FOUT", e.ToString(), 1); 
        } 
       } 
       finally 
       { 
        w.Close(); 
        w.Dispose(); 
       } 
     } 
    } 
} 

答えて

2

そのクラスに値を渡し、内部に別のフォームインスタンスを作成する必要はありません。 new Form_Licentiemanagerと書くと、そのフォームの新しいインスタンスが作成され、同じインスタンスが再利用されないため、その新しいインスタンスの値はまだ空です。それを修正するには、次の手順を実行します。

private void button1_Click(object sender, EventArgs e) 
{ 
    Class_licentiemanager SchrijfLicentieBestand = new Class_licentiemanager(); 
    SchrijfLicentieBestand.schrijfLicBestand(klantNaam); 
} 

そして、あなたのコードを変更:

class Class_licentiemanager 
{ 
    Class_Logging logging = new Class_Logging(); 
    public void schrijfLicBestand(string klantNaam) 
    { 
     // same code here ...... 
        w.WriteLine("test line, works fine"); 
        w.WriteLine("Naam klant : " + klantNaam); 
     // same code here ...... 
    }   
} 
+0

おかげで多くのことを、これは動作します。 – Hansvb

関連する問題