hsivaram

Hi,


If i enter any alphabet say 'j' in textbox, list of names starting with 'j' should be displayed in same textbox as list below........i have that list of names in database......How can i do that




Re: Visual Basic Language List of names from database

decyclone

Hi kadambari,

try this :

public partial class Form1 : Form
{
static string[] NameSource = new string[] { "Joe", "John", "Mike", "Steve", "Stuart", "Michael", "Tom", "Tim", "Alex", "Rick" };

public Form1()
{
InitializeComponent();
AutoCompleteStringCollection Names = new AutoCompleteStringCollection();
Names.AddRange(Form1.NameSource);
textBox1.AutoCompleteCustomSource = Names;
}

private string[] GetDataSource()
{
List<string> Names = new List<string>();
foreach (string Name in Form1.NameSource)
{
if (Name.StartsWith(textBox1.Text, StringComparison.CurrentCultureIgnoreCase))
{
Names.Add(Name);
}
}
return Names.ToArray();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
AutoCompleteStringCollection Names = new AutoCompleteStringCollection();
Names.AddRange(GetDataSource());

textBox1.AutoCompleteCustomSource = Names;
}
}





Re: Visual Basic Language List of names from database

DazPinto

skadambari wrote:
Hi,


If i enter any alphabet say 'j' in textbox, list of names starting with 'j' should be displayed in same textbox as list below........i have that list of names in database......How can i do that


This code will make a dataset and fill a combo box with the names that have been retrieved e.g. all names that begin with 'J'

Type this at the very top of your code:

Imports ss = System.Data.SqlClient

Make:

  • 1 textbox call 'txtByName' (where the user types in the search)
  • 1 combobox called 'cmbByName' (returns values)
  • A datagrid (returns values in a table)

Code Snippet

Dim cn As New System.Data.OleDb.OleDbConnection
Dim cm As New System.Data.OleDb.OleDbCommand
Dim rd As System.Data.OleDb.OleDbDataReader

Private Sub txtbyName_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtbyName.TextChanged cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=purchases.mdb"

Try
cn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
cmbbyName.Items.Clear()
cm.Connection = cn
cm.CommandText = "select * from pDetails where Name like '" & txtbyName.Text & "%'"
rd = cm.ExecuteReader()
cmbbyName.Items.Clear()
While (rd.Read)
cmbbyName.Items.Add(rd("Name"))
End While
rd.Close()
If cmbbyName.Items.Count > 0 Then
cmbbyName.SelectedIndex = 0
End If
Try
Dim adp As New Data.OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim dv As New DataView
adp.SelectCommand = cm
adp.Fill(ds, "dataset")
dv.Table = ds.Tables(0)
dv.Sort = "name"
DataGrid1.DataSource = dv
DataGrid1.Refresh()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=purchases.mdb"
Try
cn.Open()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
cmbbyName.Items.Clear()
cm.Connection = cn
cm.CommandText = "select * from pDetails where Name like '" & txtbyName.Text & "%'"
rd = cm.ExecuteReader()
cmbbyName.Items.Clear()
While (rd.Read)
cmbbyName.Items.Add(rd("Name"))
End While
rd.Close()
If cmbbyName.Items.Count > 0 Then
cmbbyName.SelectedIndex = 0
End If

'Populate the datagrid
Try
Dim adp As New Data.OleDb.OleDbDataAdapter
Dim ds As New DataSet
Dim dv As New DataView
adp.SelectCommand = cm
adp.Fill(ds, "dataset")
dv.Table = ds.Tables(0)
dv.Sort = "name"
DataGrid1.DataSource = dv
DataGrid1.Refresh()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End Sub

Hope this Helps!





Re: Visual Basic Language List of names from database

decyclone

decyclone wrote:
Hi kadambari,

try this :

public partial class Form1 : Form
{
static string[] NameSource = new string[] { "Joe", "John", "Mike", "Steve", "Stuart", "Michael", "Tom", "Tim", "Alex", "Rick" };

public Form1()
{
InitializeComponent();
AutoCompleteStringCollection Names = new AutoCompleteStringCollection();
Names.AddRange(Form1.NameSource);
textBox1.AutoCompleteCustomSource = Names;
}

private string[] GetDataSource()
{
List<string> Names = new List<string>();
foreach (string Name in Form1.NameSource)
{
if (Name.StartsWith(textBox1.Text, StringComparison.CurrentCultureIgnoreCase))
{
Names.Add(Name);
}
}
return Names.ToArray();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{
AutoCompleteStringCollection Names = new AutoCompleteStringCollection();
Names.AddRange(GetDataSource());

textBox1.AutoCompleteCustomSource = Names;
}
}


Sorry, I posted the code in C#.

Here is VB Code :

Class Form4
Inherits Form

Private Shared NameSource As String() = New String() {"Joe", "John", "Mike", "Steve", "Stuart", "Michael", "Tom", "Tim", "Alex", "Rick"}

Public Sub New()
InitializeComponent()
Dim Names As New AutoCompleteStringCollection()
Names.AddRange(Form4.NameSource)
TextBox1.AutoCompleteCustomSource = Names
End Sub

Private Function GetDataSource() As String()
Dim Names As New List(Of String)()
For Each Name As String In Form4.NameSource
If (Name.StartsWith(TextBox1.Text, StringComparison.CurrentCultureIgnoreCase)) Then
Names.Add(Name)
End If
Next
Return Names.ToArray()
End Function

Private Sub textBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles TextBox1.TextChanged
Dim Names As New AutoCompleteStringCollection()
Names.AddRange(GetDataSource())
TextBox1.AutoCompleteCustomSource = Names
End Sub

End Class

Just make sure, textBox1's following properties with given values :

AutoCompleteMode is set to anything but none
AutoCompleteSource is set to CustomSource

And you can fill the NameFields with any data from database. But you might have to change it's type from String array to some sort of generic string list or string collection.

Hope it helps.





Re: Visual Basic Language List of names from database

skadambari

Thanks