C# 如何在不使用C语言序列化的情况下重新打开表单时获取选中的组合框项和按钮上的文本
我在VisualStudio2010和.NETFramework版本4.0中用C windows窗体应用程序创建了一个项目 在我的项目中,主窗体包含另一个名为Connect的窗体。连接表单有五个用于COM端口设置和连接按钮的组合框。当我从组合框下拉列表中选择项目并单击“连接”按钮时,按钮上的文本显示“断开连接”。然后我将关闭连接窗体。Comport将被连接 我的主要问题是,当我重新打开表单以断开通信时。我想在组合框和按钮上的文本显示断开和以前相同的项目 我在堆栈上问这个问题。有人建议我使用序列化,我就是这么做的。工作正常。但我的前辈说不要使用序列化。他不想在项目中使用序列化 那么有没有办法解决这个问题呢?我不知道怎么做。请帮我解决这个问题。提前谢谢 连接表单的代码C# 如何在不使用C语言序列化的情况下重新打开表单时获取选中的组合框项和按钮上的文本,c#,winforms,button,combobox,C#,Winforms,Button,Combobox,我在VisualStudio2010和.NETFramework版本4.0中用C windows窗体应用程序创建了一个项目 在我的项目中,主窗体包含另一个名为Connect的窗体。连接表单有五个用于COM端口设置和连接按钮的组合框。当我从组合框下拉列表中选择项目并单击“连接”按钮时,按钮上的文本显示“断开连接”。然后我将关闭连接窗体。Comport将被连接 我的主要问题是,当我重新打开表单以断开通信时。我想在组合框和按钮上的文本显示断开和以前相同的项目 我在堆栈上问这个问题。有人建议我使用序列化
public partial class Connect : Form
{
public bool Connect_Status = false;
public Connect()
{
InitializeComponent();
COM_List();
}
private void COM_List()
{
for (int i = 0; i < CommPortManager.Instance.GetCommList().Count; i++)
{
cb_CommPort.Items.Add(CommPortManager.Instance.GetCommList()[i]);
}
}
private void btn_Connect_Click(object sender, EventArgs e)
{
if (btn_Connect.Text == "Connect")
{
CommPortManager.Instance.PortName = cb_CommPort.Text;
CommPortManager.Instance.BaudRate = cb_BaudRate.Text;
CommPortManager.Instance.Parity = cb_Parity.Text;
CommPortManager.Instance.StopBits = cb_StopBits.Text;
CommPortManager.Instance.DataBits = cb_DataBits.Text;
if ((cb_CommPort.Text == "") || (cb_BaudRate.Text == "") || (cb_Parity.Text == "") || (cb_DataBits.Text == "") || (cb_StopBits.Text == ""))
{
if (cb_CommPort.Text == "")
{
MessageBox.Show("Please select COM Port and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (cb_BaudRate.Text == "")
{
MessageBox.Show("Please select BaudRate and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (cb_Parity.Text == "")
{
MessageBox.Show("Please select Parity and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (cb_DataBits.Text == "")
{
MessageBox.Show("Please select DataBits and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if(cb_StopBits.Text == "")
{
MessageBox.Show("Please select StopBits and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Connect_Status = false;
}
else
{
if (CommPortManager.Instance.COM_Open() == false)
{
MessageBox.Show("Could not open the COM port. Most likely it is already in use, has been removed, or is unavailable.", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
Connect_Status = false;
}
else
{
//CommPortManager.Instance.COM_Close();
Connect_Status = true;
btn_Connect.Text = "Disconnect";
cb_CommPort.Enabled = false;
cb_BaudRate.Enabled = false;
cb_DataBits.Enabled = false;
cb_Parity.Enabled = false;
cb_StopBits.Enabled = false;
btn_Connect.BackColor = System.Drawing.Color.Salmon;
}
}
}
else
{
CommPortManager.Instance.COM_Close();
btn_Connect.Text = "Connect";
Connect_Status = false;
cb_CommPort.Enabled = true;
cb_BaudRate.Enabled = true;
cb_DataBits.Enabled = true;
cb_Parity.Enabled = true;
cb_StopBits.Enabled = true;
btn_Connect.BackColor = System.Drawing.Color.DarkTurquoise;
}
}
private void btn_Close_Click(object sender, EventArgs e)
{
this.Close();
}
private void Connect_Load(object sender, EventArgs e)
{
//code here to setup the value;
cb_CommPort.Text = CommPortManager.Instance.PortName;
cb_BaudRate.Text = CommPortManager.Instance.BaudRate;
cb_Parity.Text = CommPortManager.Instance.Parity;
cb_StopBits.Text = CommPortManager.Instance.StopBits;
cb_DataBits.Text = CommPortManager.Instance.DataBits;
if (CommPortManager.Instance.IsOpen == true)
{
btn_Connect.Text = "Disconnect";
btn_Connect.BackColor = System.Drawing.Color.Salmon;
cb_CommPort.Enabled = false;
cb_BaudRate.Enabled = false;
cb_DataBits.Enabled = false;
cb_Parity.Enabled = false;
cb_StopBits.Enabled = false;
}
else
{
btn_Connect.Text = "Connect";
Connect_Status = false;
cb_CommPort.Enabled = true;
cb_BaudRate.Enabled = true;
cb_DataBits.Enabled = true;
cb_Parity.Enabled = true;
cb_StopBits.Enabled = true;
btn_Connect.BackColor = System.Drawing.Color.DarkTurquoise;
}
}
}
CommPortManager类代码:
public class CommPortManager
{
//***************************************************singleton pattern
private static CommPortManager instance;
private CommPortManager()
{
ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPort_DataReceived); // event handler
}
public static CommPortManager Instance
{
get
{
if (instance == null)
{
instance = new CommPortManager();
}
return instance;
}
}
//**************************************************************end singleton pattern
public List<byte> Rx_Buffer = new List<byte>();
public List<string> GetCommList() //retrieve list of comport from Computer
{
return (System.IO.Ports.SerialPort.GetPortNames().ToList());
}
private SerialPort ComPort = new SerialPort();
public bool COM_Open()
{
try
{
if (ComPort.IsOpen)
{
ComPort.Close();
}
ComPort.PortName = portName;
ComPort.BaudRate = int.Parse(baudRate); //convert Text to Integer
ComPort.Parity = (Parity)Enum.Parse(typeof(Parity), parity); //convert Text to Parity
ComPort.DataBits = int.Parse(dataBits); //convert Text to Integer
ComPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), stopBits); //convert Text to stop bits
ComPort.Open();
return true;
}
catch (Exception)
{
return false;
}
}
public void COM_Close()
{
ComPort.Close();
}
// declare variables for property
private string baudRate = string.Empty;
private string parity = string.Empty;
private string stopBits = string.Empty;
private string dataBits = string.Empty;
private string portName = string.Empty;
// Property to hold the BaudRate
public string BaudRate
{
get { return baudRate; }
set { baudRate = value; }
}
// property to hold the Parity
public string Parity
{
get { return parity; }
set { parity = value; }
}
// property to hold the StopBits
public string StopBits
{
get { return stopBits; }
set { stopBits = value; }
}
// property to hold the DataBits
public string DataBits
{
get { return dataBits; }
set { dataBits = value; }
}
// property to hold the PortName
public string PortName
{
get { return portName; }
set { portName = value; }
}
public bool IsOpen { get { return ComPort.IsOpen; } }
public bool SendData(List<byte> byte_List)
{
Rx_Buffer.Clear();
try
{
ComPort.Write(byte_List.ToArray(), 0, byte_List.Count); //convert list data to byte array and then send to relay
return true;
}
catch (Exception)
{
return false;
}
}
void ComPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
int length = ComPort.BytesToRead; //retrieve number of bytes in the buffer
byte[] buffer = new byte[length]; //create a byte array to hold the buffer
ComPort.Read(buffer, 0, length); //read the data and display to the user
Rx_Buffer.AddRange(buffer); //Received buffer list
}
}
每次调用click事件时,您都在创建连接表单的新实例,这就是您丢失数据的原因。尝试将connect from object存储为实例变量,并在每次单击时使用该实例。当您不需要connect表单的全局实例和序列化时,可以使用以下任一方法 您可以使用CommPortManager.Instance读取连接表单加载时的值
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//code here to setup the value;
cb_CommPort.Text = CommPortManager.Instance.PortName;
// other fields
}
您可以使用app.config保存和检索值。右键单击项目->设置选项卡添加所需设置SportName等
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//code here to setup the value;
cb_CommPort.Text = Properties.Settings.Default.PortName;
// other fields
}
private void btn_Connect_Click(object sender, EventArgs e)
{
Properties.Settings.Default["PortName"] = CommPortManager.Instance.PortName = cb_CommPort.Text;
// your code
Properties.Settings.Default.Save();
}
使用单个实例创建自定义对象以存储和检索值
在主窗体上创建自定义对象和创建实例,并在窗体之间共享该对象。与以前提交的一样。检查提交文件1和2
你必须改变你的按钮点击
private void btn_Connect_Click(object sender, EventArgs e)
{
if (btn_Connect.Text == "Connect")
{
CommPortManager.Instance.PortName = cb_CommPort.Text;
CommPortManager.Instance.BaudRate = cb_BaudRate.Text;
CommPortManager.Instance.Parity = cb_Parity.Text;
CommPortManager.Instance.StopBits = cb_StopBits.Text;
CommPortManager.Instance.DataBits = cb_DataBits.Text;
if ((cb_CommPort.Text == "") || (cb_BaudRate.Text == "") || (cb_Parity.Text == "") || (cb_DataBits.Text == "") || (cb_StopBits.Text == ""))
{
if (cb_CommPort.Text == "")
{
MessageBox.Show("Please select COM Port and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (cb_BaudRate.Text == "")
{
MessageBox.Show("Please select BaudRate and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (cb_Parity.Text == "")
{
MessageBox.Show("Please select Parity and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (cb_DataBits.Text == "")
{
MessageBox.Show("Please select DataBits and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else if (cb_StopBits.Text == "")
{
MessageBox.Show("Please select StopBits and then Connect", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Connect_Status = false;
}
else
{
if (CommPortManager.Instance.COM_Open() == false)
{
MessageBox.Show("Could not open the COM port. Most likely it is already in use, has been removed, or is unavailable.", "TestCertificate", MessageBoxButtons.OK, MessageBoxIcon.Information);
Connect_Status = false;
}
else
{
//commenting this because when its COM_Open is success closing the Com port
//CommPortManager.Instance.COM_Close();
Connect_Status = true;
btn_Connect.Text = "Disconnect";
cb_CommPort.Enabled = false;
cb_BaudRate.Enabled = false;
cb_DataBits.Enabled = false;
cb_Parity.Enabled = false;
cb_StopBits.Enabled = false;
btn_Connect.BackColor = System.Drawing.Color.Salmon;
}
}
}
else
{
CommPortManager.Instance.COM_Close();
btn_Connect.Text = "Connect";
Connect_Status = false;
cb_CommPort.Enabled = true;
cb_BaudRate.Enabled = true;
cb_DataBits.Enabled = true;
cb_Parity.Enabled = true;
cb_StopBits.Enabled = true;
btn_Connect.BackColor = System.Drawing.Color.DarkTurquoise;
}
也可以按如下方式形成负载变化
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//code here to setup the value;
cb_CommPort.Text = CommPortManager.Instance.PortName;
cb_BaudRate.Text = CommPortManager.Instance.BaudRate;
cb_Parity.Text = CommPortManager.Instance.Parity;
cb_StopBits.Text = CommPortManager.Instance.StopBits;
cb_DataBits.Text = CommPortManager.Instance.DataBits;
btn_Connect.Text = CommPortManager.Instance.IsOpen ? "Disconnect" : "Connect";
}
您能否添加不使用序列化的原因,这可能会帮助我们找到答案。另外:关闭应用程序后重新打开应用程序时是否应恢复设置?您可以为连接详细信息创建自定义对象,在主窗体上创建实例,并将该对象共享到连接窗体。
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
//code here to setup the value;
cb_CommPort.Text = CommPortManager.Instance.PortName;
cb_BaudRate.Text = CommPortManager.Instance.BaudRate;
cb_Parity.Text = CommPortManager.Instance.Parity;
cb_StopBits.Text = CommPortManager.Instance.StopBits;
cb_DataBits.Text = CommPortManager.Instance.DataBits;
btn_Connect.Text = CommPortManager.Instance.IsOpen ? "Disconnect" : "Connect";
}