Auto fill text box in WPF

Below code is helpful to WPF developer to create auto fill text box. Like we are want to create Client list in drop-down but that list is very large so we can provide auto fill text box where user enter any character and we can show relevant list in below of the text box.

I have created one user control in WCP so any can use with set appropriate properties.

Below are the UI for this controls.

Now below the xmal for this controls…

<UserControl x:Class=”MyProject.AutoFillTaxBox”
xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation&#8221;
xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml&#8221;
Height=”80″ Width=”233″>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”84*” />
<ColumnDefinition Width=”4*” />
<ColumnDefinition Width=”145*” />
</Grid.ColumnDefinitions>
<TextBox x:Name=”txtSearchtext” Height=”25″ VerticalAlignment=”Top” Grid.ColumnSpan=”3″ KeyDown=”txtSearchtext_KeyDown” PreviewKeyDown=”txtSearchtext_PreviewKeyDown” />
<ListBox x:Name=”SuggetionList” Margin=”0,25,0,9″ Visibility=”Collapsed” SelectionChanged=”SuggetionList_SelectionChanged” ScrollViewer.CanContentScroll=”True” Grid.ColumnSpan=”3″ />
</Grid>
</UserControl>

Also below are the code for this..

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace MyProject
{
/// <summary>
/// Interaction logic for AutoFillTaxBox.xaml
/// </summary>
public partial class AutoFillTaxBox : UserControl
{
public delegate void SetIDs(int ID);
public SetIDs functionIDSet;
public AutoFillTaxBox()
{
InitializeComponent();
txtSearchtext.TextChanged += new TextChangedEventHandler(txtSearchtext_TextChanged);
}
private List<SuggestedList> _DataSource = null;
private string _SelectedText;
private int _SelectedID;
public List<SuggestedList> DataSource
{
set
{
_DataSource = value;
}
get
{
return _DataSource;
}
}
public string SelectedText
{
set
{
txtSearchtext.Text = value;
}
get
{
return txtSearchtext.Text.Trim();
}
}
public int SelectedID
{
set
{
_SelectedID = value;
functionIDSet.Invoke(_SelectedID);
}
get
{
return _SelectedID;
}
}
private void txtSearchtext_TextChanged(object sender, TextChangedEventArgs e)
{
List<SuggestedList> oAutoFillList = new List<SuggestedList>();
oAutoFillList.Clear();
foreach (SuggestedList item in _DataSource)
{
if(!string.IsNullOrEmpty(txtSearchtext.Text))
{
if (item.Text.ToUpper().StartsWith(txtSearchtext.Text.ToUpper()))
{
oAutoFillList.Add(item);
}
if (item.Text.ToUpper() == txtSearchtext.Text.ToUpper())
{
_SelectedID = item.ID;
}
}
}
if (oAutoFillList.Count > 0)
{
SuggetionList.ItemsSource = oAutoFillList;
SuggetionList.DisplayMemberPath = "Text";
SuggetionList.Visibility = Visibility.Visible;
}
else
{
SuggetionList.Visibility = Visibility.Collapsed;
SuggetionList.ItemsSource = null;
}
}
private void SuggetionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (SuggetionList.ItemsSource != null)
{
SuggetionList.Visibility = Visibility.Collapsed;
// Remove event handler because that shoudl not be fire up to execution
txtSearchtext.TextChanged -= new TextChangedEventHandler(txtSearchtext_TextChanged);
if (SuggetionList.SelectedIndex != -1)
{
txtSearchtext.Text = ((SuggestedList)(SuggetionList.SelectedItem)).Text;
this.SelectedID = ((SuggestedList)(SuggetionList.SelectedItem)).ID;
}
/// Add event handler after change text value.
txtSearchtext.TextChanged += new TextChangedEventHandler(txtSearchtext_TextChanged);
}
}
private void txtSearchtext_KeyDown(object sender, KeyEventArgs e)
{
if (SuggetionList.ItemsSource != null && e.SystemKey == Key.LeftAlt)
{
SuggetionList.Focus();
}
}
private void txtSearchtext_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (SuggetionList.ItemsSource != null && e.SystemKey == Key.LeftAlt)
{
SuggetionList.Focus();
}
}
}
public class SuggestedList
{
public SuggestedList(int iID, string sText)
{
this.ID = iID;
this.Text = sText;
}
public int ID { set; get; }
public string Text { set; get; }
public override string ToString()
{
return this.Text;
}
}
}

So here you can use public property to create this control as generic and use it….
  • DataSource
  • Delegate to handle get any of the custom event on selection change.
  • selected ID
  • set Selected text in case if edit..
Let me know for more details on this control.
About these ads

Bi directional update data collection

I have data in form of list of collection as below code. With this article we can see how data directly update in collection, no required to write specific code for assign in variable and then pass that variable in business logic to update data.

With this list object always stay update as made changes in UI.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IMS.DataAccess;

namespace IMS.BusinessLogic
{
public class Payment
{

public List GetPaymentList(int? InvoiceID)
{
List oPaymentList = new List();

DataSet dsPayment = new DataSet();
using (DataAccess.DatabaseHelper objDBHelper = new DataAccess.DatabaseHelper())
{
if (InvoiceID != null && InvoiceID > 0)
{
objDBHelper.AddParameter("PaymentID", InvoiceID);
}
dsPayment = objDBHelper.ExecuteDataSet("GetPaymentList", CommandType.StoredProcedure);
}

if (dsPayment.Tables.Count > 0 && dsPayment.Tables[0].Rows.Count > 0)
{
foreach (DataRow item in dsPayment.Tables[0].Rows)
{
PaymentInfo oPaymentInfo = new PaymentInfo();
oPaymentInfo.PaymentID = Int32.Parse(item["PaymentID"].ToString());
oPaymentInfo.PaymentDate = DateTime.Parse(item["PaymentDate"].ToString());
oPaymentInfo.TrasferCharges =

if (dsPayment.Tables.Count == 2 && dsPayment.Tables[1].Rows.Count > 0)
{
DataRow[] RowList = dsPayment.Tables[1].Select("PaymentID=" + oPaymentInfo.PaymentID);
List oPayDetailsList = new List();
oPaymentInfo.PaymentDetailList = oPayDetailsList;
foreach (DataRow itemRow in RowList)
{
PaymentDetailsInfo oPaymentInfoDetails = new PaymentDetailsInfo();
oPaymentInfoDetails.PaymentDetailsID = Int32.Parse(itemRow["PaymentDetailsID"].ToString());
oPaymentInfoDetails.PaymentID = Int32.Parse(itemRow["PaymentID"].ToString());
oPaymentInfoDetails.PaymentAmount = Decimal.Parse(itemRow["PayAmount"].ToString());
oPaymentInfoDetails.InvoiceID = Int32.Parse(itemRow["InvoiceID"].ToString());
oPaymentInfoDetails.DueAmount = Decimal.Parse(itemRow["PendingAmount"].ToString());
oPaymentInfoDetails.PaidAmount = Decimal.Parse(itemRow["PaidAmount"].ToString());
oPaymentInfoDetails.InvoiceNo = itemRow["InvoiceNo"].ToString();

if (! oPaymentInfo.InvoiceNos.Contains(oPaymentInfoDetails.InvoiceNo))
{
oPaymentInfo.InvoiceNos += "," + oPaymentInfoDetails.InvoiceNo;
}
oPaymentInfo.PaymentDetailList.Add(oPaymentInfoDetails);
}

oPaymentInfo.InvoiceNos = oPaymentInfo.InvoiceNos.Trim(",".ToCharArray());
}

oPaymentList.Add(oPaymentInfo);

}

}
return oPaymentList;
}

public class PaymentInfo
{
public int PaymentID { get; set; }
public DateTime PaymentDate { get; set; }
public decimal TotalPaymentAmount
{
get
{
return this.PaymentDetailList.Sum(P => P.PaymentAmount);
}
}
public string InvoiceNos { get; set; }
public int CurrencyID { get; set; }
public int CreatedID { get; set; }
public List PaymentDetailList { get; set; }

}
public class PaymentDetailsInfo
{
public int PaymentDetailsID { get; set; }
public int PaymentID { get; set; }
public int InvoiceID { get; set; }
public string InvoiceNo { get; set; }
public decimal PaymentAmount { get; set; }
public decimal InvoiceAmount { get; set; }
public decimal PaidAmount { get; set; }
public decimal DueAmount { get; set; }
}
}

This Data I am going to bind in my datagridview there I have added 5-7 fieds out of that one the field PaymentAmount text box with readonly=false property.

dgPaymentList.Visible = true;
Payment oPayment = new Payment();
List oPaymentList = oPayment.GetPaymentList(null);
dgPaymentList.AutoGenerateColumns = false;
dgPaymentList.RowHeadersVisible = false;
dgPaymentList.DataSource = oPaymentList;

Hence when I am doing any changes in that textbox that directly update my collection and one call update method which is in business class that directly update data by this collection entity.

Let me know if required any more information on this.

Export from Silverlight

Below is the code for export data from silver light code.

With this code user can export data in (csv formate) by very fast performance results.

In this code need to read data from grid and convert in one csv format as tab separator. And that file data need to save in one file with xls extension with save dialog box by silver light So user can save it any where in client machine.

This Process gives very efficient results because all code executes in client machine by Silver light.
Read more of this post

Follow

Get every new post delivered to your Inbox.

Join 239 other followers