2009-08-31 24 views
15

मैं एक टेक्स्टबॉक्स नियंत्रण रखना चाहता हूं जो विंडोज अनुप्रयोग में सी # 2008 और LINQ के साथ डेटाबेस से मूल्यों को सुझाता है और जोड़ता है।स्वत: पूर्ण टेक्स्टबॉक्स नियंत्रण

मैं इसे एक combobox के साथ करता हूं लेकिन मैं इसे टेक्स्टबॉक्स के साथ नहीं कर सकता।

मैं इसे कैसे कर सकता हूं?

+0

मुझे खेद है, मैंने नहीं देखा कि आप विंडोज़ एप्लिकेशन का उपयोग कर रहे थे। – sshow

उत्तर

32

यह काम करने के लिए सबसे अच्छा तरीका नहीं हो सकता है, लेकिन काम करना चाहिए:

enter image description here

आप

:

this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 

private void textBox1_TextChanged(object sender, EventArgs e) 
{ 
    TextBox t = sender as TextBox; 
    if (t != null) 
    { 
     //say you want to do a search when user types 3 or more chars 
     if (t.Text.Length >= 3) 
     { 
      //SuggestStrings will have the logic to return array of strings either from cache/db 
      string[] arr = SuggestStrings(t.Text); 

      AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); 
      collection.AddRange(arr); 

      this.textBox1.AutoCompleteCustomSource = collection; 
     } 
    } 
} 
+0

सुझाव स्ट्रिंग प्रोग्राम चलाने की अनुमति नहीं देता है और कहता है "वर्तमान संदर्भ में 'SuggestStrings' नाम मौजूद नहीं है " –

+13

@ मोहम्मद-रेजा कोड में टिप्पणी के रूप में // सुझावों के निशान में कैश/डीबी से तारों की सरणी वापस करने के लिए तर्क होगा। आपको सुझावों को लागू करना होगा। उम्मीद न करें कि आप सिर्फ एसओ से कोड कॉपी करेंगे और यह काम करना शुरू कर देगा। हम आपको पॉइंटर्स प्रदान कर सकते हैं। –

0

आप कुंजीडाउन ईवेंट से अटैचमेंट कर सकते हैं और फिर उस टेक्स्ट के उस हिस्से के लिए डेटाबेस से पूछ सकते हैं जिसे उपयोगकर्ता पहले ही दर्ज कर चुका है। उदाहरण के लिए, यदि उपयोगकर्ता "टी" में प्रवेश करता है, तो "टी" से शुरू होने वाली चीज़ों की खोज करें। फिर, जब वे अगले पत्र में प्रवेश करते हैं, उदाहरण के लिए "ई", तालिका में चीजों की खोज करें जो "ते" से शुरू होती हैं।

उपलब्ध वस्तुओं को "फ़्लोटिंग" लिस्टबॉक्स में प्रदर्शित किया जा सकता है, उदाहरण के लिए। आपको टेक्स्टबॉक्स के नीचे केवल सूची बॉक्स को रखना होगा ताकि वे उपलब्ध प्रविष्टियों को देख सकें, फिर जब वे टाइपिंग कर रहे हों तो सूची बॉक्स को हटा दें।

+0

प्रोग्रामिंग का पहला नियम: पहिया को पुन: पेश न करें;) –

+0

सच है। यह नहीं पता था कि इसके लिए एक विकल्प था। मैं कभी भी बाध्यकारी डेटा नहीं करता हूं। (.NET 1.0 के साथ शुरू हुआ, जब वे बाहर निकले, तो कुछ डेटाबेसिंग सुविधाओं का उपयोग करने का प्रयास किया, लेकिन उन्हें बहुत ही प्रतिबंधित पाया गया।) –

9

AutoCompleteSource, AutoCompleteCustomSource और AutoCompleteMode गुण देखें।

textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; 
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
AutoCompleteStringCollection col = new AutoCompleteStringCollection(); 
col.Add("Foo"); 
col.Add("Bar"); 
textBox1.AutoCompleteCustomSource = col; 

ध्यान दें कि डिजाइनर आपको लगता है कि किसी भी कोड लिखने के बिना ...

+0

मुझे डेटाबेस से उपयोग करना चाहिए। क्या आप मेरी मदद कर सकते हैं? मैं सुझाव और स्वत: पूर्ण –

+1

@ मोहम्मद रेजा के लिए डेटाबेस से पढ़ना चाहता हूं: आपको डीबी तक पहुंचने के लिए ADO.net का उपयोग करके कोड लिखना होगा। –

+0

बस अपनी क्वेरी के परिणामों के साथ सूची भरें –

1
निश्चित रूप से

यह कैसे आप इसे लागू पर निर्भर करता है, लेकिन शायद यह एक अच्छी शुरुआत है करने के लिए अनुमति देता है:

using System.Windows.Forms; 

public class AutoCompleteTextBox : TextBox { 

    private string[] database;//put here the strings of the candidates of autocomplete 
    private bool changingText = false; 

    protected override void OnTextChanged (EventArgs e) { 
     if(!changingText && database != null) { 
      //searching the first candidate 
      string typed = this.Text.Substring(0,this.SelectionStart); 
      string candidate = null; 
      for(int i = 0; i < database.Length; i++) 
       if(database[i].Substring(0,this.SelectionStart) == typed) { 
        candidate = database[i].Substring(this.SelectionStart,database[i].Length); 
        break; 
       } 
      if(candidate != null) { 
       changingText = true; 
       this.Text = typed+candidate; 
       this.SelectionStart = typed.Length; 
       this.SelectionLength = candidate.Length; 
      } 
     } 
     else if(changingText) 
      changingText = false; 
     base.OnTextChanged(e); 
    } 

} 

मुझे यकीन नहीं है कि यह बहुत अच्छी तरह से काम कर रहा है, लेकिन मुझे लगता है कि इस कोड का आधार काफी अच्छा है।

+0

को एहसास नहीं हुआ कि इसके लिए गुण-विमर्श गुण थे। मैं System.Windows.Forms के साथ इतना काम नहीं करता, आखिरी संस्करण जहां मैंने इसका इस्तेमाल किया था। नेट फ्रेमवर्क 1.1 –

0
private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; 
      textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
      AutoCompleteStringCollection col = new AutoCompleteStringCollection(); 
      con.Open(); 
      sql = "select *from Table_Name; 
      cmd = new SqlCommand(sql, con); 
      SqlDataReader sdr = null; 
      sdr = cmd.ExecuteReader(); 
      while (sdr.Read()) 
      { 
       col.Add(sdr["Column_Name"].ToString()); 
      } 
      sdr.Close(); 

      textBox1.AutoCompleteCustomSource = col; 
      con.Close(); 
     } 
     catch 
     { 
     } 
    } 
1
To AutoComplete TextBox Control in C#.net windows application using 
wamp mysql database... 

here is my code.. 

AutoComplete(); 

write this **AutoComplete();** text in form-load event.. 

private void Autocomplete() 
    { 
     try 
     { 
      MySqlConnection cn = new MySqlConnection("server=localhost; 
database=databasename;user id=root;password=;charset=utf8;"); 
      cn.Open(); 
      MySqlCommand cmd = new MySqlCommand("SELECT distinct Column_Name 
    FROM table_Name", cn); 
      DataSet ds = new DataSet(); 
      MySqlDataAdapter da = new MySqlDataAdapter(cmd); 
      da.Fill(ds, "table_Name"); 
      AutoCompleteStringCollection col = new 
      AutoCompleteStringCollection(); 
      int i = 0; 
      for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++) 
      { 
       col.Add(ds.Tables[0].Rows[i]["Column_Name"].ToString()); 

      } 
      textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
      textBox1.AutoCompleteCustomSource = col; 
      textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; 
      cn.Close(); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, 
     MessageBoxIcon.Error); 
     } 
    } 
0
You can add a parameter in the query like @emailadd to be added in the aspx.cs file where the Stored Procedure is called with cmd.Parameter.AddWithValue. 
    The trick is that the @emailadd parameter doesn't exist in the table design of the select query, but being added and inserted in the table. 

    USE [DRDOULATINSTITUTE] 
    GO 
    /****** Object: StoredProcedure [dbo].[ReikiInsertRow] Script Date: 5/18/2016 11:12:33 AM ******/ 
    SET ANSI_NULLS ON 
    GO 
    SET QUOTED_IDENTIFIER ON 
    GO 
    ALTER procedure [dbo].[ReikiInsertRow] 
    @Reiki varchar(100), 
    @emailadd varchar(50) 
    as 
    insert into dbo.ReikiPowerDisplay 
    select Reiki,ReikiDescription, @emailadd from ReikiPower 
    where [email protected]; 

Posted By: Aneel Goplani. CIS. 2002. USA 
0

इस परिणाम तक पहुँचने के लिए दो तरीकों का पालन कर सकते हैं रों, पर्यावरण गुण टैब द्वारा एफआईआर और गुणों को स्थापित करने:

enter image description here

सबसे अच्छा तरीका है के रूप में एक इस प्रकार इस आशय कोड से, मेरे उदाहरण बनाने देखना है:

AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection(); 

foreach (string name in listNames) 
{  
    sourceName.Add(name); 
} 

txtName.AutoCompleteCustomSource = sourceName; 
txtName.AutoCompleteMode = AutoCompleteMode.Suggest; 
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource; 
0
private void TurnOnAutocomplete() 
    { 
     textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; 
     textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; 
     AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); 
     string[] arrayOfWowrds = new string[]; 

     try 
     { 
      //Read in data Autocomplete list to a string[] 
      string[] arrayOfWowrds = new string[]; 
     } 
     catch (Exception err) 
     { 
      MessageBox.Show(err.Message, "File Missing", MessageBoxButtons.OK, MessageBoxIcon.Error); 
     } 

     collection.AddRange(arrayOFWords); 
     textBox.AutoCompleteCustomSource = collection; 
    } 

स्वत: पूर्ण सूची के लिए आपके डेटा की आवश्यकता होने के बाद आपको केवल इसे कॉल करने की आवश्यकता है। एक बार बाध्य होने पर यह टेक्स्टबॉक्स के साथ रहता है। जब भी टेक्स्टबॉक्स में टेक्स्ट बदल जाता है, तो आपको इसे कॉल करने या कॉल करने की आवश्यकता नहीं होती है, जो आपके प्रोग्राम को मार देगा।

संबंधित मुद्दे