Skip to content

WF-875216 : Prepare UG document for the events fired when selecting/unselecting Tokens in SfComboBox control #1123

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions WindowsForms/ComboBox/Token.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,134 @@ sfComboBox1.Style.TokenStyle.Font = New Font(“Arial”, 10F, FontStyle.Bold)
* Using the <kbd>Down Arrow</kbd>, <kbd>Up Arrow</kbd>, and <kbd>Enter</kbd> keys, item can be selected from the combobox.
* Using the <kbd>Backspace</kbd> key, the last positioned token will be removed from the text area.
* When the <kbd>Esc</kbd> key is pressed, the drop-down area will be closed if it has been opened already.

## Events Fired When Selecting and Deselecting Tokens

The [SelectedValueChanged](https://help.syncfusion.com/cr/windowsforms/Syncfusion.WinForms.ListView.SfComboBox.html#Syncfusion_WinForms_ListView_SfComboBox_SelectedValueChanged) event is triggered whenever tokens are selected or deselected in the SfComboBox control. This allows users to handle changes in token selections effectively.

{% tabs %}
{% highlight c# %}

private List<object> _previousSelectedTokens;
private Label label;

SfComboBox sfComboBox1 = new SfComboBox
{
EnableToken = true,
DisplayMember = "Name",
ValueMember = "Id",
Size = new Size(300, 30),
DataSource = new List<dynamic>
{
new { Id = 1, Name = "Item1" },
new { Id = 2, Name = "Item2" },
new { Id = 3, Name = "Item3" }
}
};

sfComboBox1.Style.TokenStyle.Font = new Font("Arial", 9.75F, FontStyle.Regular, GraphicsUnit.Point);

// Initialize Label
label = new Label
{
Width = 200,
Location = new Point(10, 110)
};

// Initialize selected items and previous tokens
sfComboBox1.SelectedItems.Add(sfComboBox1.DropDownListView.View.DisplayItems[0]);
_previousSelectedTokens = sfComboBox1.SelectedItems.Cast<dynamic>().ToList();

sfComboBox1.SelectedValueChanged += SfComboBox1_SelectedValueChanged;

this.Controls.Add(sfComboBox1);
this.Controls.Add(label);

private void TokenComboBox_SelectedValueChanged(object sender, EventArgs e)
{
var comboBox = sender as SfComboBox;

// Get current and previous tokens
var currentSelectedTokens = comboBox.SelectedItems.Cast<dynamic>().ToList();
var addedTokens = currentSelectedTokens.Except(_previousSelectedTokens).ToList();
var removedTokens = _previousSelectedTokens.Except(currentSelectedTokens).ToList();

// Update previous tokens
_previousSelectedTokens = currentSelectedTokens;

// Handle added tokens
foreach (var token in addedTokens)
{
label.Text = $"Token Added: {token.Name}";
}

// Handle removed tokens
foreach (var token in removedTokens)
{
label.Text = $"Token Removed: {token.Name}";
}
}

{% endhighlight %}
{% highlight vb %}

Private _previousSelectedTokens As List(Of Object)
Private label As Label

' Initialize SfComboBox
Dim sfComboBox1 As New SfComboBox With {
.EnableToken = True,
.DisplayMember = "Name",
.ValueMember = "Id",
.Size = New Size(300, 30),
.DataSource = New List(Of Object) From {
New With {.Id = 1, .Name = "Item1"},
New With {.Id = 2, .Name = "Item2"},
New With {.Id = 3, .Name = "Item3"}
}
}

sfComboBox1.Style.TokenStyle.Font = New Font("Arial", 9.75F, FontStyle.Regular, GraphicsUnit.Point)

' Initialize Label
label = New Label With {
.Width = 200,
.Location = New Point(10, 110)
}

' Initialize selected items and previous tokens
sfComboBox1.SelectedItems.Add(sfComboBox1.DropDownListView.View.DisplayItems(0))
_previousSelectedTokens = sfComboBox1.SelectedItems.Cast(Of Object)().ToList()

AddHandler sfComboBox1.SelectedValueChanged, AddressOf TokenComboBox_SelectedValueChanged

Me.Controls.Add(sfComboBox1)
Me.Controls.Add(label)


Private Sub TokenComboBox_SelectedValueChanged(sender As Object, e As EventArgs)
Dim comboBox = TryCast(sender, SfComboBox)

' Get current and previous tokens
Dim currentSelectedTokens = comboBox.SelectedItems.Cast(Of Object)().ToList()
Dim addedTokens = currentSelectedTokens.Except(_previousSelectedTokens).ToList()
Dim removedTokens = _previousSelectedTokens.Except(currentSelectedTokens).ToList()

' Update previous tokens
_previousSelectedTokens = currentSelectedTokens

' Handle added tokens
For Each token In addedTokens
label.Text = $"Token Added: {token.Name}"
Next

' Handle removed tokens
For Each token In removedTokens
label.Text = $"Token Removed: {token.Name}"
Next
End Sub

{% endhighlight %}
{% endtabs %}

![Event Triggered on Token Selection or Deselection](Token_images/Token_Event.gif)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.