 |
|
C# AutoComplete เวลาพิมพ์ค้นหาใน textbox มันไม่ค้นหาคำอื่นๆด้วย %text% แบบนี้ แต่มันค้นหาจากคำแรก ไม่ได้หมายถึงตอนกด Enter นะครับ หมายถึงตอนพิมพ์ และยังไม่ได้เลือก ยังไม่ได้ Enter |
|
 |
|
|
 |
 |
|
งมๆมาได้แค่นี้แหละครับ มือใหม่ ฮ่าๆๆ ถ้าใช้ DataGridView แทน listbox ข้อความจะออกมาเป็นระเบียบกว่านี้ มันจะแสดงแยกเป็น col เลย ทีนี้ส่งค่าสบาย ๆ ไม่ต้องไปตัดช่องว่างออกเหมือนผม 5555 ลองๆเอาไปประยุกต์กันดูครับ อาจจะยังไม่ถูกหลักการ เดี๋ยวพอเราเรียนรู้คำสั่งมากขึ้นค่อยกลับมาแก้ให้ code สั้นลงครับ
Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
namespace doproject
{
public partial class frm_stkitem : Form
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataReader dr;
SqlDataAdapter da = new SqlDataAdapter();
string MyDB = ConfigurationManager.ConnectionStrings["DBConnect"].ConnectionString;
public frm_stkitem()
{
InitializeComponent();
this.ActiveControl = textBox1;
textBox1.AutoCompleteMode = AutoCompleteMode.None;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
AutoSuggest();
hideResults();
}
private void AutoSuggest()
{
try
{
conn = new SqlConnection(MyDB);
conn.Open();
cmd = new SqlCommand("SELECT Barcode, Code, Name1, Name2 FROM items", conn);
dr = cmd.ExecuteReader();
textBox1.AutoCompleteCustomSource.Clear();
AutoCompleteStringCollection acsc = new AutoCompleteStringCollection();
textBox1.AutoCompleteCustomSource = acsc;
while (dr.Read())
{
string rw = (string)dr["code"] + " " + (string)dr["name1"] + " " + (string)dr["name2"] + " " + (string)dr["Barcode"];
acsc.Add(rw.ToString());
}
conn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void textBox1_TextChanged(object sender, System.EventArgs e)
{
listBox1.Items.Clear();
if (textBox1.Text.Length == 0)
{
hideResults();
return;
}
foreach (String s in textBox1.AutoCompleteCustomSource)
{
if (s.ToUpper().Contains(textBox1.Text.ToUpper()))
{
listBox1.Items.Add(s);
listBox1.Visible = true;
}
}
if (listBox1.Items.Count > 0)
{
listBox1.SelectedIndex++;
}
else {
listBox1.Visible = false;
}
}
void hideResults()
{
listBox1.Visible = false;
}
void result() {
string Text1 = textBox1.Text;
Text1 = Text1.Replace("'", "''");
string[] str1 = Text1.Split(" ".ToCharArray());
conn.Open();
cmd = new SqlCommand("SELECT Barcode, Code, Name1, Name2 FROM items WHERE code like '%" + str1[0] + "%' or Barcode like '%" + str1[0] + "%'", conn);
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
dataGridView1.DataSource = dt;
}
else
{
MessageBox.Show("ไม่พบสินค้า " + textBox1.Text);
}
textBox1.Clear();
textBox1.Focus();
conn.Close();
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
if (listBox1.Items.Count > 0)
{
textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
result();
}
}
if (e.KeyCode == Keys.Up && this.listBox1.SelectedIndex - 1 > -1)
{
listBox1.SelectedIndex--;
textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
}
if (e.KeyCode == Keys.Down && this.listBox1.SelectedIndex + 1 < this.listBox1.Items.Count)
{
listBox1.SelectedIndex++;
textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
}
}
private void listBox1_Click(object sender, EventArgs e)
{
textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString();
hideResults();
result();
}
}
}
|
ประวัติการแก้ไข 2016-11-16 17:58:34
 |
 |
 |
 |
Date :
2016-11-16 17:57:25 |
By :
nPointXer |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|