0

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 :(
lornasw93
  • 182
  • 4
  • 26
  • why create new Form1()?? – har07 Dec 17 '13 at 08:53
  • Please, show how you populate label with data from file – Sergey Berezovskiy Dec 17 '13 at 08:53
  • Verify that you are referring to correct label (i.e. you set the value on label36.Text before accessing it.) Because i have tested your given scenario and it work for me. – Ramashankar Dec 17 '13 at 09:05
  • Seems odd, and not the whole picture. The line `Form1 f1 = new Form1();` is only creating a new form instance. What then causes "Retrieve Value from File" to happen? Is it in the Form's constructor? Called by a method _that is in the constructor_? I see nothing that would cause `GetSetBarcode` to be populated with anything but the default value (which is why hard-coding it _works_). – DonBoitnott Dec 18 '13 at 12:51

3 Answers3

0

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

Community
  • 1
  • 1
Mehdi Bugnard
  • 3,889
  • 4
  • 45
  • 86
0

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 ^^

DJmRek
  • 378
  • 6
  • 13
0

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.

Community
  • 1
  • 1
lornasw93
  • 182
  • 4
  • 26