简体   繁体   English

获取表单的textbox.text

[英]Get textbox.text of a form

I searched a lot for this but i did not found an answer that works. 我为此进行了大量搜索,但没有找到有效的答案。 This is what i am lookig for. 这就是我的目标。

I have a window with a textbox. 我有一个带有文本框的窗口。 When i push a button i make a instance of a class and then i want to read te textbox.text into the class. 当我按下按钮时,我创建了一个类的实例,然后我想将textbox.text读入该类。 This is what i tried: 这是我尝试的:

Textbox leave event (name textbox = textBox_klantnaam): 文本框离开事件(名称文本框= textBox_klantnaam):

klantNaam = textBox_klantnaam.Text;

In the same form i have a property: 以相同的形式,我有一个属性:

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

onclick button: onclick按钮:

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

The class which neeeds to read the textbox.text and then write it to a file the property "klantNaam" seems to be empty???? 需要读取textbox.text然后将其写入属性为“ 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();
                }
        }
    }
}

You need to pass the value to that class, and not create another form instance inside of it. 您需要将值传递给该类,而不是在其中创建另一个表单实例。 When you write new Form_Licentiemanager then you're creating an new instance of that form and not reusing the same instance, so the value on that new instance is still empty. 当您编写new Form_Licentiemanager您将创建该表单的新实例,而不是重用同一实例,因此该新实例上的值仍为空。 To fix that, do the following: 要解决此问题,请执行以下操作:

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

And change your code: 并更改您的代码:

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 ......  
    }           
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM