I have an control that inherits from another control (TxTextControl). I have a SelectedText property that basicaly wraps the base SelectedText property, which is apparently needed because my control is implementing an interface with that property. The code is this:
public string SelectedText
{
get
{
return base.Selection.Text; // Error here (#1042)
}
set
{
if (base.Selection == null)
{
base.Selection = new TXTextControl.Selection(0, 0);
}
base.Selection.Text = value;
}
}
When I drop this control on a form, no problems. It compiles and runs. Everything looks great. However, when I save, close then reopen the form, the form designer shows this error:
Object reference not set to an instance of an object.
1. Hide Call Stack
at Test.FormattedTextBox2.get_SelectedText() in C:\Projects\Test\FormattedTextBox2.cs:line 1042
Anyone know what is going on? I'm about to pull out my last hair...
UPDATE:
darkassisin93's answer wasn't exactly correct, but that was because my posted code wasn't exactly accurate. I needed to test if base.Selection was null before attempting to access a property of that object. In any case, that answer got me headed in the right direction. Here is the actual solution:
public string SelectedText
{
get
{
string selected = string.Empty;
if (base.Selection != null)
{
selected = base.Selection.Text;
}
return selected;
}
set
{
if (base.Selection == null)
{
base.Selection = new TXTextControl.Selection(0, 0);
// Have to check here again..this apparently still
// results in a null in some cases.
if (base.Selection == null) return;
}
base.Selection.Text = value;
}
}
Try replacing
return base.SelectedText;
with
return base.SelectedText ?? string.Empty;
It's most likely because the base class's SelectedText property is set to null.
Related
I have a Xamarin form where I am trying to add a SyncFusion AutoComplete control. The data is a simple class with only three string fields (CUSTNMBR, CUSTNAME, ZIP). I want it to match on any of the fields and display the coresponding CUSTNMBR. Here it my line in Xaml:
<xForms:SfAutoComplete x:Name="customerAutoComplete" WidthRequest="120" BackgroundColor="White" />
In the form's code-behind constructor I call LoadCustomerData():
private async void LoadCustomerData()
{
customerAutoComplete.DataSource = await GetCustomerCodes();
customerAutoComplete.DisplayMemberPath = "CUSTNMBR";
customerAutoComplete.SelectedValuePath = "CUSTNMBR";
customerAutoComplete.SuggestionMode = SuggestionMode.Custom;
customerAutoComplete.Filter = FilterCustomers;
customerAutoComplete.AutoCompleteMode = AutoCompleteMode.Suggest;
customerAutoComplete.Watermark = "Zip Code, Customer ID, or Customer Name";
customerAutoComplete.MinimumPrefixCharacters = 3;
}
Here is my filter method.
private bool FilterCustomers(string search, object customer)
{
var text = customerAutoComplete.Text;
if (customer != null)
{
var myCustomer = (OrganizationSearchDto)customer;
if (myCustomer.CustName.Contains(text) || myCustomer.CustNmbr.Contains(text) ||
myCustomer.Zip.Contains(text))
{
return true;
}
}
return false;
}
The above code worked partially when I had customerAutoComplete.SuggestionMode = SuggestionMode.Contains but it did not match on the other two fields. Now it still runs, however nothing is shown in the dropdown list (its blank). Why is my dropdown blank? Any hints, suggestion or a hard shove in the right direction will be appreciated.
For anyone encountering this, tests to try:
Put a breakpoint on return true - is that breakpoint hit for the customer(s) you expect to be shown as suggestions?
Swap return true and return false, so it is true for all the OTHER customers - the opposite of what you want. See if it is still blank. If it is, then it isn't the filter - code elsewhere is interfering with display. Would need to show more code, or make a github containing a minimum repo that shows the problem.
[from OP] The issue was that property names on DisplayMemberPath are case sensitive, as are the filter checks.
The fix for the filter was to ignore case everywhere. E.g.
if (myCustomer.CustName.ToLower().Contains(text.ToLower()) || ...)
We have analyzed the reported query. We have achieved the requirement by using the following code snippet,
public bool ContainingSpaceFilter(string search, object item)
{
if (item != null)
{
var myCustomer = item as Employee;
if (**myCustomer.Name.ToUpper().Contains(search.ToUpper()**) || myCustomer.ID.Contains(search) ||
myCustomer.ZipCode.Contains(search))
{
return true;
}
}
return false;
}
I am assigning the field name of Sitecore image control dynamically from code behind file like below:
.ascx
<sc:Image runat="server" ID="scImgRelatedArticle"></sc:Image>
.ascx.cs
if(currentItem != null)
{
Sitecore.Web.UI.WebControls.Date scDateArticleDate = e.Item.FindControl("scDateArticleDate") as Sitecore.Web.UI.WebControls.Date;
if (scDateArticleDate != null)
{
if (DisplayDates)
{
scDateArticleDate.Field = StartDateFieldName;
scDateArticleDate.Item = currentItem;
}
}
}
Sometimes current Item is null i don't want to assign any field value. I dont want to display the item. But i am ending up with an error message "Field property is required. All field web controls require the field name to be set."
Is there a way in sitecore to do this automatically if i didn't specify the scDateArticleDate.Item property.
You should always set the Field property
scDateArticleDate.Field = StartDateFieldName // where is a string right!
Then you control the visibility of the item depending on if you have or not the item.
Also notice you post a image in your ascx and a date field in the .cs
the complete code would be
scDateArticleDate.Field = StartDateFieldName; //always set the field
if(currentItem != null)
{
Sitecore.Web.UI.WebControls.Date scDateArticleDate = e.Item.FindControl("scDateArticleDate") as Sitecore.Web.UI.WebControls.Date;
if (scDateArticleDate != null)
{
if (DisplayDates)
{
scDateArticleDate.Item = currentItem;
scDateArticleDate.Visible = true;
}
else
{
scDateArticleDate.Visible = false;
}
}
}
cheers
You are not assigning the Sitecore field to the sc:image web control,it should work as:
Sitecore.Data.Fields.Date scDateArticleDate=(Sitecore.Data.Fields.Date)e.Item.FindControl("scDateArticleDate");
I am getting the error "reference not set to an instance of an object" when the following code occurs on startup:
switch (Popup_Data_Type_ComboBox.SelectedItem.ToString())
{
I am pretty sure that this error is occurring as Popup_Data_Type_ComboBox has not yet been created therefore its not possible to get the sting value. How Can I get around this problem?
Ok thanks a lot for all the help I threw in a check if Popup_Data_Type_ComboBox.SelectedItem == null and it now works fine
Add a check before the switch, assuming the code is in a method that just handles the Popup_Data_Type_ComboBox.SelectionChanged-event or the likes:
if (Popup_Data_Type_ComboBox == null
|| Popup_Data_Type_ComboBox.SelectedIndex < 0)
{
// Just return from the method, do nothing more.
return;
}
switch (...)
{
}
The most likely issue is that your combo box hasn't been created, or doesn't have a selected item. In this case, you'd have to explicitly handle that:
if (Popup_Data_Type_ComboBox != null && Popup_Data_Type_ComboBox.SelectedItem != null)
{
switch (Popup_Data_Type_ComboBox.SelectedItem.ToString())
{
//...
}
}
else
{
// Do your initialization with no selected item here...
}
I'd verify first that Popup_Data_Type_ComboBox is instantiated, and then verify that an item is selected. If you are running this on startup as you said, then it is likely no item is selected. you can check with:
if(Popup_Data_Type_ComboBox.SelectedItem != null)
{
switch (Popup_Data_Type_ComboBox.SelectedItem.ToString())
{
//.....
}
}
I am working on C#.net windows application. i am filling combobox on my winform by using follows.
cmbEMPType.DataSource = objEntityManager.EmployeeTypes();
cmbEMPType.DisplayMember = "EMPTypeName";
cmbEMPType.ValueMember = "EMPTypeId";
where objEntityManager.EmployeeTypes(); in the manager method that gets the List from Linq to sql server. this is working fine.
but as i select the item form combo box, and clicked the button then in the button click event i am getting cmbEMPType.SelectedValue as EmpType return type rather than its Id. why should this? I don't want to create one more EmpType object. need simple selected value. also can not keep faith with SelectedIndex. it may varies for item each time.
**Edited**
public List<EMPType> EmployeeTypes()
{
List<EMPType> EMPTypeList = null;
try
{
if (CommonDataObject.dataContext.EMPAllTypes.Any())
{
EMPTypeList = CommonDataObject.dataContext.EMPAllTypes.ToList();
}
return EMPTypeList;
}
catch
{
return EMPTypeList;
}
}
Edited
private void btnSave_Click(object sender, EventArgs e)
{
iEMPTypeId = cmbEMPType.SelectedValue;
}
here I must get inte. but asking of create the EMPType object.
This is the correct and expected behavior, you can't change it.
SelectedValue should return the type of the property, e.g. if EMPTypeId is integer it should return integer - please post more code so that we can try figuring out why you get different return value.
If by any chance you're using SelectedItem then have such code to get the ID:
int selectedID = (cmbEMPType.SelectedItem as EmpType).EMPTypeId;
To handle cases when there's nothing selected:
object oSelectedEmp = cmbEMPType.SelectedItem;
int selectedID = oSelectedEmp == null ? -1 : (oSelectedEmp as EmpType).EMPTypeId;
The problem is the sequence of your code. Please remove the first line code to the last line. You will get an int value (iEMPTypeId) from cmbEMPType.SelectedValue.
cmbEMPType.DisplayMember = "EMPTypeName";
cmbEMPType.ValueMember = "EMPTypeId";
cmbEMPType.DataSource = objEntityManager.EmployeeTypes();
iEMPTypeId = cmbEMPType.SelectedValue
Another option is to override the toString function in your EMPType class. As Edwin de Koning stated "If no ValueMember is specified it gives a ToString() representation."
Something like (I cant test it at the moment):
public override string ToString()
{
return this.ID;
}
You can check out this article: http://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx
When I set SelectedNode to null the tree updates correctly but BeforeSelect and AfterSelect do not fire.
Is there any way to tell when the selection has been changed to null?
My first thought is to extend the control and add an event though I would have thought something like this would already be available.
I think your solution is good. However, I just discovered this control (keep an eye on SO's right column :)):
http://treeviewadv.sourceforge.net/
which supports what you're looking for, maybe has other goodies too...
It seems the only way I could do this was to create a new control and provide a new implementation for SelectedNode, even OnAfterSelect and OnBeforeSelect weren't getting called.
public new TreeNode SelectedNode {
get { return base.SelectedNode; }
set {
// Remember, if `value' is not null this will be called in `base'.
if (value == null) {
TreeViewCancelEventArgs args
= new TreeViewCancelEventArgs(value, false, TreeViewAction.Unknown);
OnBeforeSelect(args);
if (args.Cancel)
return;
}
base.SelectedNode = value;
// Remember, if `value' is not null this will be called in `base'.
if (value == null) {
OnAfterSelect(new TreeViewEventArgs(value, TreeViewAction.Unknown));
}
}
}