C#TabController ||克隆网络浏览器并获取它的名称

发布于 2025-01-27 07:40:20 字数 1825 浏览 1 评论 0原文

我目前正在从事一个项目,但我无法再获得任何我的选项卡和一个创建另一个选项卡的按钮,看起来是这样的,

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:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

遗失的美好 2025-02-03 07:40:20

鉴于您似乎需要为在TabPage上创建的每个WebBrowser管理状态,您的最佳选项是创建一个新类,该类别为WebBrowser类。该子类有单一的责任在您的tabexecute方法中执行这些呼叫。该类可能看起来像这样:

public class TabWebBrowser:WebBrowser
{

    private TextBox txt_inject;
    
    // needed state
    public void SetInject(TextBox textBox) 
    {
        txt_inject = textBox;
    }

    // Click eventhandler
    public void Execute(object sender, EventArgs e) {
    
        System.Windows.Forms.HtmlDocument document = this.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);
    }
}

现在我们需要实例化,接线点击事件并提供所需的引用到其他对象实例,例如您称为txt_inject的文本框:

... code omitted

string namesys = tabpage + " - Script";
Random rnd = new Random();

var twb = new TabWebBrowser(); // Create a new instance of our own subclass

this.web = twb;  // Set our instance as the WebBrowser to use
this.bt = new System.Windows.Forms.Button();

... code omitted

// wire up the Click event
this.bt.Click += twb.Execute;

// the Execute method needs a txt_inject reference, no idea where that came from
// but .. let's set that as well here

twb.SetInject(txt_inject);

... rest of your code omitted

应该这样做。

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:

public class TabWebBrowser:WebBrowser
{

    private TextBox txt_inject;
    
    // needed state
    public void SetInject(TextBox textBox) 
    {
        txt_inject = textBox;
    }

    // Click eventhandler
    public void Execute(object sender, EventArgs e) {
    
        System.Windows.Forms.HtmlDocument document = this.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);
    }
}

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:

... code omitted

string namesys = tabpage + " - Script";
Random rnd = new Random();

var twb = new TabWebBrowser(); // Create a new instance of our own subclass

this.web = twb;  // Set our instance as the WebBrowser to use
this.bt = new System.Windows.Forms.Button();

... code omitted

// wire up the Click event
this.bt.Click += twb.Execute;

// the Execute method needs a txt_inject reference, no idea where that came from
// but .. let's set that as well here

twb.SetInject(txt_inject);

... rest of your code omitted

And that should do it.

ま柒月 2025-02-03 07:40:20

可能是最简单的

,使用这样的内联事件处理程序可能是最简单的:

public void AddNewTab()
{
    ...
    //Pay attention to var to declare local variables
    var webBrowser = new WebBrowser();
    var button = new Button();
    ...
    button.Click += (sender, e) => {
        var document = webBrowser.Document;
        ...
    };
}

可能是最好的

,如果您创建>,它可能是最好的UserControl,托管webbrowser,按钮和其他控件,并封装相关逻辑:

public void AddNewTab()
{
    ...
    var userControl = new MyUserControl();
    ...
    
}
private void ExecuteButtonClick(object sender, EventArgs e)
{
    var userControl = (MyUserControl) sender;
    var webBrowser = userControl.WebBrowser;
    ... 
}

您甚至可以使用Designer来安排控件,只需在代码中创建USERCONTROL的新实例并摆脱所有这些控制实例和与布局相关的代码。

您可以揭示在UserControl之外可能需要的属性和事件;对于属性,它可能是整个WebBrowser控件或文档等。对于事件,它可能是executeButtonClick事件,当您单击按钮时,它将升高。

然后,在表单中,单击addNewtab按钮,您只需要实例化USerControl的新实例,然后将所需的属性传递给它。另外,您可以处理executeButtonClick,并尽一切努力。

最终仅出于学习目的

,最后只是为了学习目的,要学习ow,您可以使用事件处理程序的发件人参数,看看如何找到控件,您可以将tabexecute Event Handler更改为此:

private void tabexecute(object sender, EventArgs e)
{
    //The Button in the TabPage:
    var button = (Button) sender; 
                 
    //The TabPage: 
    var tabPage = button.Parent;   
                
    //The WebBrowser in the TabPage:
    var webBrowser = tabPage.Controls.OfType<WebBrowser>().First(); 
   
    var document = webBrowser.Document;
    ...
}

改进点/了解更多

Probably the easiest

It would be probably the easiest, to use an inline event handler like this:

public void AddNewTab()
{
    ...
    //Pay attention to var to declare local variables
    var webBrowser = new WebBrowser();
    var button = new Button();
    ...
    button.Click += (sender, e) => {
        var document = webBrowser.Document;
        ...
    };
}

Probably the best

And it would probably be the best if you create a UserControl, hosting the WebBrowser, the Button, and other controls, and encapsulating related logic:

public void AddNewTab()
{
    ...
    var userControl = new MyUserControl();
    ...
    
}
private void ExecuteButtonClick(object sender, EventArgs e)
{
    var userControl = (MyUserControl) sender;
    var webBrowser = userControl.WebBrowser;
    ... 
}

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 handle ExecuteButtonClick, 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:

private void tabexecute(object sender, EventArgs e)
{
    //The Button in the TabPage:
    var button = (Button) sender; 
                 
    //The TabPage: 
    var tabPage = button.Parent;   
                
    //The WebBrowser in the TabPage:
    var webBrowser = tabPage.Controls.OfType<WebBrowser>().First(); 
   
    var document = webBrowser.Document;
    ...
}

Points of improvement/learn more

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文