-1
    private void rectangleButton3_Click(object sender, EventArgs e)
    {
        using (OpenFileDialog ofd = new OpenFileDialog())
        {
            ofd.Filter = "Excel Files only | *.xlsx; *.xls; *.csv;";
            ofd.Title = "Choose the file";
            if (ofd.ShowDialog() == DialogResult.OK)
                label1.Text = ofd.FileName;
        }
    }

Hi, im trying to use label1 as my path to export .csv file into my project: https://ibb.co/wNbnqbg

and then in my second form im trying to import: https://ibb.co/mRG0q5S but the problem is this code: https://ibb.co/Gv3fSPv dont know that label1 is.

Im trying to create main page where user choose with which .csv file will be working. After rectangleButton3_click you save that path into label1 and then in second form i want use this path /label1 to import data into datagridview.

  • 1
    You won't be access to the control from another form, you'll need to pass the value some other way. Plenty of options available. https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form – Ryan Thomas Feb 13 '23 at 16:02
  • 1
    Don't store something in a control only show things in a control. Think about a datastructure to store the path and then move that datastructure between your forms as you need them. At best the forms only know about the datastructure and nothing about other forms. Typically such a datastructure is called a model. – Ralf Feb 13 '23 at 16:02

2 Answers2

0

You can't use label1 in a different forms because it's a private field of the form so you can't share value through this field. Also Label is a visual component and not intended for data storage.

To share the value between the two forms, you can create a property in the second form that takes the value from first form. You can set the property in the first form after Excel file has been selected.

public class SecondForm : Form
{
    private string _filePath;
    public string FilePath
    {
        get { return _filePath; }
        set { _filePath = value; }
    }
    ...
}

When you creating a new form you can set the value from label1 or other place. For example if you open the second form immediately after choosing file you can use next code:

private void rectangleButton3_Click(object sender, EventArgs e)
{
    using (OpenFileDialog ofd = new OpenFileDialog())
    {
        ofd.Filter = "Excel Files only | *.xlsx; *.xls; *.csv;";
        ofd.Title = "Choose the file";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            SecondForm secondForm = new SecondForm();
            secondForm.FilePath = ofd.FileName;
            secondForm.Show();
        }
    }
}

Finally you can read FilePath in the second form because it's a part of your form and the value is already written from the first form.

If you need to access the value between many forms there are two ways to solve it.

"Good" and architecturally correct solution is to pass this value from form to form and to have a property for file path in each form. It's a right way because you will continue to have a loosely coupled architecture and be able to easily modify the code. For example the middle form may start to get the file path in a different way or the composition of the forms itself may change.

"Bad" and hacky solution is a global state with static properties which you can read and write in any place:

public static class ApplicationState
{
    private static string _filePath;
    public static string FilePath
    {
        get { return _filePath; }
        set { _filePath = value; }
    }
}

So you can write ApplicationState.FilePath = ofd.FileName or var filePath = ApplicationState.FilePath; in any place of your code. But the problem is that this violates the encapsulation principle, making it difficult to test and modify the code when developing the project because you will have a single value within the entire application.

Vadim Martynov
  • 8,602
  • 5
  • 31
  • 43
  • The problem is there is one more form beetwen these two, i will try show you how it work now: This is main page: https://ibb.co/DVBj2MS when you click on that button where arrow is showing, you can choose what file you want to "save" into label" Then you click on Zoznam 2 and this form open: https://ibb.co/bFdb8Zs then you click on first button and last and final form appear: https://ibb.co/Pz70qMT and this is the Form where user will be working with datas from excel, in this last form there will be combobox with moth so user can sort datas by months – Pavol Trimay Feb 13 '23 at 16:16
  • All what i want on main page is somehow save or import .csv file thats all... then in other forms i want use that path (or file ). Because in other forms i will sort datas from file/path what user choose, if you want i can create a video how it work :) – Pavol Trimay Feb 13 '23 at 16:25
  • @PavolTrimay oh now I understand your scenario. I updated my answer but finally solution depends on many factors. For exampe on how the application will be further developed and whether there will be a need for other interactions between the forms. With global value you can't reuse forms to open and convert many files in same time because there is only one global state. – Vadim Martynov Feb 13 '23 at 16:32
  • Its only 1 file. That you choose on main page, in other forms you just work with the file you choose on main page, if somebody want other file they must close and again open a software – Pavol Trimay Feb 13 '23 at 16:51
-1

You probably want to access it from other form or class.

  1. First you have to declare the label as public or anything visible to other files;
  2. The class calling it have to know the Form1 unit, you may need to add it to the uses clause, but I am not sure;
  3. Then you call it by the complete name, for example Form1.label1.Text.
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 16 '23 at 22:39