每当我使用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/");
}
答案 0 :(得分:0)
您可以在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()
。