RadAutoCompleteBox UserControl :
public partial class CompleteBoxUserControl: UserControl
{
private List<UserProfile> _allSelectedUserProfiles;
public RadAutoCompleteBox _myCompleteBox;
public string toolTipText = "Username
is not valid";
private ICollection<UserProfile> _basUserProfiles;
private List<int> _ids=new List<int>();
bool _isValidated;
public CompleteBoxUserControl()
{
InitializeComponent();
try
{
InitilizeData();
}
catch (Exception)
{
}
}
public void InitilizeData()
{
completeBox.TokenValidating +=
CompleteBox_TokenValidating;
completeBox.TextBlockFormatting +=
CompleteBox_TextBlockFormatting;
completeBox.KeyPress +=
CompleteBox_KeyPress;
((RadAutoCompleteBoxElement) completeBox.TextBoxElement).Items.CollectionChanged +=
delegate(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.Action ==
Telerik.WinControls.Data.NotifyCollectionChangedAction.Add)
{
var tokenizedTextItem =
e.NewItems[0] as RadTokenizedTextItem;
if (tokenizedTextItem != null)
{
//added
_isValidated = true;
_ids.Add(Convert.ToInt32(tokenizedTextItem.Value));
}
}
else if (e.Action ==
Telerik.WinControls.Data.NotifyCollectionChangedAction.Remove)
{
var tokenizedTextItem =
e.NewItems[0] as RadTokenizedTextItem;
if (tokenizedTextItem != null)
{
UserProfile userProfile =
(from p in General.outEntities.UserProfile.Local
where p.UserId == Convert.ToInt32(tokenizedTextItem.Value)
select
p).FirstOrDefault();
if (userProfile != null)
{
_ids.Remove(Convert.ToInt32(tokenizedTextItem.Value));
//removed
}
}
}
//GetUserIds(); // update UserIds object when user
add/remove item
//GetSelectedUserProfiles(); // update UserProfiles object
when user add/remove item
};
_basUserProfiles = new Collection<UserProfile>();
_basUserProfiles = (from p in General.outEntities.UserProfile.Local
select
p).ToList();
completeBox.AutoCompleteDataSource
= _basUserProfiles;
completeBox.AutoCompleteDisplayMember
= "UserName";
completeBox.AutoCompleteValueMember
= "UserId";
}
#region
Events
private void CompleteBox_TextBlockFormatting(object sender, TextBlockFormattingEventArgs
e)
{
var token = e.TextBlock as TokenizedTextBlockElement;
if (token != null)
{
List<int> ids = (from i in _ids where i == Convert.ToInt32(token.Item.Value) select i).ToList();
if (ids.Count>1)
{
token.GradientStyle = GradientStyles.Solid;
token.BackColor = Color.Salmon;
token.BorderColor = Color.CornflowerBlue;
return;
}
else
{
token.GradientStyle = GradientStyles.Solid;
token.BackColor = Color.SkyBlue;
token.BorderColor = Color.CornflowerBlue;
}
}
}
private void CompleteBox_TokenValidating(object sender, TokenValidatingEventArgs
e)
{
if (completeBox.Items.Any(item => item.Text == e.Text))
{
//if
(RadMessageBox.Show("Should I create duplicate item: " + e.Text,
"Action needed",
// MessageBoxButtons.YesNo) ==
DialogResult.No)
{
// e.IsValidToken = false;
_isValidated = false;
}
}
}
private string[] tokens = null;
public string token = "";
string value = "";
private void CompleteBox_KeyPress(object sender, KeyPressEventArgs e)
{
//if
(e.KeyChar < 'z' && e.KeyChar > '0')
//{
// value += e.KeyChar;
//}
//var
result = (from x in _basUserProfiles where x.UserName.Contains(value) select
x).Take(100);
//if
(result.Any())
//{
// completeBox.AutoCompleteDataSource = result;
//}
this.BorderStyle = BorderStyle.None;
newToolTip.Hide();newToolTip.Dispose();
tokens =
completeBox.Text.Split(completeBox.Delimiter);
token = tokens[tokens.Length - 1];
if (token == "")
_isValidated = true;
else
_isValidated = false;
}
#endregion
#region
Methods
public List<UserProfile> GetSelectedUserProfiles()
{
if (!string.IsNullOrEmpty(completeBox.Text) && _isValidated
== false)
{
ShowToolTip();
return null;
}
_allSelectedUserProfiles = new List<UserProfile>();
foreach (RadTokenizedTextItem thirdToken in completeBox.Items)
{
var userid = thirdToken.Value;
var selectedUser =(General.outEntities.UserProfile.Local.Where(p => p.UserId == Convert.ToInt32(userid))).SingleOrDefault();
if (selectedUser != null)
{
_allSelectedUserProfiles.Add(selectedUser);
}
}
return _allSelectedUserProfiles;
}
private RadToolTip newToolTip = new RadToolTip();
public List<int> GetUserIds()
{
if (string.IsNullOrEmpty(completeBox.Text) || _isValidated)
return
completeBox.Items.Select(thirdToken => Convert.ToInt32(thirdToken.Value))
.Select(Convert.ToInt32)
.ToList();
ShowToolTip();
//RadMessageBox.Show("An
item is not valid or repeated");
return null;
}
private void ShowToolTip()
{
newToolTip.Hide();
newToolTip.Dispose();
this.BorderStyle = BorderStyle.FixedSingle;
newToolTip = new RadToolTip();
var point = new Point(MousePosition.X-185, MousePosition.Y-50);
newToolTip.Show(toolTipText,
point);
}
//get
UserIds as Text
public string GetUserIdsStrings()
{
if (!string.IsNullOrEmpty(completeBox.Text) && _isValidated
== false)
{
ShowToolTip();
return null;
}
var stringsArray = GetUserIds().Select(i =>
i.ToString()).ToArray();
var values = string.Join(",", stringsArray);
return values;
}
#endregion
}
--------------------------------------------
How to use this user control:
How to use this user control:
private void button1_Click(object sender, EventArgs e)
{
List<UserProfile> userProfiles =
completeBoxUserControl1.GetSelectedUserProfiles();
List<int> list = completeBoxUserControl1.GetUserIds();
}
ليست هناك تعليقات:
إرسال تعليق