Skip to content

Commit 83bbd24

Browse files
Allow adding custom email
Allow adding custom email
1 parent 4cf635a commit 83bbd24

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

MultiselectComboBox/MultiselectComboBox/MainWindow.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
SelectionMode="Multiple"
1919
MultiSelectionDisplayMode="Token"
2020
PlaceholderText="Try anne"
21+
InputSubmitted="OnInputSubmitted"
2122
HorizontalAlignment="Center"
2223
VerticalAlignment="Center"
2324
Width="500">

MultiselectComboBox/MultiselectComboBox/MainWindow.xaml.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@
55
using Microsoft.UI.Xaml.Input;
66
using Microsoft.UI.Xaml.Media;
77
using Microsoft.UI.Xaml.Navigation;
8+
using Syncfusion.UI.Xaml.Editors;
89
using System;
910
using System.Collections.Generic;
1011
using System.IO;
1112
using System.Linq;
1213
using System.Runtime.InteropServices.WindowsRuntime;
14+
using System.Text.RegularExpressions;
1315
using Windows.Foundation;
1416
using Windows.Foundation.Collections;
17+
using Windows.UI.Popups;
1518

1619
// To learn more about WinUI, the WinUI project structure,
1720
// and more about our project templates, see: http://aka.ms/winui-project-info.
@@ -27,5 +30,43 @@ public MainWindow()
2730
{
2831
this.InitializeComponent();
2932
}
33+
34+
private async void OnInputSubmitted(object sender, ComboBoxInputSubmittedEventArgs e)
35+
{
36+
var emailString = e.Text;
37+
bool isEmail = Regex.IsMatch(emailString, @"\A(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\Z", RegexOptions.IgnoreCase);
38+
39+
// If given input is a valid email format
40+
if (isEmail)
41+
{
42+
// Create new person for selection
43+
Person newPerson = new Person();
44+
newPerson.EmailID = emailString;
45+
int atIndex = emailString.LastIndexOf('@');
46+
int dotIndex = emailString.IndexOf('.');
47+
string name = emailString.Substring(0, atIndex);
48+
if (name.Contains('.'))
49+
{
50+
newPerson.FirstName = name.Substring(0, dotIndex);
51+
newPerson.LastName = name.Substring(dotIndex + 1);
52+
}
53+
else
54+
{
55+
newPerson.FirstName = name;
56+
}
57+
e.Item = newPerson;
58+
}
59+
60+
// If email is id invalid, show error dialog.
61+
else
62+
{
63+
var dialog = new ContentDialog();
64+
dialog.CloseButtonText = "Close";
65+
dialog.Content = "Invalid email id!";
66+
dialog.Title = "Error";
67+
dialog.XamlRoot = this.Content.XamlRoot;
68+
await dialog.ShowAsync();
69+
}
70+
}
3071
}
3172
}

0 commit comments

Comments
 (0)