none
web browser with bing search

    Question

  • Hi guys, im kinda new to c#, so dont get mad when i ask somthing stupid, but ive been working on a web browser for a week now and it has got tabs and controls, but i dont know how to add a search like in ie8 & ie7, so could someone please show me how to get a bing or google search for it? Thanks for your time for reading this message, and im looking forward to your answers. I am using visual studio 2008 pro, if that makes a difference, thanks;)
    Sunday, September 27, 2009 4:29 PM

Answers

All replies

  • If you perform a search on IE8 it simply opens a new tab with a address.

    Just put a textbox and a button and on button click event open a new tab with an address(samples are below)

    It's easy for you to open a new tab if you are developing your own webbrowser.

    Here is a sample address for bing and google

    http://www.bing.com/search?q=foo&src=IE-SearchBox&FORM=IE8SRC&adlt=strict --> q is the search criteria
    http://www.google.com.tr/search?hl=tr&source=hp&q=foo&meta=&aq=f&oq= -->q is the search criteria.
    • Proposed as answer by Tamer Oz Monday, September 28, 2009 6:39 PM
    • Marked as answer by rafal kopiec Monday, September 28, 2009 6:41 PM
    Monday, September 28, 2009 8:23 AM
  • Thankx very much, i appreciate your time and bother to look at a rookie's post, and finally i can go about my search! ;)
    Monday, September 28, 2009 6:43 PM
  • could i just have a sample code, please, for the search button? as i dont know what format it is sopposed to be in(how to write it?))
    Thankx!
    Monday, September 28, 2009 6:48 PM
  • Do you have a method that opens new tab at your browser?

    On click event of search button:

    Call that method.

    string

     

    link=String.Format("http://www.bing.com/search?q={0}&src=IE-SearchBox&FORM=IE8SRC&adlt=strict", TextBox1.Text);

    get the link variable and open in your new tab.
    Monday, September 28, 2009 6:55 PM
  • ok, so this is my current situation. sorry for me to bother you, i dont want do be a big pain
    in the butt, but where am i supposed to put in the code?thanks for your understanding...


    #region Using directives
    
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Windows.Forms;
    
    #endregion
    
    namespace WebBrowserExpress
    {
    
        // Web Browswer Express - a new web browser written in Visual C# Express.
    
    
        partial class Form1 : Form
        {
    
            ArrayList tabpages = new ArrayList();
            ArrayList webpages = new ArrayList();
    
            int current_tab_count = 0;          // Keep track of how many tabs there are..
            int image_animation = 0;            // Used when animating the picturebox image.
    
    
            public Form1()
            {
                InitializeComponent();
                tabControl1.TabPages.Clear();                // Get rid of any existing pages in the tab control
                Create_a_new_tab();                          // This method creates a new tab, new webbrowser and opens the default page.
                WebBrowser webpage = GetCurrentWebBrowser(); // Get the current webpage and..
                webpage.GoHome();                            // ...display the default page.
            }
    
           
            // *********** Navigation Button Handling Code ************** //
    
      
            private void button_newtab_Click(object sender, EventArgs e)
            {
                Create_a_new_tab();
    
                // If the URL box is empty (as it is when the program first starts)
                // then load the default page.
    
                // If the URL box isn't empty, open the URL it contains in the new page.
    
                 WebBrowser thiswebpage = GetCurrentWebBrowser();
                 if (comboBoxurl.Text == "")
                     thiswebpage.GoHome();
                 else
                     thiswebpage.Navigate(comboBoxurl.Text);
                
             }
    
            private void button_CloseTab_Click(object sender, EventArgs e)
            {
                // Close the current tab, unless it happens to be the last one
    
                if (current_tab_count < 2) return;
    
                TabPage current_tab = tabControl1.SelectedTab;
                WebBrowser thiswebpage = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];
                thiswebpage.Dispose();
                tabpages.Remove(current_tab);
                current_tab.Dispose();
                tabControl1.TabPages.Remove(current_tab);
                current_tab_count--;
            }
    
            private void button_back_Click(object sender, EventArgs e)
            {
                // Go Back - if the web control can go back, do it.
                WebBrowser thiswebpage = GetCurrentWebBrowser();
                if (thiswebpage.CanGoBack)
                    thiswebpage.GoBack();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                // Go Home - go to the default home page.
                WebBrowser thiswebpage = GetCurrentWebBrowser();
                thiswebpage.GoHome();
                timer1.Enabled = true;  // Start the animation timer
            }
    
            private void button_refresh_Click(object sender, EventArgs e)
            {
                // Refresh - cause the browser to reload and redisplay the current page
                WebBrowser thiswebpage = GetCurrentWebBrowser();
                thiswebpage.Refresh();
                timer1.Enabled = true;  // Start the animation timer
            }
    
            private void button_forward_Click(object sender, EventArgs e)
            {
                // Go Forward - if the web control cna go forward, do it.
                WebBrowser thiswebpage = GetCurrentWebBrowser();
                if (thiswebpage.CanGoForward)
                    thiswebpage.GoForward();
            }
    
            private void button_stop_Click(object sender, EventArgs e)
            {
                WebBrowser thiswebpage = GetCurrentWebBrowser();
                thiswebpage.Stop();
            }
    
    
    
            // ******  The URL combo box and search box ******* //
    
    
            private void button_go_Click(object sender, EventArgs e)
            {
                // The user has clicked on the GO button next to the Address text box, so
                // open the URL address in this page
    
                string url = comboBoxurl.Text;
                if (url == "") return;                              // Return if nowhere to go
                WebBrowser thiswebpage = GetCurrentWebBrowser();    // Get the currently displayed WebBrowser
                thiswebpage.Navigate(url);                          // Get the URL from the control, and send the WebBrowser to fetch and display it
                timer1.Enabled = true;                              // Start the animation
            }
    
    
            private void textBoxSearch_KeyUp(object sender, KeyEventArgs e)
            {
                // If the user presses return in the Search text box, click the go button.
                // This allows the user to enter the address without having to click on the GO
                // button. Notice how we cheat a little - if the user presses return, WE
                // click on the button here programmatically. Sneaky, huh?
    
                if (e.KeyCode == Keys.Enter) button_find.PerformClick();
            }
    
            private void comboBoxurl_KeyUp(object sender, KeyEventArgs e)
            {
                // Check to see if user has pressed return and if they have, perform the 
                // same actions as if the GO button was clicked.
                if (e.KeyCode == Keys.Enter) button_go.PerformClick();
            }
    
            private void comboBoxurl_SelectedIndexChanged(object sender, EventArgs e)
            {
                // The user selected an item from the drop down list, so
                // go to that website by programmatically clicking the GO button.
                button_go.PerformClick();
            }
    
    
            // ********************* Helper methods ******************************* //
    
            private void Create_a_new_tab()
            {
                // Add a new tab control, and put a web control in it.
                // Let's have a maxium of 10 tabs to keep things sane.
    
                if (current_tab_count == 10) return;
    
                // Create the new tab page
                TabPage newpage = new TabPage("Loading...");    // Create the new page
                tabpages.Add(newpage);                          // Add the page to an arraylist to keep track of it.
                tabControl1.TabPages.Add(newpage);              // Add the page to the TabControl so it actually appears
            
                // Keep track of how many pages are open.
                current_tab_count++;
    
                // Create a new WebBrowser control.
                WebBrowser webpage = new WebBrowser();          // Create the new control
                webpages.Add(webpage);                          // Add the control to an arraylist to keep track of it.
                webpage.Parent = newpage;                       // Make sure the WebControl's parent is the TabControl page
                webpage.Dock = DockStyle.Fill;                  // Make the WebControl fill the entire TabControl page.
                
                // We define a handler to perform some actions when the document has been fetched and displayed.
                // We need to do this as the WebControl will work in the background, and we need to know when it has
                // finished so we can switch off the animation timer, and update the name of the Tab with the
                // the document title.
                webpage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webpage_DocumentCompleted);
               // webpage.GoHome();                               // Display the default page.
                timer1.Enabled = true;                          // Start the timer for animating the little logo in the top right.
    
                // Bring the new page to the foreground
                tabControl1.SelectedTab = newpage;
                    
            }
    
    
            private WebBrowser GetCurrentWebBrowser()
            {
                // This method returns the currently display WebControl.
                // We use an arraylist of webpages, and find the right one by getting the index
                // of the current tab.
    
                TabPage current_tab = tabControl1.SelectedTab;
                WebBrowser thiswebpage = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];
                return thiswebpage;
            }
    
           
            private void UpdateName(TabPage tb)
            {
                // Update the name of the current tab by looking
                // at the document it is displayed
    
                TabPage current_tab = tb;
                WebBrowser thiswebpage = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];
    
                if (thiswebpage.Document != null)
                    current_tab.Text = thiswebpage.Document.Title;
                else
                    current_tab.Text = "Zzz";
            }
    
            private void UpdateAllNames()
            {
                // Update all the tab names for all the currently open web pages.
    
                foreach (TabPage tb in tabControl1.TabPages)
                {
                    UpdateName(tb);
                }
            }
    
           
          
    
            void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
            {
                // This method is called when a webpage has completed rendering.
                // It's called because we asked the WebControl send this message.
                // See the CreateNewTab method
    
                UpdateAllNames();               // Update the names of the tabs by looking at the HTML document title.
                UpdateBackForwardButtons();     // Make sure the back and forward buttons are disabled if actions are not possible
                timer1.Enabled = false;         // Stop the animation
            }
    
          
            
    
            private void UpdateBackForwardButtons()
            {
                // Update the state of the buttons (i.e. ghosted or not)
                // dpending on if the webbrowser control can go back, forward etc.
    
                WebBrowser thiswebpage = GetCurrentWebBrowser();
    
                if (thiswebpage.CanGoBack) button_back.Enabled = true;
                else button_back.Enabled = false;
    
                if (thiswebpage.CanGoForward) button_forward.Enabled = true;
                else button_forward.Enabled = false;
    
                if (current_tab_count > 1) button_CloseTab.Enabled = true;
                else button_CloseTab.Enabled = false;
            }
    
            private void tabControl1_Selected(object sender, TabControlEventArgs e)
            {
                // This is called when a new tab page is made active.
                // We don't need to do much - the webbrowser control will automatically be
                // brought to the forground - but we do need to make sure the buttons
                // for Back and Forward reflect this particular page.
    
                UpdateBackForwardButtons();
            }
    
    
    
            private void timer1_Tick(object sender, EventArgs e)
            {
                // This method is called by the timer, and we use it to update the
                // image displayed by the PictureBox control to create a simple
                // animation.
    
                image_animation++;
                if (image_animation > 12) image_animation = 0;
    
                switch (image_animation)
                {
                  
                }
            }
    
            private void textBoxSearch_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void cmdgo_Click(object sender, EventArgs e)
            {
                
            }
        }
    }

    Monday, September 28, 2009 7:08 PM