This is a migrated thread and some comments may be shown as answers.

AutoCompleteBox SelectedValues

8 Answers 156 Views
AutoCompleteBox
This is a migrated thread and some comments may be shown as answers.
Exchange
Top achievements
Rank 1
Exchange asked on 01 Apr 2013, 05:44 PM
How do you retrieve the selected values for the auto complete box??

I  have the DisplayMember and DisplayValue set:
racSchTechs.AutoCompleteDataSource = datatable;
racSchTechs.AutoCompleteDisplayMember = "Name_Full";
racSchTechs.AutoCompleteValueMember = "id_lanid";


but I want to get the "id_lanid" from the items selected. I do not see this functionality. 

Thank you!

8 Answers, 1 is accepted

Sort by
0
Stefan
Telerik team
answered on 04 Apr 2013, 01:59 PM
Hi,

Thank you for writing.

The selected items in the control can be found in the Items collection. Each item contain text and a value, so you can get both the display and the value member.
radAutoCompleteBox1.Items[1]

I hope this helps.
 

Regards,
Stefan
the Telerik team
WinForms Q1 2013 boasts PivotGrid, PDF Viewer, Chart enhancements and more. Check out all of the latest highlights.
0
Tommy
Top achievements
Rank 1
answered on 29 Aug 2013, 02:28 AM
hi,

How are you suppose to know the item number to get the value

AutoDoorComplete.AutoCompleteDataSource = DoorNameList;

AutoDoorComplete.AutoCompleteDisplayMember = "DoorName";

AutoDoorComplete.AutoCompleteValueMember = "DoorID";



When I select a DoorName I want the selected AutoCompleteValueMember DoorID

DoorID = Convert.ToInt32(AutoUser.Items[0].Value.ToString()); How can this work if I do not know the Items[number] ?
Why is it so hard to get the value when selected ?

 As the other user said the "SELECTED Value"
Tommy

0
Stefan
Telerik team
answered on 29 Aug 2013, 05:58 AM
Hello Tommy,

Thank you for writing.

RadAutoCompleteBox can have more than one item selected at a time. Please have a look at the attached picture (2013-08-29_0849.png), where the control have three values selected (name0, name2, name9). The selected items are stored in the Items collection of the control, so at this moment it has three items in it. Each item has a Text property which holds the value from the field specified as AutoCompleteDisplayMember and a Value property which holds the value from the field specified as AutoCompleteValueMember. Have a look at image 2013-08-29_0855.png, where you can see the selected items text and values. 

I hope that you find this information useful.
 

Regards,
Stefan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Kamelia
Top achievements
Rank 1
answered on 12 Nov 2014, 07:41 AM
Hi, Is it possible to preselect items in a radautocompletebox in windows form ???

I have some items which I want to have them in my radautocompletebox , so I can remove or add items in it . AND I need it to show me the items I had entered before.
thank you in advance
0
Stefan
Telerik team
answered on 12 Nov 2014, 11:31 AM
Hi Kamelia,

Thank you for writing.

I am not quite sure I understand what is your requirement. However, you can use the formatting event and style some of the tokens with different back color to indicate selection. Alternatively, you can select one or more consecutive items by selecting their text, as demonstrated here: http://www.telerik.com/help/winforms/editors-autocompletebox-caret-positioning-and-selection.html.

If this is not what you need, please provide more details on the precise requirement and I will do my best to help you achieve it.

Regards,
Stefan
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Jørgen Granborg
Top achievements
Rank 1
answered on 26 May 2015, 12:49 PM

I believe the OP has the same problem that we have run into:

 When we close our form, we want to save the items that are currently selected. This is easy, using the Items collection. We need to save both the Text and the Value.

When we then re-open the form, we need to restore it to the same settings as when it was closed, ie. the AutoCompleteBox should be filled with the same items that were saved when we closed it - both text AND value members of the Items collection. We can easily recreate the slected items with text values, but cannot link the ID values to these entries in the Items collection.

So the question is - how do we restore the Items collection with both Text AND Value to an AutoCompleteBox that were saved previously?

0
Jørgen Granborg
Top achievements
Rank 1
answered on 26 May 2015, 12:52 PM
// We are using a AutoCompleteDataSource to get the AutocompleteItems //
0
Dimitar
Telerik team
answered on 27 May 2015, 01:07 PM
Hi Jorgen,

Thank you for writing.

To properly restore the items you need to recreate the same data source and assign it to the autocomplete box. The following example shows how you can save and restore the items using simple csv file. The MyItem class just contains two properties (text and value). The code adds all the items to the autocomplete box when the data source is restored. This is achieved with the Text property: 
private void radButton1_Click(object sender, EventArgs e)
{
    selctedItems = radAutoCompleteBox1.Text;
 
    var csv = new StringBuilder();
 
    foreach (var item in (BindingList<MyItem>) radAutoCompleteBox1.AutoCompleteDataSource)
    {
        var first = item.Text.ToString();
        var second = item.Value.ToString();
        var newLine = string.Format("{0},{1}{2}", first, second, Environment.NewLine);
        csv.Append(newLine);  
    }
    
    File.WriteAllText("test.csv", csv.ToString());
}
 
private void radButton2_Click(object sender, EventArgs e)
{
    BindingList<MyItem> data = new BindingList<MyItem>();
    string text = "";
    var reader = new StreamReader(File.OpenRead(@"test.csv"));
    while (!reader.EndOfStream)
    {
        var line = reader.ReadLine();
        var values = line.Split(',');
 
        data.Add(new MyItem()
        {
            Text = values[0],
            Value = int.Parse(values[1])
        });
        text += values[0] + ";";
    }
 
    radAutoCompleteBox1.AutoCompleteDataSource = data;
    radAutoCompleteBox1.AutoCompleteDisplayMember = "Text";
    radAutoCompleteBox1.AutoCompleteValueMember = "Value";
 
    radAutoCompleteBox1.Text = text;
}

Could you please check the above code and see if it is suitable for your case? If not could you please specify how this approach is different from your real one?

I am looking forward to your reply.
 
Regards,
Dimitar
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
AutoCompleteBox
Asked by
Exchange
Top achievements
Rank 1
Answers by
Stefan
Telerik team
Tommy
Top achievements
Rank 1
Kamelia
Top achievements
Rank 1
Jørgen Granborg
Top achievements
Rank 1
Dimitar
Telerik team
Share this question
or