Select to view content in your preferred language

Use OnCreateFeature Event when adding a new feature

977
4
Jump to solution
09-28-2021 07:10 AM
CarlBrydon
New Contributor II

Hello,

I am putting together a c# Add-In using Visual Studio Community 2019.  I also have a ComboBox int he Add-In containing a list of feature codes being read from a dbase table.

I'd like to be able to do the following...

Whenever a new feature is created in any of the layers in my current MXD, populate a field on the feature just created with the current value of the ComboBox.

I am not sure how to get the name of the ComboBox control and reference it another *.cs file.

I am aware of the the Editing_EditEventListener sample, but there is more to that solution than I really need.  I tried copying the EventListener.cs file from that solution and adding it to my own project (I removed the portions of code referring to events other than OnCreateFeature to make it easier to read). I added the code below to EventListener_OnCreateFeature, but nothing happens when I do that.  Perhaps I need more than just the EventListener.cs file from the Developer sample?

IEditLayers ieLayers = (IEditLayers)m_editor;
IFeatureLayer pFLayer = ieLayers.CurrentLayer;
IFeatureCursor pFCur = pFLayer.Search(null, false);
IFeature pFeat = pFCur.NextFeature();
m_editor.StartOperation();
//  use dummy value until I can get the ComboBox Value
pFeat.Value[pFeat.Fields.FindField("FEAT_CODE")] = "xxxxxx";
pFeat.Store();
m_editor.StopOperation("FeatureCodeUpdate");

 

I don't doubt there are flaws in the code I've added, but I believe the first step is getting the OnCreateFeature editor event to listen and fire whenever a new feature is added.  Hoping someone can get me pointed in the right direction.

Thanks,

Carl

0 Kudos
1 Solution

Accepted Solutions
BrentHoskisson
Occasional Contributor III

You will have to edit FeatureCodeList.cs to expose the value of the combo box publicly:

public string ComboValue

{

get { return ComboBox.SelectedItem; }

}

Now, it whatever class calls this method, you just need to get the value and put it in a place where the EventListener can find it.  I can't give you more specifics without seeing the code that starts up the combo box form.  However, it is all basic programming from here.

 

 

View solution in original post

4 Replies
BrentHoskisson
Occasional Contributor III

Your IFeature interface can cast to IRowEvents where there is an OnNew and OnChanged event that you might be able to set up.  I haven't tried it, but it might work.

 

Are you saying that the combo box is in a dll that you cannot edit and so create a public property in?  It is difficult to understand what the actual issue is.

 

Brent Hoskisson

 

0 Kudos
CarlBrydon
New Contributor II

Thanks for the suggestion Brent.  Below is the code I ended up using to update the feature attribute with just static text.

void EventListener_OnCreateFeature(ESRI.ArcGIS.Geodatabase.IObject obj)
        {
            IFeature pFeat = (IFeature)obj;
            pFeat.Value[pFeat.Fields.FindField("FEAT_CODE")] = "xxxxxx";
            pFeat.Store();
        }

 

In terms of where the ComboBox I didn't really explain things as well as I should have.

Below is a screenshot from Visual Studio...

CarlBrydon_0-1632930443922.png

 

FeatureCodeList.cs is the code generated by the AddInWizard for the ComboBox.  I need to reference the ComboBox (defined within FeatureCodeList.cs) in EventListener_OnCreateFeature which is in EventListener.cs.  So instead of the line...

pFeat.Value[pFeat.Fields.FindField("FEAT_CODE")] = "xxxxxx";

I need to have...

pFeat.Value[pFeat.Fields.FindField("FEAT_CODE")] = ComboxBox.CurrentValue (or whatever the correct syntax is);

 

 

 

0 Kudos
BrentHoskisson
Occasional Contributor III

You will have to edit FeatureCodeList.cs to expose the value of the combo box publicly:

public string ComboValue

{

get { return ComboBox.SelectedItem; }

}

Now, it whatever class calls this method, you just need to get the value and put it in a place where the EventListener can find it.  I can't give you more specifics without seeing the code that starts up the combo box form.  However, it is all basic programming from here.

 

 

CarlBrydon
New Contributor II

Thanks Brent,

I had a bit of help from Éric G as ESRI Canada as well and had to expose the Combox Box as a field in FeatureCodeList class.

private static FeatureCodeList s_combobox;
s_combobox = this;

Then using code similar to what you provided

 internal static string  GetSelectedValue()
        {
            return s_combobox.Value;
        }  

 

Finally within the EventListener.cs code, I defined the OnCreateFeature event as follows...

void EventListener_OnCreateFeature(ESRI.ArcGIS.Geodatabase.IObject obj)
{
IFeature pFeat = (IFeature)obj;
pFeat.Value[pFeat.Fields.FindField("FEAT_CODE")] = FeatureCodeList.GetSelectedValue();
pFeat.Store();
}

 

Thanks for your assistance with this!

Carl

0 Kudos