MapInfo Pro Developers User Group

 View Only
  • 1.  Working with selection in C#

    Posted 08-24-2020 03:55
    Hello,

    I was wondering how to get values from selected feature in C#. By using SelectionChanged event I can get basic information on selection: like table name, number of rows, row ID... but what if I want to get lets say ID column value (or multiple IDs) of selected features?


    ------------------------------
    Miroslav Kovacevic
    Knowledge Community Shared Account
    ------------------------------


  • 2.  RE: Working with selection in C#

    Posted 08-25-2020 04:04
    Hi Miroslav,

    Which SelectionChanged event are you talking about? Is this in MI Pro or MapXtreme? In mapXtreme the SelectionChangedEventArgs has a .Features property which gives you a result set you can loop through to get the information about each of the features.either added or removed (as indicated by the SelectionChangedEventArgs.Selected property). Once you have the feature object use the column index to access the data e.g.var currentValue = feature[index] where index is the value assigned when the column was created. If you don't know the index value then look at feature.columns at runtime and it will list them all.

    Liz

    ------------------------------
    Liz Walker
    ------------------------------



  • 3.  RE: Working with selection in C#

    Posted 08-25-2020 07:37
    Hello Liz,

    thank you for your answer. I'm using MapInfo. Now, when I search ExtensibilityReference for SelectionChangedEventArgs I get two results. One is SelectionChangedEventArgs in MapInfo.Types namespace (which I used), and second one is in MapInfo.Engine namespace (the one you mentioned)

    Can you please share piece of code on how to register this event?

    Thank you

    ------------------------------
    Miroslav Kovacevic
    Knowledge Community Shared Account
    ------------------------------



  • 4.  RE: Working with selection in C#

    Posted 08-27-2020 08:59
    If you know the selection that you want to add the handler to then something along the lines of this :-

    private void AddHandlerToSelection() { var mySelection = MapInfo.Engine.Session.Current.Selections.DefaultSelection; mySelection.SelectionChangedEvent += mySelection_SelectionChangedEvent; } private void mySelection_SelectionChangedEvent(object sender, SelectionChangedEventArgs e) { //do something here }​​

    ------------------------------
    Liz Walker
    ------------------------------



  • 5.  RE: Working with selection in C#

    Posted 08-27-2020 09:02
    Hopefully this is more readable:-

    private void AddHandlerToSelection()
    {
    var mySelection = MapInfo.Engine.Session.Current.Selections.DefaultSelection;
    mySelection.SelectionChangedEvent += mySelection_SelectionChangedEvent;
    }

    private void mySelection_SelectionChangedEvent(object sender, SelectionChangedEventArgs e)
    {
    //do something here
    }​

    ------------------------------
    Liz Walker
    ------------------------------



  • 6.  RE: Working with selection in C#

    Posted 09-01-2020 05:05
    Liz,

    thank you for your answer and sorry for late reply (couldn't access this tread url for some reason). This is very helpful.

    ------------------------------
    Miroslav Kovacevic
    Knowledge Community Shared Account
    ------------------------------



  • 7.  RE: Working with selection in C#

    Employee
    Posted 08-31-2020 10:35
    Hi Miroslav,

    For examples on using events, see the ProSpy sample which come with mapbasic -> SAMPLES\RIBBONINTERFACE\DotNet\ProSpy

    For using the Selection in C# check out the MapInfo.Types.Data.ICatalog interface in the ExtensibilityReference.
    There is SelectionCount and SelectionBaseTable and ConvertSelectionToTable.

    Another useful sample is SAMPLES\RIBBONINTERFACE\DotNet\TableCatalogObjectModel - it has many samples and even drops you into the debugger to step through them.

    Once you have the selection table you can use mapbasic to read the table rows and columns.
    Another option is to use the MapInfo Data Access Library, which is a .NET Api for accessing MapInfo Tables. It is based on MapXtreme and easy to use from C#, you just need to understand that it opens tables separate from Pro.
    There is an overview section for it in the Extensibility Reference.
    The best way to learn this is from the sample SAMPLES\RIBBONINTERFACE\DotNet\MapInfoDataAccessLibraryExamples\Examples.cs

    -Bob

    ------------------------------
    Bob Fortin
    Software Architect and Distinguished Engineer
    MapInfo Pro Development Team
    ------------------------------



  • 8.  RE: Working with selection in C#

    Posted 09-01-2020 05:10
    Bob,

    thank you for your answer. I have looked this examples, and I'm using them a lot

    ------------------------------
    Miroslav Kovacevic
    Knowledge Community Shared Account
    ------------------------------