CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 2008
    Posts
    33

    How to send data from class to form textbox

    The following code is realted to CefSharp but my problem is with C# basics. As you can see into the code, i have the class "MyRequestHandler : IRequestHandler" wich contains "OnBeforeResourceLoad" where i can get resources URLS with "string url = request.Url;". My problem is how to send that "string url = request.Url;" to main form and show the url into a textbox.

    Edit: seems that OnBeforeResourceLoad runs into a separate thread.

    Code:
    public partial class Form1 : Form
        {
            private readonly ChromiumWebBrowser browser;
    
            public Form1()
            {
                InitializeComponent();
    
                browser = new ChromiumWebBrowser("http://www.google.es")
                {
                    Dock = DockStyle.Fill,
                    RequestHandler = new MyRequestHandler()
                };
                toolStripContainer1.ContentPanel.Controls.Add(browser);
    
        }
    
            public class MyRequestHandler : IRequestHandler
            {
                public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
                {
                    // You can also check the URL here
                    string url = request.Url;
                    callback.Dispose();
                    return CefReturnValue.Continue;
                }
            }
    Last edited by Zeokat; March 1st, 2017 at 09:24 PM.

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to send data from class to form textbox


  3. #3
    Join Date
    Oct 2008
    Posts
    33

    Re: How to send data from class to form textbox

    Ok. When you are saying solution #5 maybe you are talking about solution with 5 points? the one that includes the code of this class:
    Code:
    public static class ControlExtensions
    {
        public static void Invoke(this Control control, Action action)
        {
            if (control.InvokeRequired) control.Invoke(new MethodInvoker(action), null);
            else action.Invoke();
        }
     }
    And then when i need to update GUI controls i can use something like:
    Code:
    richTextBox.Invoke(() => { richTextBox.AppendText(text + Environment.NewLine); });
    Anyway i'm kinda lost, i don't know how to implemet that code into my scenario and call my TextBox with Invoke from OnBeforeResourceLoad. If you have any other reference will be very apreciated.

    Thanks in advance for your help and time

  4. #4
    Join Date
    Oct 2008
    Posts
    33

    Re: How to send data from class to form textbox

    Ok, finally i could achieved one solution but maybe it isn't the right way to access my textbox, not sure.

    Code:
    namespace CEF_Test
    {
        public static class ControlExtensions
        {
            public static void Invoke(this Control control, Action action)
            {
                if (control.InvokeRequired) control.Invoke(new MethodInvoker(action), null);
                else action.Invoke();
            }
        }
        public partial class Form1 : Form
        {
            private readonly ChromiumWebBrowser browser;
    
            public Form1()
            {
                InitializeComponent();
    
                browser = new ChromiumWebBrowser("http://www.google.es")
                {
                    Dock = DockStyle.Fill,
                    RequestHandler = new MyRequestHandler()
                };
                toolStripContainer1.ContentPanel.Controls.Add(browser);
            }
    
            public class MyRequestHandler : IRequestHandler
            {
    
                public CefReturnValue OnBeforeResourceLoad(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IRequestCallback callback)
                {
                    // You can also check the URL here
                    string url = request.Url;
                    TextBox t = Application.OpenForms["Form1"].Controls["textBox1"] as TextBox;
                    t.Invoke(() => { t.AppendText(url + Environment.NewLine); });
                    callback.Dispose();
                    return CefReturnValue.Continue;
                }
    
            }
          }
    }

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: How to send data from class to form textbox

    You need to find code that shows you how to receive data using the ChromiumWebBrowser. The code you've posted doesn't seem to do that. Once you get that working, then you can solve the problem of displaying the data in a text box.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured