每当我使用initialize组件时,我就会丢失数据,例如组合框上选择的索引。我尝试在表单加载中声明浏览器,然后在按钮下使用加载url函数,但这不起作用。下面是代码:
int tracker;
string LocationTracker;
tracker = (cbEventsList.SelectedIndex);
lblLocation.Text = cData[tracker];
LocationTracker = cData[tracker];
//In this button the program takes the information previosuly entered into the Enter Event Tab and loads it on the lab
//InitializeComponent();
// BrowserView browserView = new WinFormsBrowserView();
// Controls.Add((Control)browserView);
browserView.Browser.LoadURL("https://www.google.com/maps/place/" + cData[tracker]);
}
private void btnLoadNew_Click(object sender, EventArgs e)
{
InitializeComponent();
BrowserView browserView = new WinFormsBrowserView();
Controls.Add((Control)browserView);
browserView.Browser.LoadURL("https://www.google.com/maps/place/");
}
private void MapView_Load(object sender, EventArgs e)
{
// btnLoadNew.Visible = false;
InitializeComponent();
BrowserView browserView = new WinFormsBrowserView();
Controls.Add((Control)browserView);
browserView.Browser.LoadURL("https://www.google.com/maps/place/");
}
你可以在Form构造函数中初始化BrowserView,然后将其存储为私有字段,并在必要时使用该字段加载其他url:
using DotNetBrowser;
using DotNetBrowser.WinForms;
using System;
using System.Windows.Forms;
namespace WinFormsSampleCS
{
public partial class Form1 : Form
{
private BrowserView browserView;
public Form1()
{
InitializeComponent();
browserView = new WinFormsBrowserView();
Controls.Add((Control)browserView);
//Load initial URL here, if necessary
browserView.Browser.LoadURL("https://www.google.com/maps/place/");
}
private void btnLoadNew_Click(object sender, EventArgs e)
{
//load another URL on button click
browserView.Browser.LoadURL("https://www.google.com/maps/place/");
}
}
}
正如您所看到的,根本没有必要多次调用InitializeComponent()
。