I have this function in C# which loads elements in a PropertyGrid.
public void LoadElements(Document selectedDocument)
{
try
{
if (this.PropertyGrid != null)
{
DocumentProperties DP = new DocumentProperties(selectedDocument);
if(DP != null)
{
this.PropertyGrid.SelectedObject = DP;
}
}
}
catch (Exception ex)
{ Logger.LogError(ex); }
}
The problem is that it throws an exception everytime.
HRESULT -2147467261
The object reference is not set to an instance of an object.
in System.Windows.Forms.PropertyGrid.set_SelectedObjects(Object[] value)
in System.Windows.Forms.PropertyGrid.set_SelectedObject(Object value)
in FloatingForms.FloatingPropertiesForm.LoadElements(Document selectedDocument) dans d:\DEV\FloatingForms\FloatingPropertiesForm.cs:ligne 108
I don't understand because I test if PropertyGrid is not null and if my DocumentProperties Object is not null. Evrything is not null but that doesn't work.
Could you help me to find a solution please ?
EDIT
I have a WPF grid in which there are Canvas. Everytime a Canvas is selected Activated event
the following Element_GotFocus
eventHandler is called.
private void Element_GotFocus(object sender, EventArgs e)
{
try
{
if (MyApplication.PropertiesPanel != null)
{
if (sender is Canvas)
{
if ((sender as Canvas).Tag != null)
{
if ((sender as Canvas).Tag is Document)
{
if (MyApplication.PropertiesPanel != null)
{
MyApplication.PropertiesPanel.LoadElements((sender as Canvas).Tag as Document);
}
}
}
}
}
}
catch (Exception ex)
{ Logger.LogError(ex); }
}
EDIT 2
DocumentProperties Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Entities
{
public class DocumentProperties
{
#region Properties
private int m_iDocumentId;
private string m_sDocumentName;
private string m_sDocumentDescription;
#endregion
#region Getters / Setters
public int Id
{
get { return this.m_iDocumentId; }
}
public string Name
{
get { return this.m_sDocumentName; }
}
public string Description
{
get { return this.m_sDocumentDescription; }
}
#endregion
#region Constructors
public DocumentProperties(Document Document)
{
try
{
this.m_iDocumentId = Document.Id;
this.m_sDocumentName = Document.Name;
this.m_sDocumentDescription = Document.Description;
}
catch (Exception ex)
{ Logger.LogError(ex); }
}
#endregion
}
}
PS : Document Class is a complex object in which there is lots of designing properties I don't want to display in the propertygrid. That's why I defined DocumentProperties, to limit user access to properties.