Hi,
i was applied validations for textboxes and comboxes using validated ,validating events and errorprovider
it's working fine.
but here the problem is:
i opened the form,and i kept the mouse on combobox or textbox ,without entering any if i click form's cancel (on top of form),
the form is not closed how to solve it.
Here is the code:
<pre lang="c#">using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.IO.Ports;
namespace Billling_Machine
{
public partial class Connect : Form
{
String ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
public Connect()
{
InitializeComponent();
}
private void Connect_Load(object sender, EventArgs e)
{
using (MySqlConnection con = new MySqlConnection(ConnectionString))
{
String query = "select machine_id from device";
MySqlCommand command = new MySqlCommand(query,con);
con.Open();
MySqlDataAdapter da = new MySqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
DataRow dr = dt.NewRow();
dr["machine_id"] = "Select";
dt.Rows.InsertAt(dr,0);
comboBox1.DataSource = dt;
comboBox1.DisplayMember = "machine_id";
comboBox1.ValueMember = "machine_id";
comboBox1.SelectedIndex = 0;
}
string[] ArrayComPortsNames = null;
int index = -1;
string ComPortName = null;
ArrayComPortsNames = SerialPort.GetPortNames();
do
{
index += 1;
comboBox2.Items.Add(ArrayComPortsNames[index]);
}
while (!((ArrayComPortsNames[index] == ComPortName)
|| (index == ArrayComPortsNames.GetUpperBound(0))));
Array.Sort(ArrayComPortsNames);
if (index == ArrayComPortsNames.GetUpperBound(0))
{
ComPortName = ArrayComPortsNames[0];
}
comboBox2.Text = "select";
}
private void btnconnect_Click(object sender, EventArgs e)
{
if (this.ValidateChildren(ValidationConstraints.Enabled))
{
try
{
String value = comboBox1.SelectedValue.ToString();
Properties.Settings.Default.machineid = value;
serialPort1.PortName = comboBox2.SelectedItem.ToString();
Properties.Settings.Default.serialportname = serialPort1.PortName;
if (!serialPort1.IsOpen)
{
serialPort1.Open();
Properties.Settings.Default.portstatus = serialPort1.IsOpen;
}
this.Hide();
Program.ConnectObj.Show();
}
catch (Exception q)
{
MessageBox.Show("Unable To Open" + comboBox2.SelectedItem.ToString());
}
}
}
private void Connect_FormClosed(object sender, FormClosedEventArgs e)
{
Program.ConnectObj.Show();
}
private void Connect_FormClosing(object sender, FormClosingEventArgs e)
{
Program.ConnectObj.connect.Enabled = true;
Program.ConnectObj.btnConnect.Enabled = true;
Program.ConnectObj.btnDisconnect.Enabled = false;
Program.ConnectObj.disconnect.Enabled = false;
Program.ConnectObj.uploadToDevice.Enabled = false;
Program.ConnectObj.downloadFromDevice.Enabled = false;
}
private void comboBox1_Validating(object sender, CancelEventArgs e)
{
bool cancel = false;
if (comboBox1.SelectedIndex == -1)
{
cancel = true;
this.errorProvider1.SetError(this.comboBox1, "Select Machine ID");
}
e.Cancel = cancel;
}
private void comboBox1_Validated(object sender, EventArgs e)
{
this.errorProvider1.SetError(this.comboBox1, string.Empty);
}
private void comboBox2_Validating(object sender, CancelEventArgs e)
{
bool cancel = false;
if (comboBox2.SelectedIndex == -1)
{
cancel = true;
this.errorProvider1.SetError(this.comboBox2, "Select COM port");
}
e.Cancel = cancel;
}
private void comboBox2_Validated(object sender, EventArgs e)
{
this.errorProvider1.SetError(this.comboBox2, string.Empty);
}
}
}
and designer is:
<pre lang="c#">namespace Billling_Machine
{
partial class Connect
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.btnconnect = new System.Windows.Forms.Button();
this.comboBox2 = new System.Windows.Forms.ComboBox();
this.label1 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.label2 = new System.Windows.Forms.Label();
this.serialPort1 = new System.IO.Ports.SerialPort(this.components);
this.errorProvider1 = new System.Windows.Forms.ErrorProvider(this.components);
this.groupBox1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).BeginInit();
this.SuspendLayout();
this.groupBox1.Controls.Add(this.btnconnect);
this.groupBox1.Controls.Add(this.comboBox2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Controls.Add(this.comboBox1);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.groupBox1.Location = new System.Drawing.Point(58, 42);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(326, 175);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Enter";
this.btnconnect.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnconnect.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnconnect.Location = new System.Drawing.Point(96, 125);
this.btnconnect.Name = "btnconnect";
this.btnconnect.Size = new System.Drawing.Size(75, 23);
this.btnconnect.TabIndex = 13;
this.btnconnect.Text = "Connect";
this.btnconnect.UseVisualStyleBackColor = true;
this.btnconnect.Click += new System.EventHandler(this.btnconnect_Click);
this.comboBox2.Font = new System.Drawing.Font("Times New Roman", 9.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.comboBox2.FormattingEnabled = true;
this.comboBox2.Location = new System.Drawing.Point(143, 76);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new System.Drawing.Size(121, 23);
this.comboBox2.TabIndex = 12;
this.comboBox2.Text = "Select";
this.comboBox2.Validating += new System.ComponentModel.CancelEventHandler(this.comboBox2_Validating);
this.comboBox2.Validated += new System.EventHandler(this.comboBox2_Validated);
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Times New Roman", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(29, 82);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(78, 17);
this.label1.TabIndex = 11;
this.label1.Text = "COM Port";
this.comboBox1.Font = new System.Drawing.Font("Times New Roman", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(143, 42);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 27);
this.comboBox1.TabIndex = 10;
this.comboBox1.Text = "Select";
this.comboBox1.Validating += new System.ComponentModel.CancelEventHandler(this.comboBox1_Validating);
this.label2.AutoSize = true;
this.label2.Font = new System.Drawing.Font("Times New Roman", 11.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label2.Location = new System.Drawing.Point(29, 43);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(86, 17);
this.label2.TabIndex = 9;
this.label2.Text = "Machine ID";
this.errorProvider1.ContainerControl = this;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(458, 266);
this.Controls.Add(this.groupBox1);
this.Name = "Connect";
this.Text = "Connect";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Connect_FormClosing);
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Connect_FormClosed);
this.Load += new System.EventHandler(this.Connect_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.errorProvider1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button btnconnect;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.Label label1;
public System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label2;
public System.IO.Ports.SerialPort serialPort1;
private System.Windows.Forms.ErrorProvider errorProvider1;
}
}