2 Replies - 3139 Views - Last Post: 30 June 2011 - 04:33 PM Rate Topic: -----

#1 simarrandhava   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-June 11

url loading in tabbed web browser

Posted 30 June 2011 - 11:25 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace web_browser
{
    public partial class voicebrowser : Form
    {
        int i = 1;
       
       
        
        public voicebrowser()
        {
            InitializeComponent();
        }


        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoBack();
        }
       

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoForward();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Refresh();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Stop();
        }

        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
        }
       
 
        private void url_Click(object sender, EventArgs e)
        {
            
            url.SelectAll();
        
        }


        private void toolStripButton6_Click(object sender, EventArgs e)
        {
            if (url.Text == string.Empty)
            {
                return;
            }
            else
            {

                ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(url.Text);

            }
        }


       



        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            progressbar.Step = 10;                                              
            progressbar.PerformStep();                                         
            progressbar.Visible = false;                                      
            openinglabel.Visible = false;                                       
            donelabel.Visible = true;
       
            if (((WebBrowser)tabControl1.SelectedTab.Controls[0]).CanGoBack)
            {
               toolStripButton1.Enabled=true;
            }
            else
            {
                toolStripButton1.Enabled = false;
            }

            if (((WebBrowser)tabControl1.SelectedTab.Controls[0]).CanGoForward)
            {
                toolStripButton2.Enabled = true;
            }
            else
            {
                toolStripButton2.Enabled = false;
            }
           

            
        }

       
        private void newTabToolStripMenuItem_Click(object sender, EventArgs e)
        {

            WebBrowser webBrowser1 = new WebBrowser();
            tabControl1.TabPages.Add("Page     " + i + "          ");
            tabControl1.SelectTab(i - 1);
            webBrowser1.Name = "webBrowser1";
            webBrowser1.Dock = DockStyle.Fill;
            tabControl1.SelectedTab.Controls.Add(webBrowser1);
            i = i + 1;
          
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
        }

       

        private void savePageAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).ShowSaveAsDialog();
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).ShowPrintPreviewDialog();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).ShowPrintDialog();
        }

        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).ShowPageSetupDialog();
        }

        private void toolStripMenuItem1_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).ShowPropertiesDialog();
        }

        private void aboutBrowserToolStripMenuItem_Click(object sender, EventArgs e)
        {
            about ab = new about();
            ab.Show();

        }

        private void url_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                if (url.Text == string.Empty)
                {
                    return;

                }
                else
                {
                    ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Navigate(url.Text);
                }
            }

        }

        private void webBrowser1_Navigating_1(object sender, WebBrowserNavigatingEventArgs e)
        {
            donelabel.Visible = false;     
            progressbar.Visible = true;    
            progressbar.Value = 0;          
            progressbar.Minimum = 0;        
            progressbar.Maximum = 100;     
            progressbar.Step = 10;          
            progressbar.PerformStep();      
        }

        private void webBrowser1_Navigated_1(object sender, WebBrowserNavigatedEventArgs e)
        {
            progressbar.Step = 80;                                  
            progressbar.PerformStep();
            url.Text = ((WebBrowser)tabControl1.SelectedTab.Controls[0]).Url.ToString();            
            openinglabel.Text = "Openning: " + url.Text;    
            openinglabel.Visible = true;                           
       

        }

       

        private void cutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).document.ExecCommand("Cut", false, null);
        }

        private void copyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).document.ExecCommand("Copy", false, null);
        }

        private void pasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).document.ExecCommand("Paste", false, null);
        }

        private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).document.ExecCommand("SelectAll", false, null);
        }

        private void tabControl1_MouseClick(object sender, MouseEventArgs e)
        {
            if (tabControl1.TabPages.Count > 1) 
            {
                for (int j = 0; j < this.tabControl1.TabPages.Count; j++)
            {
                Rectangle r = tabControl1.GetTabRect(j);
                Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 9, 7);
                if (closeButton.Contains(e.Location))
                {

                        this.tabControl1.TabPages.RemoveAt(j);
                        tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
                        i = i - 1;
                        break;
                   
                }

                }

            }
        }

       

        private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - 15, e.Bounds.Top + 4);
            e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + 15, e.Bounds.Top + 4);
            e.DrawFocusRectangle();

        }

       
        private void voicebrowser_Load(object sender, EventArgs e)
        {

            
                 
            WebBrowser webBrowser1 = new WebBrowser();
            tabControl1.TabPages.Add("Page     " + i + "          ");
            tabControl1.SelectTab(i - 1);
            webBrowser1.Name = "webBrowser1";
            webBrowser1.Dock = DockStyle.Fill;
            tabControl1.SelectedTab.Controls.Add(webBrowser1);
            i = i + 1;
            
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();


            
           
        
        }

      

        private void removetabToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (tabControl1.TabPages.Count > 1)
            {
                tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
                tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
                i = i - 1;
            }
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            if (tabControl1.TabPages.Count > 1)
            {
                tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
                tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
                i = i - 1;
            }
        }

        private void toolStripButton8_Click(object sender, EventArgs e)
        {
            
            WebBrowser webBrowser1 = new WebBrowser();
            tabControl1.TabPages.Add("Page     " + i + "          ");
            tabControl1.SelectTab(i - 1);
            webBrowser1.Name = "webBrowser1";
            webBrowser1.Dock = DockStyle.Fill;
            tabControl1.SelectedTab.Controls.Add(webBrowser1);
            i = i + 1;
            ((WebBrowser)tabControl1.SelectedTab.Controls[0]).GoHome();
            
        }

        private void toolStripMenuItem2_Click(object sender, EventArgs e)
        {
            (new voicebrowser()).Show();
        }

        private void historyToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void restartToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Hide();
           (new voicebrowser()).Show();
        }

        private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            open op = new open();
            op.Show();

        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            calender cl = new calender();
            cl.Show();
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            Process.Start("calc.exe");
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void setHomeToolStripMenuItem_Click(object sender, EventArgs e)
        {
           
        }

        private void fullScreenToolStripMenuItem_Click(object sender, EventArgs e)
        {

            if (!(this.FormBorderStyle == FormBorderStyle.None && this.WindowState == FormWindowState.Maximized))
            {
                this.FormBorderStyle = FormBorderStyle.None;
                this.WindowState = FormWindowState.Maximized;
            }
            else
            {
                this.WindowState = FormWindowState.Normal;
                this.FormBorderStyle = FormBorderStyle.Sizable;
            }
        }

        private void pageSourceToolStripMenuItem_Click(object sender, EventArgs e)
        {
            String source = ("source.txt");
            StreamWriter writer = File.CreateText(source);
            writer.Write(((WebBrowser)tabControl1.SelectedTab.Controls[0]).DocumentText);
            writer.Close();
            Process.Start("notepad.exe", source);     
        }

        private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void voicebrowser_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (tabControl1.TabPages.Count > 1)
            {
                DialogResult dlg_res = (new closeall()).ShowDialog();

                if (dlg_res == DialogResult.No)
                {
                    e.Cancel = true;
                    tabControl1.TabPages.RemoveAt(tabControl1.SelectedIndex);
                    tabControl1.SelectTab(tabControl1.TabPages.Count - 1);
                    i = i - 1;

                }
                else
                    if (dlg_res == DialogResult.Cancel)
                    {
                        e.Cancel = true;
                    }
                    else
                    {
                        Application.ExitThread();
                    }
            }
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void voicebrowser_FormClosed(object sender, FormClosedEventArgs e)
        {
            File.Delete("source.txt");
        }

        private void statusStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            
        }


       
       

       
    }
}



i have three problrms
1)The url is not being loaded in the textbox named as url in the toolstrip.
2)When I maximize the form,the web browser or tab control window does not get maximized.I tried to change the dock property and set it to fill,but doing so,a new problem occurs:The menustrip gets hidden or we can say that tabcontrol coveror hide the menustrip.
3) progress bar is not working.I have done coding for it in navigating,navigated and document completed event,but still it does not work.

attaching the whole project with this question.

Is This A Good Question/Topic? 0
  • +

Replies To: url loading in tabbed web browser

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 15427
  • View blog
  • Posts: 61,808
  • Joined: 12-June 08

Re: url loading in tabbed web browser

Posted 30 June 2011 - 11:34 AM

I can tell you that for #2 you need to play with the order. Once you 'fill it' right click on the control and either "push back" or "bring to front". Fiddle with it and your other controls should fall in line.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6536
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: url loading in tabbed web browser

Posted 30 June 2011 - 04:33 PM

Let's start with you have three problems but NO QUESTIONS.
'It doesn't work' is not a question. "
So please check out the link and come back with some questions. Otherwise it looks like you're asking "Please fix my copy/pasted code for me."

Are are good articles on how to debug your code:
Debugging tutorial
Debugging tips
Debugging in detail
Great debugging tips
It still doesn't work, article

Let me also throw in a couple tips:
  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.
    btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)
  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like Index, TimeOut, Row, Column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
  • Try to avoid having work actually take place in GUI control event handlers. It is usually better to have the GUI handler call other methods so those methods can be reused and make the code more readible.
    btnSave(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveMenuItem(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    SaveContextMenu(object sender, eventargs e)
    {
        SavePreferences();
    }
    
    Form1_Closing(object sender, eventargs e)
    {
        if (IsDirty) SavePreferences();
    }
    


Standard resources, references and suggestions for new programmers.


I would recommend you start with "Hello World" just like the other million+ coders out there. Then work your way up to the more advanced tasks like this.

The problem with taking on large, complex tasks like this when you are new to coding is that
  • it will frustrate you to the point of quitting,
  • you don't know enough about coding to know where to start or in what direction to design your program
  • You risk learning via the 'Swiss cheese' method where you only learn certain bits and pieces for the one project but have huge holes in your education.


I am going to guess that you are trying to teach yourself C# without much guidance, a decent book or without knowing where to look. Sometimes just knowing where to look can make all the difference. Google is your friend.
Search with either "C#" or "MSDN" as the first word: "MSDN Picturebox", "C# Custom Events", "MSDN timer" etc.

But honestly, just typing away and seeing what pops up in Intellisense is going to make your self-education take 20 years. You can learn by trying to reverse engineer the language through banging on the keyboard experimentation - or you can learn by doing the tutorials and following a good "How to learn C#" book.

Free editions of Visual Studio 2010

May I suggest picking up a basic C# introductory book? There are so many great "How do I build my first application" tutorials on the web... There are dozens of "Learn C# in 21 days", "My first C# program" type books at your local book seller or even public library.

D.I.C. C# Resource page Start here
Intro to C# online tutorial then here...
C# control structures then here.
MSDN Beginner Developer video series
MSDN video on OOP principals, making classes, constructors, accessors and method overloading
MSDN Top guideline violations, know what to avoid before you do it.
Design patterns as diagrams

The tutorials below walk through making an application including inheritance, custom events and custom controls.
Bulding an application - Part 1
Building an application - Part 2
Quick and easy custom events
Passing values between forms/classes

Working with environmental variables
'Why do we use delegates?' thread

Debugging tutorial
Debugging tips
Debugging in detail
Great debugging tips
It still doesn't work, article

Build a Program Now! in Visual C# by Microsoft Press, ISBN 0-7356-2542-5
is a terrific book that has you build a Windows Forms application, a WPF app, a database application, your own web browser.

C# Cookbooks
Are a great place to get good code, broken down by need, written by coding professionals. You can use the code as-is, but take the time to actually study it. These professionals write in a certain style for a reason developed by years of experience and heartache.

Microsoft Visual Studio Tips, 251 ways to improve your productivity, Microsoft press, ISBN 0-7356-2640-5
Has many, many great, real-world tips that I use all the time.

Writing a text file is always one of the first things people want to do, in order to store data like high-scores, preferences and so on
Writing a text file tutorial.
Reading a text file tutorial.

And everyone always wants to connect to a database, right out of the gate so
Database tutorials right here on DIC

These are just good every-day references to put in your bookmarks.
MSDN C# Developers Center with tutorials
Welcome to Visual Studio

Have you seen the 500+ MSDN Code Samples? They spent a lot of time creating samples and demos. It seems a shame to not use them.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1