A
A
Andrey Fomin2021-10-26 20:01:32
C++ / C#
Andrey Fomin, 2021-10-26 20:01:32

How to pass data from one form to another?

Hello!
There are two forms:
61782d0da9d5b119481086.png
And the problem is the following: after clicking the "Send a number to Bob" button, the value from the "Big number P" textbox from the "Alice" form is transferred to the "Bob" form, to the "Big P number" textbox, and how It can be done?
If there are visual examples of code or links to the source, I will be grateful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris the Animal, 2021-10-26
@Casper-SC

Without MVP, you can try something like this. Here, the whole thing is convenient in that the update can be obtained at least in 10 forms, at least somewhere else. We just subscribe to the event handler and that's it.
In a very small program, this will be superfluous, but it does not hurt to at least try something more complicated than assigning from one data form to another directly.
Program.cs

using System;
using System.Windows.Forms;
using WinFormsApp.Infrastructure;

namespace WinFormsApp
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.SetHighDpiMode(HighDpiMode.SystemAware);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            
            Application.Run(new AliceForm(Storage));
        }

        private static DataStorage Storage => new DataStorage();
    }
}

Infrastructure\DataStorage.cs
using System.Collections.Generic;

namespace WinFormsApp.Infrastructure
{
    public delegate void ItemUpdatedHandler(string key);

    public class DataStorage
    {
        private readonly Dictionary<string, string> _data = new Dictionary<string, string>();

        public event ItemUpdatedHandler ItemUpdated; 

        public void AddOrUpdate(string key,string newValue)
        {
            if (_data.TryGetValue(key, out _))
            {
                _data[key] = newValue;
            }
            else
            {
                _data.Add(key, newValue);
            }
            ItemUpdated?.Invoke(key);
        }

        public bool TryGetValue(string key, out string value)
        {
            return _data.TryGetValue(key, out value);
        }
    }
}

Infrastructure\DataStorageKey.cs
namespace WinFormsApp.Infrastructure
{
    internal sealed class DataStorageKey
    {
        public const string BIG_P_KEY = "big_p";
    }
}

AliceForm.cs
using System.Windows.Forms;
using WinFormsApp.Infrastructure;

namespace WinFormsApp
{
    public partial class AliceForm : Form
    {
        private readonly DataStorage _dataStorage;
        private readonly BobForm _bobForm;

        public AliceForm(DataStorage dataStorage)
        {
            InitializeComponent();

            Load += OnFormLoad;

            _dataStorage = dataStorage;
            _bobForm = new BobForm(dataStorage);
        }

        private void OnFormLoad(object sender, System.EventArgs e)
        {
            _bobForm.Show(this);
        }

        private void OnSendButtonClick(object sender, System.EventArgs e)
        {
            _dataStorage.AddOrUpdate(DataStorageKey.BIG_P_KEY, _bigPTextBox.Text);
        }
    }
}

BobForm.cs
using System.Windows.Forms;
using WinFormsApp.Infrastructure;

namespace WinFormsApp
{
    public partial class BobForm : Form
    {
        private readonly DataStorage _dataStorage;

        public BobForm(DataStorage dataStorage)
        {
            InitializeComponent();
            
            _dataStorage = dataStorage;
            _dataStorage.ItemUpdated += OnDataStorageItemUpdated;

            Closing += OnFormClosing;
        }

        private void OnFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _dataStorage.ItemUpdated -= OnDataStorageItemUpdated;
        }

        private void OnDataStorageItemUpdated(string key)
        {
            if (key == DataStorageKey.BIG_P_KEY)
            {
                if (_dataStorage.TryGetValue(key, out string value))
                {
                    _bigPTextBox.Text = value;
                }
            }
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question