MapWindow 4 - ActiveX Control Programming : MapWindow Discussion Forum
Hi all, I'm trying to convert this code: Private Sub Map1_SelectBoxFinal(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_SelectBoxFinalEvent) Handles Map1.SelectBoxFinal Dim sf As MapWinGIS.Shapefile Dim myExtents As New MapWinGIS.Extents()
converting from VB.Net to C# help please?
Posted by: leddy ()
Date: April 15, 2008 08:12AM

Hi all,

I'm trying to convert this code:
Private Sub Map1_SelectBoxFinal(ByVal sender As Object, ByVal e As AxMapWinGIS._DMapEvents_SelectBoxFinalEvent) Handles Map1.SelectBoxFinal 
Dim sf As MapWinGIS.Shapefile 
Dim myExtents As New MapWinGIS.Extents() 
Dim selectedShapes() As Integer 
Dim i As Integer, hndl As Integer 
Dim pxMin As Double, pxMax As Double, pyMin As Double, pyMax As Double, pzMin As Double, pzMax As Double 
Dim col As System.Drawing.Color 
'Check if the map is in selection mode 
If Map1.CursorMode = MapWinGIS.tkCursorMode.cmSelection Then 
'Get the handle of the layer at position 0 
hndl = Map1.get_LayerHandle(0) 
'Get the shapefile in the specified layer 
sf = Map1.get_GetObject(hndl) 
'Convert the boundaries of the selection box from pixel units to projected map coordinates 
Map1.PixelToProj(e.left, e.bottom, pxMin, pyMin) 
Map1.PixelToProj(e.right, e.top, pxMax, pyMax) 
'Set the extents object to be used to find shapes that have been selected in the shapefile 
myExtents.SetBounds(pxMin, pyMin, 0, pxMax, pyMax, 0) 
'Check if there are any shapes with in the shapefile that intersect with the selection box 
If sf.SelectShapes(myExtents, 0, MapWinGIS.SelectMode.INTERSECTION, selectedShapes) Then 
'Get the System.Drawing.Color which is being used as the fill color for the shapes in the layer 
col = Map1.get_ShapeLayerFillColor(hndl) 
'Set all shapes in the shapefile back to their original color 
Map1.set_ShapeLayerFillColor(hndl, System.Convert.ToUInt32(RGB(col.R, col.G, col.B))) 
'For each of the selected shapes in the shapefile, color them differently than their original fill color 
For i = 0 To UBound(selectedShapes) 
Map1.set_ShapeFillColor(hndl, selectedShapes(i), System.Convert.ToUInt32(RGB(100, 100, 0))) 
Next 
End If 
End If 
End Sub

to C# - this is what I have so far but it's giving an error on the last for loop - can anyone see anything obvious I'm missing?
private void mapTest_SelectBoxFinal(object sender, AxMapWinGIS._DMapEvents_SelectBoxFinalEvent e)
        {
            
            MapWinGIS.Shapefile sf;
            MapWinGIS.Extents myExtents = new MapWinGIS.Extents();
            int[] selectedShapes;
            object obj;

            int hndl;
            double pxMin = 0, pxMax = 0, pyMin = 0, pyMax = 0;

            //Check if the map is in selection mode 
            if (mapTest.CursorMode == MapWinGIS.tkCursorMode.cmSelection)
            {
                //Get the handle of the layer at position 0 
                hndl = mapTest.get_LayerHandle(0);
                //Get the shapefile in the specified layer 
                obj = mapTest.get_GetObject(hndl);

                if (obj.GetType() != null)
                {
                    if (System.Convert.ToString(obj.GetType()) == "MapWinGIS.ShapefileClass")
                    {
                        sf = mapTest.get_GetObject(hndl) as MapWinGIS.Shapefile;

                        //Convert the boundaries of the selection box from pixel units to projected map coordinates 
                        mapTest.PixelToProj(e.left, e.bottom, ref pxMin, ref pyMin);
                        mapTest.PixelToProj(e.right, e.top, ref pxMax, ref pyMax);
                        //Set the extents object to be used to find shapes that have been selected in the shapefile 
                        myExtents.SetBounds(pxMin, pyMin, 0, pxMax, pyMax, 0);

                        //Check if there are any shapes with in the shapefile that intersect with the selection box 
                        if (sf.SelectShapes(myExtents, 0, MapWinGIS.SelectMode.INTERSECTION, ref obj))
                        {
                            //Get the System.Drawing.Color which is being used as the fill color for the shapes in the layer 
                            UInt32 col = (UInt32)(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red));

                            col = Convert.ToUInt32(mapTest.get_ShapeLayerFillColor(hndl));
                            //Set all shapes in the shapefile back to their original color 
                            mapTest.set_ShapeLayerFillColor(hndl, col);
                            //For each of the selected shapes in the shapefile, color them differently than their original fill color 
                            for (int i = 0; i < selectedShapes.GetUpperBound(); i++)
                            {
                                mapTest.set_ShapeFillColor(hndl, selectedShapes, col);
                            }
                        }
                    }
                }
            }

        }

Many thanks in advance

leddy

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: Army90 ()
Date: April 15, 2008 12:09PM

I modified your code slightly and the provided code below works...... Basically there were a couple of problems... Evidently VB.Net doesn't care if you instantiate an array before you pass it as a ref. Unfortunately, C# will not let you get away with this. So there are a couple of changes that were required.

1. I introduced and instantiated the temp object.
2. I pass the temp object instead of the selectedObjects variable to the SelectShapes call.
3. Upon successful return, I cast the temp object to the int array that is the selectObjects object.


The other syntactical change was the GetUpperBound method from VB doesn't exist in C#. For a C# array the equivalent is the Length Property.

You'll also notice that I removed the 2 conditionals that check for the Object Type and ensured that it was a ShapeFileClass. I removed these because I ran into a similar problem that was never resolved involving the __ComObject Type being returned. (see [www.mapwindow.org] ) for further reading on this.... If you absolutely need this part to work, I'm not sure what to tell you.... at this point.

Last minor change was with how the colors were being set. The logic that your original code had was never going to change the color. You set the 'col' variable to Red but then you wrote over it with the current layer color and you used that color to "change" the color of the selected shapes (i.e. no change). The sequence of calls as I have them correct that problem. Hope this helps....

HooAh!

MapWinGIS.Shapefile sf;
MapWinGIS.Extents myExtents = new MapWinGIS.Extents();
int[] selectedShapes;
object temp = new object();
object obj;

int hndl;
double pxMin = 0, pxMax = 0, pyMin = 0, pyMax = 0;

//Check if the map is in selection mode 
if (mapMain.CursorMode == MapWinGIS.tkCursorMode.cmSelection)
{
    //Get the handle of the layer at position 0 
    hndl = mapMain.get_LayerHandle(0);
    //Get the shapefile in the specified layer 
    obj = mapMain.get_GetObject(hndl);

    sf = mapMain.get_GetObject(hndl) as MapWinGIS.Shapefile;

    //Convert the boundaries of the selection box from pixel units to projected map coordinates 
    mapMain.PixelToProj(e.left, e.bottom, ref pxMin, ref pyMin);
    mapMain.PixelToProj(e.right, e.top, ref pxMax, ref pyMax);
    //Set the extents object to be used to find shapes that have been selected in the shapefile 
    myExtents.SetBounds(pxMin, pyMin, 0, pxMax, pyMax, 0);

    //Check if there are any shapes with in the shapefile that intersect with the selection box 
    if (sf.SelectShapes(myExtents, 0, MapWinGIS.SelectMode.INTERSECTION, ref temp))
    {
        selectedShapes = (int[])temp;

        //Get the System.Drawing.Color which is being used as the fill color for the shapes in the layer 
        UInt32 col = Convert.ToUInt32(System.Drawing.ColorTranslator.ToOle(mapMain.get_ShapeLayerFillColor(hndl)));
        //Set all shapes in the shapefile back to their original color 
        mapMain.set_ShapeLayerFillColor(hndl, col);
        //For each of the selected shapes in the shapefile, color them differently than their original fill color 
        col = (UInt32)(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red));
        for (int i = 0; i < selectedShapes.Length; i++)
        {
            mapMain.set_ShapeFillColor(hndl, selectedShapes[[color=ff010001]i[/color]], col);
        }
    }
}

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: leddy ()
Date: April 16, 2008 02:52AM

That's spot on - thank you soo much!! I was tearing my hair out with this one.

I'll no doubt be back.... :o)

Thanks again

leddy

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: thabet084 ()
Date: February 23, 2009 04:00AM

a great thanks for U Army90

Mohammed Thabet Zaky
GIS developer
MSN:thabet084@hotmail.com
Blog:[www.thabettech.blogspot.com]
Egypt,Cairo

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: tahantobing ()
Date: February 23, 2009 05:57AM

hi

there are useful homepage:

[www.developerfusion.com]
or
[www.developerfusion.com]

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: bittu ()
Date: January 13, 2011 05:13AM

Hi,
i have used this C# converted code in to my app to get selected the polygon ..but iam getting error in the following line
"[[color=ff010001]i[/color]], col);"
error: syntax error ']' i tried a lot but nothing is working..plz help me to get out off this....

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: carnegiea ()
Date: January 13, 2011 07:51PM

Bittu are you referring to the codes as shown above? Or you have your own codes? Paste it here then.

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: bittu ()
Date: January 17, 2011 03:44AM

hii..
yes i am using ur given codes. i have just modified variables only..
plz see the codes given below:

private void axMap1_SelectBoxFinal(object sender, AxMapWinGIS._DMapEvents_SelectBoxFinalEvent e)
        {
            MapWinGIS.Shapefile sf;
            MapWinGIS.Extents myExtents = new MapWinGIS.Extents();
            int[] selectedShapes;
            object temp = new object();
            object obj;

            //int hndl;
            double pxMin = 0, pxMax = 0, pyMin = 0, pyMax = 0;

            //Check if the map is in selection mode 
            if (axMap1.CursorMode == MapWinGIS.tkCursorMode.cmSelection)
            {
                //Get the handle of the layer at position 0               
                handle2 = axMap1.get_LayerHandle(1);

                //Get the shapefile in the specified layer                
                obj = axMap1.get_GetObject(handle2);
               
                sf = axMap1.get_GetObject(handle2) as MapWinGIS.Shapefile;
                //Convert the boundaries of the selection box from pixel units to projected map coordinates 

                axMap1.PixelToProj(e.left, e.bottom, ref pxMin, ref pyMin);
               
                axMap1.PixelToProj(e.right, e.top, ref pxMax, pyMax);
             
                //Set the extents object to be used to find shapes that have been selected in the shapefile 
                axMap1.SetBounds(pxMin, pyMin, 0, pxMax, pyMax, 0);
                //Check if there are any shapes with in the shapefile that intersect with the selection box 
                if (sf.SelectShapes(myExtents, 0, MapWinGIS.SelectMode.INTERSECTION, ref temp))
                {
                    selectedShapes = (int[])temp;

                    //Get the System.Drawing.Color which is being used as the fill color for the shapes in the layer 
                    UInt32 col = Convert.ToUInt32(System.Drawing.ColorTranslator.ToOle(axMap1.get_ShapeLayerFillColor(handle2)));
                    //Set all shapes in the shapefile back to their original color 
                    axMap1.set_ShapeLayerFillColor(handle2, col);
                    //For each of the selected shapes in the shapefile, color them differently than their original fill color 
                    col = (UInt32)(System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red));
                    for (int i = 0; i < selectedShapes.Length; i++)
                    {
                       axMap1.set_ShapeFillColor(handle2, selectedShapes[[color=ff010001]i[/color]], col);

                    }
                }
            }
   
     }

thanks..



Edited 1 time(s). Last edit at 01/17/2011 04:49AM by pmeems.

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: pmeems ()
Date: January 17, 2011 04:50AM

@bittu:
I'm missing your question. What is not working?

--
Paul

--
Don't forget to read the new documentation: www.mapwindow.org/documentation/mapwingis4.8
Join us Google+: MapWindow GIS Google+ Community
Join the MapWindow Group on LinkedIn! LinkedIn - MapWindow Group

Download the latest beta installer at:
tinyurl.com/mwMonthly 32-Bit
tinyurl.com/mwMonthlyx64 64-Bit
Follow me on Twitter MapWindow_nl to read when a new installer is published.

---
Paul Meems
The Netherlands
[www.bontepaarden.nl]
Release manager, configuration manager and
forum moderator of MapWindow GIS

Owner of MapWindow.nl - Support for
Dutch speaking users: www.mapwindow.nl

*******
Everything I say or write is my personal opinion and
not the opinion of the company I work for.
*******
View my profile on LinkedIn

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: bittu ()
Date: January 25, 2011 03:15AM

sorry for late response...

i m getting error in this line

"axMap1.set_ShapeFillColor(handle2, selectedShapes[[color=ff010001]i[/color]], col);"

Error: 1- Syntax error [[color
Error: 2- expected ; at ]i
Error: 3- syntax error at [/color]
Error: 4- expected; at col);

Thanks

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: carnegiea ()
Date: January 25, 2011 05:15AM

Error: axMap1.set_ShapeFillColor(handle2, selectedShapes[[color=ff010001]i[/color]], col);

Should be error due to the syntax

I expect you meant this?

for (int i = 0; i < selectedShapes.Length; i++)
{
axMap1.set_ShapeFillColor(handle2, selectedShapes, col);
}

Options: ReplyQuote
Re: converting from VB.Net to C# help please?
Posted by: carnegiea ()
Date: January 25, 2011 05:16AM


axMap1.set_ShapeFillColor(handle2, selectedShapes/[i/], col);

Delete the / sign in the line



Edited 3 time(s). Last edit at 01/25/2011 05:18AM by carnegiea.

Options: ReplyQuote


Sorry, only registered users may post in this forum.





Banner Exchange




GISCP.com




Send us your banner logo (160x120) for the space above, and add this MapWindow banner ad to your site:

Just paste this text in your page: