combobox Autocomplete doesn't work - c#

I want to create a auto complete tools using combobox.
So i just add some items to my combobox .And set these items as a source of my combobox.
In form_load i do this:
private void frmInvoice_Load(object sender, EventArgs e)
{
comboBox1.AutoCompleteMode=AutoCompleteMode.Append;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
But it doesn't work and when i type a letter the whole word doesn't appear in combobox.Why ?
i follow this link :http://www.c-sharpcorner.com/UploadFile/mahesh/AutoCompletion02012006113508AM/AutoCompletion.aspx
best regards.

Since you've declared CustomSource for auto completion, you should provide that source:
private void frmInvoice_Load(object sender, EventArgs e)
{
comboBox1.AutoCompleteMode=AutoCompleteMode.Append;
comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection data = new AutoCompleteStringCollection();
// Put here the auto completions' e.g.
data.Add("My String 1");
data.Add("Autocompletion 2");
data.Add("Some stuff");
comboBox1.AutoCompleteCustomSource = data;
}

You didn't upload your CustomSource.
public Form1()
{
InitializeComponent();
this.comboBox1.AutoCompleteCustomSource.AddRange
(new string[] {"Raj Beniwal", "Rohit Malhotra", "Ronit Singh", "Ravi Kumar",
"Rohit Behl", "Sanjay Singh", "Shalini Singh", "Seema Malhotra", "Savi Verma",
"Karan Kappor", "Kapil Malhotra", "Vikash Nanda", "Vikram Jain", "Amit Garg",
"Atul Wadhwani", "Ashwani Pandey"
});
this.comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
this.comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
}
Refference from : http://www.c-sharpcorner.com/Blogs/2050/autocomplete-combobox-in-visual-C-Sharp-2010.aspx

What I have done is use a 3rd party dlls. These are of Telerik. My code is as follows
<telerik:RadComboBox x:Name="radComboBox" VerticalAlignment="Top" Visibility="Visible" AllowDrop="True"
ItemsSource="{Binding AvailList}" SelectedItem="{Binding SelectedComboboxItem, Mode=TwoWay}"
IsEditable="True"
telerik:TextSearch.TextPath="DisplayName" Height="17" Margin="10,34,39,0" />
This is in xaml. It directly reads from the ItemSource and does the autocompletion.

Or you can do this...
private void LoadStuffNames()
{
try
{
string Query = "select stuff_name from dbo.stuff";
string[] names = GetColumnData_FromDB(Query);
comboName.AutoCompleteMode = AutoCompleteMode.Suggest;
comboName.AutoCompleteSource = AutoCompleteSource.CustomSource;
AutoCompleteStringCollection x = new AutoCompleteStringCollection();
if (names != null && names.Length > 0)
foreach (string s in names)
x.Add(s);
comboName.AutoCompleteCustomSource = x;
}
catch (Exception ex)
{
}
finally
{
}
}
Cheers..

Related

How to define my own filter while working with Telerik RadGridView control?

I'm working on a C# application, where I'm trying to create my own filtering mechanism for filtering a Telerik RadGridView control.
My RadGridView control looks as follows (in the xaml file, I've left some open lines for showing the part where I define my filter):
<telerik:RadGridView x:Name="PartsGridView"
AutoGenerateColumns="False"
CanUserFreezeColumns="False"
FilteringMode="Popup"
FilterOperatorsLoading="Grid_FilterOperatorsLoading"
ItemsSource="{Binding PagedSource, ElementName=PartsRadDataPager}"
IsSynchronizedWithCurrentItem="False"
SelectedItem="{Binding SelectedPart}"
SelectionMode="Single"
ShowInsertRow="False"
EnableRowVirtualization="True"
RowLoaded="PartsGridView_RowLoaded"
FieldFilterEditorCreated="FieldFilterEditorCreated"
IsFilteringAllowed="True"
DistinctValuesLoading="PartsGridView_DistinctValuesLoading"
Grouped="PartsGridView_Grouped">
<telerik:RadGridView.GroupDescriptors>
<telerik:GroupDescriptor Member="Name" DisplayContent="Article"/>
</telerik:RadGridView.GroupDescriptors>
<telerik:RadGridView.FilterDescriptors/>
<telerik:RadGridView.Columns>
...
<telerik:GridViewDataColumn DataMemberBinding="{Binding Article.CustomerReference}"
Header="Client"
IsReadOnly="True"
Name="Client">
<telerik:GridViewDataColumn.FilteringControl>
<local:Myfilter Column="{Binding ElementName=Client}"/>
</telerik:GridViewDataColumn.FilteringControl>
</telerik:GridViewDataColumn>
</telerik:RadGridView.Columns>
</telerik:RadGridView>
The corresponding source code is as follows (I've only shown the OnFilter, it's the idea to extend the filtering possibilities to regular expressions, but let's start making the regular filter work first):
public partial class MyFilter : System.Windows.Controls.UserControl,
IFilteringControl
{
private GridViewDataColumn column;
private CompositeFilterDescriptor compositeFilter;
private Telerik.Windows.Data.FilterDescriptor textFilter;
private void OnFilter(object sender, RoutedEventArgs e)
{
//var reg = new Regex(textBox.Text.Replace("*", ".*"));
// var descriptor = new FilterDescriptor<string>
// { FilteringExpression = o => reg.IsMatch(o) };
compositeFilter = new CompositeFilterDescriptor();
textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
, Telerik.Windows.Data.FilterOperator.IsEqualTo
, null);
compositeFilter.FilterDescriptors.Add(textFilter);
textFilter.Value = TextBox;
if (!Column.DataControl.FilterDescriptors.Contains(compositeFilter))
{
Column.DataControl.FilterDescriptors.Add(compositeFilter);
}
IsActive = true;
}
When debugging, I go into the OnFilter() function but instead of just filtering, I don't see any data in the RadGridView anymore.
Does anybody have an idea what I'm doing wrong?
Edit: In case the question is not clear, don't hesitate asking for more information.
Try this:
private void OnFilter(object sender, RoutedEventArgs e)
{
//var reg = new Regex(textBox.Text.Replace("*", ".*"));
// var descriptor = new FilterDescriptor<string>
// { FilteringExpression = o => reg.IsMatch(o) };
//compositeFilter = new CompositeFilterDescriptor();
textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name
, Telerik.Windows.Data.FilterOperator.IsEqualTo
, null);
//compositeFilter.FilterDescriptors.Add(textFilter);
textFilter.Value = TextBox;
if (!Column.DataControl.FilterDescriptors.Contains(textFilter))
{
Column.DataControl.FilterDescriptors.Add(textFilter);
}
IsActive = true;
}
u can also try this:
private void OnFilter(object sender, RoutedEventArgs e)
{
compositeFilter = new CompositeFilterDescriptor();
textFilter = new Telerik.Windows.Data.FilterDescriptor(Column.Name, Telerik.Windows.Data.FilterOperator.IsEqualTo, null);
compositeFilter.FilterDescriptors.Add(textFilter);
textFilter.Value = TextBox;
if (!Column.DataControl.FilterDescriptors.Contains(compositeFilter))
{
Column.DataControl.FilterDescriptors.Add(compositeFilter);
}
IsActive = true;
}

why when use listView some controls in page disappeared?

I use SearchBar to search from listView and I have other controls
but when page load just SearchBar appear and other controls disappear
like this image
and when search then select from listView the disappeared controls appears
like this image
my xaml code :
<StackLayout>
<SearchBar x:Name="search_trade"
TextChanged="search_trade_TextChanged"
/>
<ListView x:Name="list" ItemSelected="list_ItemSelected" >
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Name}" ></TextCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Editor x:Name="edit_trade"
FontSize="14"/>
</StackLayout>
my c# code :
private void search_trade_TextChanged(object sender, TextChangedEventArgs e)
{
if (search_trade.Text.Trim()==string.Empty)
{
list.IsVisible = false;
}
list.IsVisible = true;
if (string.IsNullOrEmpty(e.NewTextValue))
{
list.ItemsSource = tempdata;
}
else
{
list.ItemsSource = tempdata.Where(x => x.Name.ToLower().StartsWith(e.NewTextValue));
}
}
private void list_ItemSelected(object sender, SelectedItemChangedEventArgs e)
{
var item = (Contacts)e.SelectedItem;
edit_trade.Text = item.Name.ToString();
search_trade.Text = "";
list.IsVisible = false;
edit_trade.IsReadOnly = true;
}
code c# that I initially set the ItemsSource :
public Anti_biotic()
{
InitializeComponent();
active();
years_months();
frame_view.IsVisible = false;
trade();
}
private void trade()
{
tempdata = new List<Contacts>
{
new Contacts() { Name = "Amoclawin228.5mg/5mlsusp60ml"},
new Contacts() { Name = "Amoclawin 457mg / 5ml susp 60 ml"},
new Contacts() { Name = "Amoflux 250 mg susp 100 ml"},
};
}
how can I solve this issue?
you are not assigning the ListView's ItemsSource when the page loads. If the ListView doesn't have any data then it won't display anything
public Anti_biotic()
{
InitializeComponent();
active();
years_months();
frame_view.IsVisible = false;
// this method creates tempdata but doesn't do anything with it
trade();
// add this
list.ItemsSource = tempdata;
}

Enable `CanUserAddRows` with dynamically created `DataGrid` columns - WPF

The DataGrid columns are dynamically created in code behind. In order to bind the columns dynamically I have a Dictionary<string, obj> property, with the key as the column header, and the values.
and the columns gets bound to the dictionary like this:
var item = new DataGridTextColumn();
item.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
item.Header = name;
item.Binding = new Binding($"di[{name}].obj") { Mode = BindingMode.TwoWay };
It works fine, but I couldn't figure out how to get the values from the new row form DataGrid
I've created a test project and ExpandoObject worked for me. I was able to display dynamic column "FirstName" and also add new rows by editing grid rows
XAML:
<Grid Margin="0,0,0,56" Loaded="Grid_Loaded_1">
<DataGrid Name="MyGrid" ItemsSource="{Binding Items}" HorizontalAlignment="Left" Margin="69,33,0,0" VerticalAlignment="Top" Height="220" Width="389"/>
<Button Content="Button" HorizontalAlignment="Left" Height="22" Margin="339,286,0,-45" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
</Grid>
CS:
public partial class MainWindow : Window
{
public ViewModel Vm { get; set; }
public MainWindow()
{
InitializeComponent();
}
private void Grid_Loaded_1(object sender, RoutedEventArgs e)
{
Vm = new ViewModel();
Vm.Items = new ObservableCollection<ExpandoObject>();
DataContext = Vm;
var fieldName = "FirstName";
var item = new ExpandoObject() as IDictionary<string, Object>;
item.Add(fieldName, "Adam"); // Dynamically adding new fields
var eoItem = item as ExpandoObject;
Vm.Items.Add(eoItem);
// Dynamically adding new columns
var col = new DataGridTextColumn();
col.Width = new DataGridLength(1, DataGridLengthUnitType.Auto);
col.Header = fieldName;
col.Binding = new Binding(fieldName) { Mode = BindingMode.TwoWay };
MyGrid.Columns.Add(col);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
}
}
public class ViewModel
{
public ObservableCollection<ExpandoObject> Items { get; set; }
}
I had similar problem some time ago. Below You can find my solution. Basically I needed to change AddingNewRow event. You will need to change types etc. to meet your needs but this should be rather easy.
private void AddingNewRow(object sender, AddingNewItemEventArgs e)
{
DictionaryRowData newRow = new DictionaryRowData();
for (int i = 0; i < dictionaryData.Columns.Count; i++)
{
newRow.Cells.Add(new DictionaryCellData());
}
e.NewItem = newRow;
}
Sorry, for not matching exactly your case, but I don't have time right now. I hope it will help

wpf combobox not updating properly

I am new to wpf and fighting with this issue for several days. I have a combobox which has dataview as itemssource. It displays the values correctly but selecteditem is always null when i re-run the application.
ChargeCodeValidValues objects that combobox is bound to:
public class ChargeCodeValidValues
{
#region Properties
public DataTable ChargeCodesValidValuesTable { get; set; }
public DataView dvChargeCodeValidValues { get; set; }
#endregion
#region Constructor
public ChargeCodeValidValues()
{
LoadChargeCodesValidValues();
}
public void LoadChargeCodesValidValues()
{
Database db = new Database();
DataTable dataTable = db.ExecuteQuery("upGet_ChargeCodesValidValues", "ChargeCodesValidValues", "ID");
this.ChargeCodesValidValuesTable = dataTable;
this.dvChargeCodeValidValues = ChargeCodesValidValuesTable.DefaultView;
}
XAML:
<DataTemplate x:Key="combodescriptionTemplate">
<ComboBox Name="cboCombo"
Loaded="cboCombo_Loaded">
<ComboBox.DataContext>
<Objects:ChargeCodeValidValues/>
</ComboBox.DataContext>
</ComboBox>
</DataTemplate>
<local:TotalCellTemplateSelector x:Key="totalcellTemplateSelector"
combodescriptionTemplate="{StaticResource combodescriptionTemplate}"/>
gridview column :
<dxg:GridColumn Header="Description" FieldName="Description" Width="Auto" MinWidth="132" AllowMoving="False" CellTemplateSelector="{StaticResource totalcellTemplateSelector}" /> -->
this column is textbox by default. It changes to combobox based on values from other column
code:
private void cboCombo_Loaded(object sender, RoutedEventArgs e)
{
ComboBox cmb = sender as ComboBox;
DataTable dtChargeCodeValidValues = oChargeCodesValidvalues.ChargeCodesValidValuesTable.Copy();
DataView dvCurrentCodeValues = dtChargeCodeValidValues.Copy().DefaultView;
cmb.ItemsSource = dvCurrentCodeValues;
cmb.DisplayMemberPath = "Description";
cmb.SelectedValuePath = "Description";
cmb.SelectedValue = "Description";
}
There is no magic that will persist a selection in a ComboBox across different invocations of your application; you'll have to write the code yourself. For example:
<ComboBox
x:Name="comboBox"
SelectionChanged="OnSelectionChanged"
Loaded="OnComboBoxLoaded"
>
<ComboBox.Items>
<system:String>One</system:String>
<system:String>Two</system:String>
<system:String>Three</system:String>
</ComboBox.Items>
</ComboBox>
...with the following code-behind:
private void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
{
int index = comboBox.SelectedIndex;
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = store.CreateFile("selectedIndex"))
{
using (BinaryWriter writer = new BinaryWriter(stream))
{
writer.Write(index.ToString());
}
}
}
}
private void OnComboBoxLoaded(object sender, RoutedEventArgs e)
{
int index = GetPreviousIndex();
comboBox.SelectedIndex = index;
comboBox.Text = comboBox.Items[index] as string;
}
private int GetPreviousIndex()
{
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = store.OpenFile("selectedIndex", FileMode.Open))
{
using (BinaryReader reader = new BinaryReader(stream))
{
try
{
return int.Parse(reader.ReadString());
}
catch (Exception x)
{
return -1;
}
}
}
}
}
Note that setting the SelectedIndex is insufficient; you have to set the Text property as well, or you won't see the selection without opening the ComboBox.
There are many ways of persisting the selection, of course; here I just cobbled something together quickly (and somewhat dirtily).

LookUpEdit does not work

I have XtraTabControl with two pages, both of them has one LookUpEdit,
when page load which on the secondpage does not work,
void Frm1_Load(object sender, EventArgs e)
{
lookUpEditA.Properties.DataSource = datasource. . . . .
lookUpEditA.Properties.ValueMember = "ID";
lookUpEditA.Properties.DisplayMember = "xxxx";
lookUpEditA.Properties.PopulateColumns();
lookUpEditA.Properties.Columns["ID"].Visible = false;
lookUpEditB.Properties.DataSource = datasource. . . . .
lookUpEditB.Properties.ValueMember = "ID";
lookUpEditB.Properties.DisplayMember = "xxxx";
lookUpEditB.Properties.PopulateColumns();
lookUpEditB.Properties.Columns["ID"].Visible = false;
}
I can see the issue only with setting visibility of 'ID' column on second LookUpEdit.
The reason of this issue is that the LookUpEdit can't operate with datasource representation (perform populating columns, operating with column's visibility and etc.) until it's handle been created. The second LookUpEdit will create it's handle only when the second tab page has been shown.
To avoid the issue you can use the following approach:
if(!lookUpEditB.IsHandleCreated)
lookUpEditB.HandleCreated += lookUpEditB_HandleCreated;
else InitLookUpEditDataSource();
//...
void lookUpEditB_HandleCreated(object sender, EventArgs e) {
lookUpEditB.HandleCreated -= lookUpEditB_HandleCreated;
InitLookUpEditDataSource();
}
void InitLookUpEditDataSource() {
lookUpEditB.Properties.DataSource = this.categoriesBindingSource;
lookUpEditB.Properties.DisplayMember = "CategoryName";
lookUpEditB.Properties.ValueMember = "CategoryID";
lookUpEditB.Properties.PopulateColumns();
lookUpEditB.Properties.Columns["CategoryID"].Visible = false;
}
As #DmitryG said, you can not use lookUpEditB.Properties.PopulateColumns() statement until control's UI handlers are not created.
As per my understanding these are created only when the second tab page shown. Except creating conditional statement to create handlers etc, you can use the XtraTabControl.SelectedPageChanged Event where you can bind the data source of the lookUpEditB by checking condition that XtraTabControl.SelectedTabPage Property is set with Page2 which contain the lookUpEditB.
Check the tested Code Snippet below:
public partial class TabControlTest : Form
{
List<Category> dataSource = new List<Category>();
public TabControlTest()
{
InitializeComponent();
for (int i = 0; i < 10; i++)
{
dataSource.Add(new Category { ID = i + 1, Name = "Category" + (i + 1) });
}
}
private void TabControlTest_Load(object sender, EventArgs e)
{
lookUpEditA.Properties.DataSource = dataSource;
lookUpEditA.Properties.ValueMember = "ID";
lookUpEditA.Properties.DisplayMember = "Name";
lookUpEditA.Properties.PopulateColumns();
lookUpEditA.Properties.Columns["ID"].Visible = false;
}
private void xtraTabControl1_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
{
if (xtraTabControl1.SelectedTabPage == xtraTabPage2)
{
lookUpEditB.Properties.DataSource = dataSource;
lookUpEditB.Properties.ValueMember = "ID";
lookUpEditB.Properties.DisplayMember = "Name";
lookUpEditB.Properties.PopulateColumns();
lookUpEditB.Properties.Columns["ID"].Visible = false;
}
}
}
Hope this help.

Categories