Sono alle prime armi e vorrei sapere come fare a gestire oggetti Form da un'altra classe o file?
Esempio:
File: Form1.cs (legato al Form1.Designer.cs)
codice:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace Csharp_Simple_CDC_Demo
{
    public partial class Form1 : Form    
    {
        delegate void SetTextCallback(string text);
    
        public Form1()
        {
            InitializeComponent();
            lstBaudRate.SelectedIndex = 5;
            rxdPcDataReceived.Clear();
            txdPcDataReceived.Clear();
            UpdateCOMPortList();
            btnSendData.Enabled = false;
            txtData.Enabled = false;
        }

        public void UpdateCOMPortList()
        {
            int i;
            bool foundDifference;
           
            i = 0;
            foundDifference = false;

            if (lstCOMPorts.Items.Count == SerialPort.GetPortNames().Length)
            {
                foreach (string s in SerialPort.GetPortNames())
                {
                    if (lstCOMPorts.Items[i++].Equals(s) == false)
                    {
                        foundDifference = true;
                    }
                }
            }
            else
            {
                foundDifference = true;
            }

            if (foundDifference == false)
            {
                return;
            }

            lstCOMPorts.Items.Clear();

            foreach (string s in SerialPort.GetPortNames())
            {
                lstCOMPorts.Items.Add(s);
            }
            lstCOMPorts.SelectedIndex = 0;
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateCOMPortList();
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            Int32 i;
            bool conversion;
            try
            {
                serialPort1.PortName = lstCOMPorts.Items[lstCOMPorts.SelectedIndex].ToString();
                conversion = Int32.TryParse(lstBaudRate.Text,out i);
                if (conversion != true)
                {
                    throw new Exception();
                }
                serialPort1.BaudRate = i;
                serialPort1.DataBits = 8;
                serialPort1.Parity = Parity.None;
                serialPort1.Open();
                btnConnect.Enabled = false;
                lstCOMPorts.Enabled = false;
                lstBaudRate.Enabled = false;
                btnClose.Enabled = true;
                //
                comStatText.Text = "Connected";
            }
            catch
            {
                comStatText.Text = "Error Opening";
            }
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            btnClose.Enabled = false;
            btnConnect.Enabled = true;
            lstCOMPorts.Enabled = true;
            lstBaudRate.Enabled = true;
            comStatText.Clear();

            try 
            {
                serialPort1.DiscardInBuffer();
                serialPort1.DiscardOutBuffer();
                serialPort1.Close();
            }
            catch { }
        }

        private void SetText(string text)
        {
            byte i;
            // InvokeRequired required compares the thread ID of the
            // calling thread to the thread ID of the creating thread.
            // If these threads are different, it returns true.
            if (this.txdPcDataReceived.InvokeRequired)
            {
                SetTextCallback d = new SetTextCallback(SetText);
                this.Invoke(d, new object[] { text });              
            }
            else
            {             
                if (text.Contains("T"))
                {
                    this.txdPcDataReceived.AppendText(text.Remove(0,1));                    
                    for (i =1; i < (text.Length - 1); i++)
                    {    
                        this.txdPcDataHex.AppendText(Convert.ToString(text[1], 16));
                        this.txdPcDataHex.AppendText(" ");
                    }                            
                }
                else if (text.Contains("R"))
                {
                    this.rxdPcDataReceived.AppendText(text.Remove(0,1));
                    for (i = 1; i < (text.Length - 1); i++)
                    {
                        this.rxdPcDataHex.AppendText(Convert.ToString(text[1], 16));
                        this.rxdPcDataHex.AppendText(" ");
                    }
                }
            }
        }

        private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
        {
            this.SetText(serialPort1.ReadExisting());
        }

        private void button1_Click(object sender, EventArgs e)
        {
            serialPort1.DiscardInBuffer();
            serialPort1.Write("pippo", 0, 5);
        }     
}
Vorrei però gestire (read/write) gli oggeti di Form1 da quest'altro file:
codice:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;


public class SerialProto
{
    public Smsp()  
    {
        pendingAnswersCtr = 0;
        broad_packet = 0;
    }

    private void SmspAvvioTx()
    {
//      serialPort1.Write("pluto", 0, 5); // <-- Per esempio vorrei poter utilizzare questa 
    }
}
Se la classe SerialProto la chiamo: public partial class Form1 : Form sotto il namespace
Csharp_Simple_CDC_Demo (come il file Form1.cs) l'oggetto serialPort1 riesco a gestirlo ma vorrei trovare un'altra strada.
Grazie.