質問

I use OpenFileDialog to open and read a file into my application to display specific data. I have multiple Forms - 2/3 of which I need to be able to display the value that is read from the file into a label. At the moment, I have just hard-coded some data into a label and using a Get Set method, I am able to get the value. However, when I have tried to get the label value when data is populated from a file, nothing returns.

In Form1.cs:

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

Retrieve Value from File:

// 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];
    }
}

In Barcode.cs:

Form1 f1 = new Form1();
MessageBox.Show(f1.GetSetBarcode); // For testing purposes... But this returns 0 :(
役に立ちましたか?

解決 3

I managed to come up with a solution, see https://stackoverflow.com/a/21310270/2952390 (answer to a related question I asked previously). Here it retrieves text from a combobox to a label on another form. Much easier than I thought it would be.

他のヒント

Mhh normally you must no do like this , it's better don't do that . You should see here . They have maybe a solution for you ;-)

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

I've tested your given scenario and it work for me. I think your problem is where you hardcode the data in your test into the label. Do you do it at Form_Load-Event? Because the load event only occurs after form.Show(); If you hardcode the labeltext in the designer or at the forms constructor it works ;)

hope I could help you ^^

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top