C#TabController ||克隆网络浏览器并获取它的名称
我目前正在从事一个项目,但我无法再获得任何我的选项卡和一个创建另一个选项卡的按钮,看起来是这样的,
public void newtab()
{
tabpage ++;
string namesys = tabpage + " - Script";
Random rnd = new Random();
this.web = new System.Windows.Forms.WebBrowser();
this.bt = new System.Windows.Forms.Button();
this.tp = new System.Windows.Forms.TabPage();
this.lb = new System.Windows.Forms.Label();
tp.Text = namesys;
bt.Location = new Point(3, 374);
bt.Text = "Execute";
this.bt.Click += new System.EventHandler(this.tabexecute);
for (int j = 0; j < 4; j++)
{
Console.WriteLine(rnd.Next());
}
int randit = rnd.Next();
lb.Location = new Point(414 ,377);
lb.Text = randit.ToString();
lb.ForeColor = Color.Black;
lb.Name = "Code";
web.Name = lb.Text;
web.Url = new Uri(string.Format("file:///{0}/Ace/AceEditor.html", Directory.GetCurrentDirectory()));
web.Location = new Point(3, 8);
web.Size = new Size(919 ,360) ;
tp.Controls.Add(web);
tp.Controls.Add(lb);
tp.Controls.Add(bt);
tabControl1.Controls.Add(tp);
}
但是现在我在您处于“创建”选项卡上的标签上时有问题代码,私有void tabexecute无法正常工作(请参阅代码)
private void tabexecute(object sender, EventArgs e)
{
HtmlDocument document = webBrowser1.Document;
string scriptName = "GetText";
object[] args = new string[0];
object obj = document.InvokeScript(scriptName, args);
string script = obj.ToString();
API.SendLuaCScript(script);
API.SendLuaCScript(txt_inject.Text);
}
,因为我不知道该怎么做htmldocument document = webbrowser1.document;
可以重建,以便它始终使用Web浏览器当前所在的位置在标签中。每次创建一个选项卡时,都会随机生成Web浏览器名称。
这是一个可能会阐明问题的屏幕截图:
I'm currently working on a project but I can't get any further I have a tab and a button that creates another tab, the code for it looks like this
public void newtab()
{
tabpage ++;
string namesys = tabpage + " - Script";
Random rnd = new Random();
this.web = new System.Windows.Forms.WebBrowser();
this.bt = new System.Windows.Forms.Button();
this.tp = new System.Windows.Forms.TabPage();
this.lb = new System.Windows.Forms.Label();
tp.Text = namesys;
bt.Location = new Point(3, 374);
bt.Text = "Execute";
this.bt.Click += new System.EventHandler(this.tabexecute);
for (int j = 0; j < 4; j++)
{
Console.WriteLine(rnd.Next());
}
int randit = rnd.Next();
lb.Location = new Point(414 ,377);
lb.Text = randit.ToString();
lb.ForeColor = Color.Black;
lb.Name = "Code";
web.Name = lb.Text;
web.Url = new Uri(string.Format("file:///{0}/Ace/AceEditor.html", Directory.GetCurrentDirectory()));
web.Location = new Point(3, 8);
web.Size = new Size(919 ,360) ;
tp.Controls.Add(web);
tp.Controls.Add(lb);
tp.Controls.Add(bt);
tabControl1.Controls.Add(tp);
}
but now I have the problem when you are in the tab created above the code, private void tabexecute was not working (see code)
private void tabexecute(object sender, EventArgs e)
{
HtmlDocument document = webBrowser1.Document;
string scriptName = "GetText";
object[] args = new string[0];
object obj = document.InvokeScript(scriptName, args);
string script = obj.ToString();
API.SendLuaCScript(script);
API.SendLuaCScript(txt_inject.Text);
}
Because I don't know how to do HtmlDocument document = webBrowser1.Document;
can be rebuilt so that it always uses the web browser where it is currently in the tab. The web browser name is randomly generated each time a tab is created.
Here is a screenshot that might clarify the issue:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
鉴于您似乎需要为在TabPage上创建的每个WebBrowser管理状态,您的最佳选项是创建一个新类,该类别为WebBrowser类。该子类有单一的责任在您的tabexecute方法中执行这些呼叫。该类可能看起来像这样:
现在我们需要实例化,接线点击事件并提供所需的引用到其他对象实例,例如您称为txt_inject的文本框:
应该这样做。
Given that you seem to need to manage state for each WebBrowser you create on a tabpage your best option is to create a new class that subclasses the WebBrowser class. That subclass has the single responsibility to execute those calls in your tabExecute method. That class might look like this:
Now we need to instantiate, wire the Click event and provide needed references to other object instances, for example the textbox you called txt_inject:
And that should do it.
可能是最简单的
,使用这样的内联事件处理程序可能是最简单的:
可能是最好的
,如果您创建
>,它可能是最好的UserControl
,托管webbrowser
,按钮和其他控件,并封装相关逻辑:您甚至可以使用Designer来安排控件,只需在代码中创建USERCONTROL的新实例并摆脱所有这些控制实例和与布局相关的代码。
您可以揭示在UserControl之外可能需要的属性和事件;对于属性,它可能是整个WebBrowser控件或文档等。对于事件,它可能是
executeButtonClick
事件,当您单击按钮时,它将升高。然后,在表单中,单击
addNewtab
按钮,您只需要实例化USerControl的新实例,然后将所需的属性传递给它。另外,您可以处理executeButtonClick
,并尽一切努力。最终仅出于学习目的
,最后只是为了学习目的,要学习ow,您可以使用事件处理程序的发件人参数,看看如何找到控件,您可以将tabexecute Event Handler更改为此:
改进点/了解更多
Probably the easiest
It would be probably the easiest, to use an inline event handler like this:
Probably the best
And it would probably be the best if you create a
UserControl
, hosting theWebBrowser
, the Button, and other controls, and encapsulating related logic:You can even use designer for arranging the controls later just create a new instance of the UserControl in the code and get rid of all those control instantiation and those layout related code.
You can expose the properties and events that you may need outside of the UserControl; For properties, it could be the whole WebBrowser control or the Document, and etc. For events, it could be an
ExecuteButtonClick
event, which will be raised when you click on the button.Then in the form, by clicking on the
AddNewTab
button, you just need to instantiate a new instance of your UserControl and pass the required properties to it. Also you can handleExecuteButtonClick
, and do whatever you need.And finally just for learning purpose
And finally just for learning purpose, to learn ow you can use the sender parameter of an event handler and see how you can find controls, you can change your tabexecute event handler to this:
Points of improvement/learn more