这是在实现Arcgis Engine的书签的添加以及读取时接触到的:点击添加标签后,弹出给标签命名的窗体,输入名称后将该名字添加到另一个标签列表的窗体。在主对话框分别实现存储和读取的接口。
主窗体的实现的代码如下:
Private Sub 添加书签ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 存为书签ToolStripMenuItem.Click Dim pMapBookmarks As IMapBookmarks pMapBookmarks = AxMapControl1.ActiveView.FocusMap Dim pAOIBookmark As IAOIBookmark pAOIBookmark = New AOIBookmark pAOIBookmark.Location = AxMapControl1.ActiveView.Extent Dim Bookmark As New Bookmark 'Dim pBMname As String ' Bookmark.MdiParent = Me Bookmark.ShowDialog() 'My.Forms.Bookmark.Bookmark_Load(sender, e) If pBookmarkName IsNot Nothing Then pAOIBookmark.Name = pBookmarkName pMapBookmarks.AddBookmark(pAOIBookmark) End If End Sub Private Sub 打开书签ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 打开书签ToolStripMenuItem.Click Dim pMapBookmarks As IMapBookmarks pMapBookmarks = CType(AxMapControl1.ActiveView.FocusMap, IMapBookmarks) Dim pEnumSpatialBM As IEnumSpatialBookmark pEnumSpatialBM = pMapBookmarks.Bookmarks BookmarkList.ShowDialog() Dim pOpenBMname As String pOpenBMname = OpenBMname Dim pSpatialBM As ISpatialBookmark pSpatialBM = pEnumSpatialBM.Next While (pSpatialBM IsNot Nothing) If pSpatialBM.Name = pOpenBMname Then pSpatialBM.ZoomTo(AxMapControl1.ActiveView.FocusMap) AxMapControl1.ActiveView.Refresh() Return End If pSpatialBM = pEnumSpatialBM.Next End While End Sub End Class
添加书签窗体的代码如下:
Public BookmarkList As New BookmarkList Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged pBookmarkName = TextBox1.Text End Sub Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'My.Forms.MainForm.BMname = TextBox1.Text pBookmarkName = TextBox1.Text My.Forms.MainForm.BookmarkList.additem(pBookmarkName) Me.Hide() End Sub
书签窗体代码
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim i As Integer i = ComboBox1.Items.Count My.Forms.MainForm.OpenBMname = ComboBox1.Text Me.Hide() End Sub Public Sub additem(ByVal BookmarkName As String) ComboBox1.Items.Add(BookmarkName) End Sub
其中pBookmarkName是在新建的模块中定义的公共变量。
在书签的过程中,主要实现不同窗体间参数的传递。传递方法是:(假如form2 textbox的值向form1的字符串变量m传递)
方法一:
定义模块变量str
form2窗体:
str=textbox.text
form1窗体:
m=str
方法二:
在form1中定义公共变量:
public str as string
Private sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOK.Click
str=textbox.text
End sub
在form2中
直接调用a=My.Forms.Form2.str
还有一个问题是:显示标签对话框时,用Form2.ShowDialog()而不是Form2.Show()。用后者会运行程序时,运行到Form2.show(),并没有完全弹出Form2,程序也没有调到Form2中,而是继续往下。改成ShowDialog是可以解决问题,可是不知道为什么……哪位知道指点一下哦~