Click here to Skip to main content
15,884,425 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
<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.Insert(dr, 0);
                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);

            //want to get first out
            if (index == ArrayComPortsNames.GetUpperBound(0))
            {
                ComPortName = ArrayComPortsNames[0];
            }
           // comboBox2.Text = ArrayComPortsNames[0];
            comboBox2.Text = "select";

        }

        private void btnconnect_Click(object sender, EventArgs e)
        {
            if (this.ValidateChildren(ValidationConstraints.Enabled))
            {
                try
                {
                    //get machine id into Form1
                    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;
                       
                    }

                    //get serialport status in Form1
                    //    bool value1 = serialPort1.IsOpen;
                    //     newform.portstatus = value1;
                    // this.Close();
                    //     newform.ShowDialog();
                    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:
C#
<pre lang="c#">namespace Billling_Machine
{
    partial class Connect
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        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();
            // 
            // groupBox1
            // 
            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";
            // 
            // btnconnect
            // 
            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);
            // 
            // comboBox2
            // 
            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);
            // 
            // label1
            // 
            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";
            // 
            // comboBox1
            // 
            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.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
            this.comboBox1.Validating += new System.ComponentModel.CancelEventHandler(this.comboBox1_Validating);
            // 
            // label2
            // 
            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";
            // 
            // errorProvider1
            // 
            this.errorProvider1.ContainerControl = this;
            // 
            // Connect
            // 
            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;
    }
}
Posted
Comments
Member 10263519 28-Feb-14 1:08am    
Combo Box is not allowing without selecting anything
Member 10263519 1-Mar-14 2:20am    
solved myself
Member 11905456 15-Jul-16 0:03am    
how you solved this.. please share the answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900