从标签检索数据到MessageBox

时间:2013-12-17 08:50:17

标签: c# winforms .net-3.5 openfiledialog

我使用OpenFileDialog打开文件并将其读入我的应用程序以显示特定数据。我有多个表单 - 其中2/3我需要能够将从文件中读取的值显示为标签。目前,我刚刚将一些数据硬编码到标签中并使用Get Set方法,我能够获得该值。但是,当我尝试从文件中填充数据时获取标签值时,没有任何返回。

在Form1.cs中:

internal string GetSetBarcode
{
    get
    {
        // Barcode label
        return this.label36.Text;
    }
    private set
    {
        this.label36.Text = value;
    }
}

从文件中检索值:

// Currently working on a new method to populate data more appropriately as this is not the best, but it works for now.

string result = System.Text.Encoding.UTF8.GetString(box);
string r = Regex.Replace(result, "[^a-zA-Z0-9 .-]", string.Empty);

for (int i = 0; i < r.Length; i++)
{
    for (int b = 11; i < b; i++) // Product Code
    {
        label7.Text += r[i];
    }
}

在Barcode.cs中:

Form1 f1 = new Form1();
MessageBox.Show(f1.GetSetBarcode); // For testing purposes... But this returns 0 :(

3 个答案:

答案 0 :(得分:0)

通常你不能这样做,最好不要这样做。 你应该看到这里。他们可能为您提供解决方案; - )

how-to-access-winform-textbox-control-from-another-class

答案 1 :(得分:0)

我已经测试了你给定的场景,它对我有用。 我认为您的问题在于将测试中的数据硬编码到标签中。 你是在Form_Load-Event上做的吗? 因为load事件只发生在form.Show()之后;如果您在设计器或表单构造函数中对labeltext进行硬编码,则可以使用;)

希望我能帮助你^^

答案 2 :(得分:0)

我设法提出了一个解决方案,请参阅https://stackoverflow.com/a/21310270/2952390(回答我之前提到的相关问题)。在这里,它从组合框检索文本到另一个表单上的标签。比我想象的容易得多。