+92 332 4229 857 99ProjectIdeas@Gmail.com

Searching In ListBox (C#.net)




The following code shows you how to search in Listbox, first loading all words from file to listbox, then user enter word in textbox, and every key press word is searched and that word is displayed on image in small and capital letters.







using System.Drawing;
using System.IO;
using System.Drawing.Imaging;

// This Function Load All Words From File To ListBox
 public void LoadWordsFromFile()
 {
    try
        {
                    
          TextReader tr = File.OpenText("abc.txt");
           while (tr.ReadLine() != null)
           {
                ListBox1.Items.Add(tr.ReadLine());
           }
                          
        }
        catch (Exception ex)
        {
             MessageBox.Show(ex.Message);
        }
                    
}

 public void Form1_Load(System.Object sender, System.EventArgs e)
 {
     try
         {
                          
           LoadWordsFromFile(); //load allWords from file to listbox
           Timer1.Interval = 100;
           Timer1.Start();
           image = new Bitmap("image.bmp");
                          
         }
         catch (Exception ex)
         {
             MessageBox.Show(ex.Message);
         }
                    
 }

// This Function Writes Text On Image At Specified Location
 public Bitmap AddTextToImage(Bitmap bImg, string smallLetters, string capitalLetters)
 {
                    
    Bitmap tmpImage = new Bitmap(bImg, new Size(bImg.Width, bImg.Height));
    Graphics graphic = Graphics.FromImage(tmpImage);
    SolidBrush brush = new SolidBrush(Color.Black);
    graphic.DrawString(smallLetters, new Font("Times New Roman", 12, FontStyle.Bold), brush, new PointF(20, 25));
    graphic.DrawString(capitalLetters, new Font("Times New Roman", 12, FontStyle.Bold), brush, new PointF(20, 50));
    graphic.Dispose();
                    
    return tmpImage;
                    
 }

            
// At every key you press in textbox it finds that word in Listbox and select it
  public void Timer1_Tick(System.Object sender, System.EventArgs e)
  {
      searchString = TextBox1.Text;
      int index = ListBox1.FindString(searchString);
  
        if (index != -1)
        {
          PictureBox1.Image = AddTextToImage(image, searchString.ToLower(),
                                                     searchString.ToUpper());
           ListBox1.SetSelected(index, true);
        
        }
                    
  }

 public void btnSearch_Click(System.Object sender, System.EventArgs e)
 {
                    
     try
         {
           if (TextBox1.Text.Equals(""))
           {
              ErrorProvider1.SetError(TextBox1, "Must Enter In TextBox");
           }
           else
           {
              ErrorProvider1.Dispose();
                    
              int index = ListBox1.FindString(TextBox1.Text);
              ListBox1.SetSelected(index, true);
              PictureBox1.Image = AddTextToImage(image, TextBox1.Text.ToLower(),
                                                     TextBox1.Text.ToUpper());
           }
                          
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
                    
 }
             




0 comments: