Сохранить состояние кнопки и ComboBox в форме при ее закрытии


    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.IO.Ports;
    
    namespace WindowsFormsApp3
    {
        public partial class Form1 : Form
        {
            Form2 form2 = new Form2();
            string dataIN;
    
            sbyte indexT;
            string sensorT = "0";
            public Form1()
            {
                form2.Owner = this;
                InitializeComponent();
    
            }
    
        
            private void buttonUpdatePorts_Click(object sender, EventArgs e)
            {
                string[] ports = SerialPort.GetPortNames();
                comboBoxPorts.Text = "";
                comboBoxPorts.Items.Clear();
                if(ports.Length != 0)
                {
                    comboBoxPorts.Items.AddRange(ports);
                    comboBoxPorts.SelectedIndex = 0;
                }
            }
    
            private void buttonConnect_Click(object sender, EventArgs e)
            {
                if(buttonConnect.Text == "Подключиться")
                {
                    try
                    {
                        serialPort1.PortName = comboBoxPorts.Text;
                        serialPort1.Open();
                        buttonConnect.Text = "Отключиться";
                        comboBoxPorts.Enabled = false;
    
                    }
                    catch
                    {
                        MessageBox.Show("ERROR"); 
                    }
                }
                else if (buttonConnect.Text == "Отключиться") {
                    serialPort1.Close();
                    buttonConnect.Text = "Подключиться";
                    comboBoxPorts.Enabled = true;
                }
            }
    
            private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
            {
                dataIN = serialPort1.ReadLine();
                this.BeginInvoke(new EventHandler(processData));
            }
            private void processData(object sender, EventArgs e)
            {
                indexT = Convert.ToSByte(dataIN.IndexOf("T"));
                sensorT = dataIN.Substring(0, indexT);
                form2.label1.Text = Convert.ToString(sensorT);
            }
    
       
            private void button1_Click(object sender, EventArgs e)
            {
                Hide();
                form2.Show();
            }
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                System.Diagnostics.Process.GetCurrentProcess().Kill();
            }
        }
    
    }
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace WindowsFormsApp3
    {
        public partial class Form2 : Form
        {
            public Form2()
            {
                InitializeComponent();
            }
    
            private void настройкиToolStripMenuItem_Click(object sender, EventArgs e)
            {
                Hide();
                Form1 form1 = new Form1();
                form1.Show();
            }
        }
    }

Суть заключается в том, что сначала открывается форма, где можно перейти в настройки и откроется форма с настройками, где можно подключиться к последовательному порты, чтобы считать с него данные, далее можно выйти назад на форму, но при переходе в настройки все сбрасывается.


Ответы (0 шт):