Exchanging data between forms and solutions (added in solution explorer)

Alfie Chadd 61 Reputation points
2022-03-01T19:23:50.69+00:00

Hello I am trying to send data between different forms but nothing has been working, iv tried numerous things for example

    public static class DataContainer
    {
        public static Int32 ValueToShare;
    }

   public void Form1_Method()
    {
        DataContainer.ValueToShare = 10;
    }

    public void Form2_Method()
    {
        MessageBox.Show(DataContainer.ValueToShare.ToString());
    }

anyone know how?

sorry if its a really bad question format im not the best at it

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,873 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,650 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Karen Payne MVP 35,386 Reputation points
    2022-03-01T22:07:32.167+00:00

    The following is a thread safe singleton done in C#9 or higher

    public sealed class DataContainer
    {
        private static readonly Lazy<DataContainer> Lazy = new(() => new DataContainer());
        public static DataContainer Instance => Lazy.Value;
        public int ValueToShare { get; set; }
    }
    

    Any project in your solution that the above class is in scope can be used as followed

    Set

    DataContainer.Instance.ValueToShare = 10;
    

    Read

    MessageBox.Show($"{DataContainer.Instance.ValueToShare}");
    

    Or perhaps

    int total = 9 + DataContainer.Instance.ValueToShare;
    

    If using .NET Framework, you need to modify the int of Lazy to

    private static readonly Lazy<DataContainer> Lazy = new Lazy<DataContainer>(() => new DataContainer());
    
    0 comments No comments

  2. Jack J Jun 24,496 Reputation points Microsoft Vendor
    2022-03-02T01:46:04.377+00:00

    @Alfie Chadd , If you just want to transfer a value from one form to another form, I suggest that you can define the property in the first form and use Application.Openforms method to get the value in the second form.

    Here is a code example you could refer to.

    Form1:

       private void button1_Click(object sender, EventArgs e)  
            {  
                Form2 form = new Form2();  
                form.Show();  
            }  
            public int ValueToShare { get; set; }  
            private void Form1_Load(object sender, EventArgs e)  
            {  
                ValueToShare = 10;  
            }  
    

    Form2:

     private void button1_Click(object sender, EventArgs e)  
            {  
      
                Form1 form = (Form1)Application.OpenForms["Form1"];  
                MessageBox.Show(form.ValueToShare.ToString());  
            }  
    

    When I click button1 in form1, it will show the form2. After clicking the button1 in form2, it will show the value stored in the form1.

    Result:

    179038-image.png

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments