카테고리 없음

[c#] 양식에서 버튼 클릭으로 웹 페이지 열기

행복을전해요 2020. 12. 14. 21:18

먼저 폼에 버튼을 추가하고 Click 이벤트 핸들러에서 다음을 수행하십시오.

private void button1_Click(object sender, EventArgs e)
{           
   //remove this from the constructor else it will be loaded along with the form
       webBrowser1.Navigate("http://www.google.com");
       }
       
-------------------
public partial class Form1 : Form

{

    bool mHooked;
    
        public Form1()
        
            {
                    InitializeComponent();
                            webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
                                    //webBrowser1.Navigate("http://www.google.com"); 
                                        }
                                        
                                            private void button1_Click(object sender, EventArgs e)
                                                {
                                                        webBrowser1.Navigate("http://www.google.com"); 
                                                            }
                                                                void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) 
                                                                    {
                                                                            if (mHooked) return;   
                                                                                    // Get the form 
                                                                                            HtmlDocument doc = webBrowser1.Document; 
                                                                                                    HtmlElement form = doc.Forms["f"];    
                                                                                                            // Get the "I'm feeling lucky" button 
                                                                                                                    HtmlElement lucky = form.All["btnI"];
                                                                                                                            lucky.Click += button1_Click;   
                                                                                                                                    mHooked = true;   
                                                                                                                                        } 
                                                                                                                                        }
                                                                                                                                        
-------------------

버튼을 클릭하면 다음을 추가하십시오.

ProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");  
Process.Start(sInfo);

(또는)

System.Diagnostics.Process.StartProcessStartInfo sInfo = new ProcessStartInfo("http://mysite.com/");
Process.Start(sInfo);

웹 스크래핑 기술을 사용하여 특정 웹 사이트에서 데이터를 추출하거나 winform 자체에서 웹 사이트를 열려면 웹 브라우저 컨트롤을 사용하고 ShaliniPavan 또는 V4Vendetta에서 제공하는 답변을 시도하십시오.



출처
https://stackoverflow.com/questions/7415121