Click here to Skip to main content
15,917,061 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a textbox and a list box. In textchanges event of textbox i m filling listbox from database
and then select value from listbox and calling another value and then saving data.. i want that if user doesn't select any value from listbox and leaving textbox by typing false value which listbox doesn't contains i want to stop him...


the code i tried is

btnclick

C#
if(listbox1.items.contains(textbox1))
{
   //calling another value. from database 
} 

else
messagebox.show("Please select a value from searchbox");



Thanks in advance....
Posted
Updated 7-May-13 20:50pm
v2
Comments
KM Perumal 8-May-13 2:21am    
Try this
if(listbox1.items.contains(textbox1.text))

C#
// CODE FOR designer FILE
namespace WindowsApplication1
{
    partial class Form1
    {
        /// <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.txt = new System.Windows.Forms.TextBox();
            this.lst = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // txt
            // 
            this.txt.Location = new System.Drawing.Point(40, 21);
            this.txt.Name = "txt";
            this.txt.Size = new System.Drawing.Size(325, 20);
            this.txt.TabIndex = 0;
            this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
            this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
            this.txt.Leave += new System.EventHandler(this.txt_Leave);
            this.txt.Enter += new System.EventHandler(this.txt_Enter);
            // 
            // lst
            // 
            this.lst.FormattingEnabled = true;
            this.lst.Location = new System.Drawing.Point(12, 37);
            this.lst.Name = "lst";
            this.lst.Size = new System.Drawing.Size(10, 4);
            this.lst.TabIndex = 1;
            this.lst.Visible = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(406, 309);
            this.Controls.Add(this.lst);
            this.Controls.Add(this.txt);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txt;
        private System.Windows.Forms.ListBox lst;
    }
}


C#
// CODE FOR cs FILE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        DataTable tblList = new DataTable();
        DataView dvList = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            dvList.RowFilter = "Item Like '" + txt.Text + "%'";
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            lst.Width = txt.Width;
            lst.Height = 100;

            lst.Top = txt.Top + txt.Height;
            lst.Left = txt.Left;

            if (!lst.Visible)
                lst.Visible = true;
        }

        private void txt_Leave(object sender, EventArgs e)
        {
            if (lst.SelectedIndex != -1)
            {
                txt.Text = lst.Text;
            }
            else
            {
                txt.Text = "";
                MessageBox.Show("Please select from list only !");
            }

            lst.Visible = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tblList.Columns.Add("Item");

            tblList.Rows.Add("ONE");
            tblList.Rows.Add("TWO");
            tblList.Rows.Add("THREE");
            tblList.Rows.Add("FOUR");
            tblList.Rows.Add("FIVE");
            tblList.Rows.Add("SIX");
            tblList.Rows.Add("SEVEN");
            tblList.Rows.Add("EIGHT");
            tblList.Rows.Add("NINE");
            tblList.Rows.Add("TEN");

            lst.DisplayMember = "Item";

            lst.DataSource = tblList;

            dvList = tblList.DefaultView;
        }

        private void txt_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyValue == 38) // UP KEY
                {
                    lst.SelectedIndex = lst.SelectedIndex - 1;
                }
                else if (e.KeyValue == 40) // DOWN KEY
                {
                    lst.SelectedIndex = lst.SelectedIndex + 1;
                }
            }
            catch { }
        }
    }
}
 
Share this answer
 
C#
// CODE FOR designer FILE
namespace WindowsApplication1
{
    partial class Form1
    {
        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.txt = new System.Windows.Forms.TextBox();
            this.lst = new System.Windows.Forms.ListBox();
            this.SuspendLayout();
            // 
            // txt
            // 
            this.txt.Location = new System.Drawing.Point(40, 21);
            this.txt.Name = "txt";
            this.txt.Size = new System.Drawing.Size(325, 20);
            this.txt.TabIndex = 0;
            this.txt.TextChanged += new System.EventHandler(this.txt_TextChanged);
            this.txt.KeyDown += new System.Windows.Forms.KeyEventHandler(this.txt_KeyDown);
            this.txt.Leave += new System.EventHandler(this.txt_Leave);
            this.txt.Enter += new System.EventHandler(this.txt_Enter);
            // 
            // lst
            // 
            this.lst.FormattingEnabled = true;
            this.lst.Location = new System.Drawing.Point(12, 37);
            this.lst.Name = "lst";
            this.lst.Size = new System.Drawing.Size(10, 4);
            this.lst.TabIndex = 1;
            this.lst.Visible = false;
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(406, 309);
            this.Controls.Add(this.lst);
            this.Controls.Add(this.txt);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.TextBox txt;
        private System.Windows.Forms.ListBox lst;
    }
}
//-----------------------------------------------------------------------------------------------
// CODE FOR cs FILE
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        DataTable tblList = new DataTable();
        DataView dvList = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void txt_TextChanged(object sender, EventArgs e)
        {
            dvList.RowFilter = "Item Like '" + txt.Text + "%'";
        }

        private void txt_Enter(object sender, EventArgs e)
        {
            lst.Width = txt.Width;
            lst.Height = 100;

            lst.Top = txt.Top + txt.Height;
            lst.Left = txt.Left;

            if (!lst.Visible)
                lst.Visible = true;
        }

        private void txt_Leave(object sender, EventArgs e)
        {
            if (lst.SelectedIndex != -1)
            {
                txt.Text = lst.Text;
            }
            else
            {
                txt.Text = "";
                MessageBox.Show("Please select from list only !");
            }

            lst.Visible = false;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            tblList.Columns.Add("Item");

            tblList.Rows.Add("ONE");
            tblList.Rows.Add("TWO");
            tblList.Rows.Add("THREE");
            tblList.Rows.Add("FOUR");
            tblList.Rows.Add("FIVE");
            tblList.Rows.Add("SIX");
            tblList.Rows.Add("SEVEN");
            tblList.Rows.Add("EIGHT");
            tblList.Rows.Add("NINE");
            tblList.Rows.Add("TEN");

            lst.DisplayMember = "Item";

            lst.DataSource = tblList;

            dvList = tblList.DefaultView;
        }

        private void txt_KeyDown(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.KeyValue == 38) // UP KEY
                {
                    lst.SelectedIndex = lst.SelectedIndex - 1;
                }
                else if (e.KeyValue == 40) // DOWN KEY
                {
                    lst.SelectedIndex = lst.SelectedIndex + 1;
                }
            }
            catch { }
        }
    }
}
//-----------------------------------------------------------------------------------------------
 
Share this 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