Hi, Jerry. Please take a moment off your busy schedule and look at this simple code. To Edit Shape Attributes, this code works fine. How can I create the same form with blank values based on existing Point Shapefile Attributes? So that I can just fill in the new values for the new Point on Save?
int layerHandle = axMap1.get_LayerHandle(0); // it’s assumed here that the layer we want to edit is the first 1 (with 0 index)
Shapefile sf = axMap1.get_Shapefile(layerHandle);
if (sf != null)
{
double projX = 0.0;
double projY = 0.0;
axMap1.PixelToProj(e.x, e.y, ref projX, ref projY);
object result = null;
Extents ext = new Extents();
ext.SetBounds(projX, projY, 0.0, projX, projY, 0.0);
// for polygon layers, tolerance can be left at zero
double tolerance = 0;
// but for point layers, we need to buffer the click point
if (sf.ShapefileType2D == ShpfileType.SHP_POINT)
{
tolerance = 100; // meters
Utils utils = new Utils();
utils.ConvertDistance(tkUnitsOfMeasure.umMeters, tkUnitsOfMeasure.umDecimalDegrees, ref tolerance);
}
if (sf.SelectShapes(ext, tolerance, SelectMode.INTERSECTION, ref result))
{
int[] shapes = result as int[];
if (shapes == null) return;
if (shapes.Length > 1)
{
string s = "More than one shapes were selected. Shape indices:";
for (int i = 0; i < shapes.Length; i++)
s += shapes[i] + Environment.NewLine;
MessageBox.Show(s);
}
else
{
sf.SelectNone();
sf.set_ShapeSelected(shapes[0], true); // selecting the shape we are about to edit
axMap1.Redraw(); Application.DoEvents();
Form form = new Form();
for (int i = 0; i < sf.NumFields; i++)
{
System.Windows.Forms.Label label = new System.Windows.Forms.Label();
label.Left = 15;
label.Top = i * 30 + 5;
label.Text = sf.Field[i].Name;
label.Width = 60;
form.Controls.Add(label);
TextBox box = new TextBox();
box.Left = 80;
box.Top = label.Top;
box.Width = 80;
box.Text = sf.CellValue[i, shapes[0]].ToString();
box.Name = sf.Field[i].Name;
form.Controls.Add(box);
}
form.Width = 180;
form.Height = sf.NumFields * 30 + 70;
Button btn = new Button
{
Text = "Ok",
Top = sf.NumFields * 30 + 10,
Left = 20,
Width = 70,
Height = 25
};
btn.Click += BtnClick;
form.Controls.Add(btn);
btn = new Button
{
Text = "Cancel",
Top = sf.NumFields * 30 + 10,
Left = 100,
Width = 70,
Height = 25
};
btn.Click += BtnClick;
form.Controls.Add(btn);
form.FormClosed += FormFormClosed;
form.Text = "Shape: " + shapes[0];
form.ShowInTaskbar = false;
form.StartPosition = FormStartPosition.CenterParent;
form.FormBorderStyle = FormBorderStyle.FixedDialog;
form.MaximizeBox = false;
form.MinimizeBox = false;
form.ShowDialog(axMap1.Parent);
}
}
}
// Execute this code if you want to save the results.
// sf.StopEditingShapes(true, true, null);
}![Attributes|625x499](upload://pqVWOSaj3cdmx5mdj4EM53B8ctN.png)
I tried several codes but nothing worked:
privateint _layerHandle = -1;
privatevoid StartEditing()
{
axMap1.GrabProjectionFromData = true;
string filename = @"d:\data\sf\buildings.shp";
var sf = new Shapefile();
if (!sf.Open(filename))
{
MessageBox.Show("Failed to open: " + filename);
return;
}
_layerHandle = axMap1.AddLayer(sf, true);
sf.InteractiveEditing = true;
axMap1.CursorMode = tkCursorMode.cmAddShape; // cmEditShape, etc
axMap1.ChooseLayer += axMap1_ChooseLayer;
// doing some edits interactively
sf.StopEditingShapes();
}
privatevoid axMap1_ChooseLayer(object sender, AxMapWinGIS._DMapEvents_ChooseLayerEvent e)
{
if (axMap1.CursorMode == tkCursorMode.cmAddShape) // cmMoveShapes, etc
{
e.layerHandle = _layerHandle;
}
}
.
Thank you as always to the rescue