Types and Variables

Is it possible to make a Type into a Variable


Answer this question

Types and Variables

  • brian_tsim

    When i select the item i want to navigate with nothing happens Any Ideas

    p.s I'm Using a tabs system
    pps this is part of a favourites system.

  • BerW

    What do you mean

  • zaynun hammoud


    public string TheURL
    {
    get
    {
    return yourCombox.Text;
    }
    set
    {
    this.theURL = value;
    }
    }



  • JørnDev

    You must use Combox1 or the name of the variable ... not

    But is a string property, must return a string.

    Regards.


  • Yves1

    you need to post the entire solution really or more specifically give us details about what is happening as it would help.

    what are you selecting is it a menu item if so, you need to make sure you have a click event so you can fire that event and navigate to the page you want.

    again you need to give us more details about this :-)



  • Pon t3h pony

    that was just what i needed thanks. i Know this is bieng abit cheeky but do you have any idead on how to do the following. How to get a webpage to navigate to the selected Url

  • Stark77

    ok well what is happening is that the user press the add favourite button which is inside the favourite menu. the add code is:

    private void addNewFavouriteToolStripMenuItem_Click(object sender, EventArgs e)
    {
    Favorite theNewFavorite = new Favorite("displayName", "TheURL");

    theNewFavorite.DoAddFavorite(theNewFavorite);

    this.theFavoriteMenuToolStripItem.DropDown.Items.Add(theURL);

    this.theFavoriteMenuToolStripItem.DropDown.Items[this.theFavoriteMenu
    ToolStripItem.DropDown.Items.Count - 1].ToolTipText = theNewFavorite.TheURL;
    }

    and then that will create the Link in the Favourites menu.

    so when the Link inside the favourites menu is clicked the Browser navigates to the URL

    Code;
    private void theFavoriteMenuToolStripItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();

    thiswebpage.Navigate(theURL);
    }

    so this is my overall code;

    #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;
    using System.Security.Policy;
    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;
    using System.IO;

    #endregion

    namespace MyOwnTabbedBrowser
    {
    public partial class BrowserExplorer : Form
    {
    int current_tab_count = 0;

    private string theURL = String.Empty;

    ArrayList tabpages = new ArrayList();
    ArrayList webpages = new ArrayList();

    private Favorite theFavorites = new Favorite();

    public BrowserExplorer()
    {
    InitializeComponent();
    tabControl1.TabPages.Clear();
    Create_a_new_tab();
    WebBrowser webpage = GetCurrentWebBrowser();
    webpage.GoHome();

    }

    public string TheURL
    {
    get
    {
    return this.theURL;

    }
    set
    {
    this.theURL = value;

    comboBoxurl.Text = this.theURL;
    }
    }

    public void DoAddFavorite(Favorite theFavToAdd)
    {
    this.theFavorites.Add(theFavToAdd);
    }

    public Favorite(string url)
    {
    theURL = url;
    }

    //*************************** Button Events ***************************//

    private void tsbGo_Click(object sender, EventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.Navigate(comboBoxurl.Text);
    }

    private void tsHome_Click(object sender, EventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.GoHome();
    }

    private void tsBack_Click(object sender, EventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.GoBack();
    }

    private void tsForward_Click(object sender, EventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.GoForward();
    }

    private void tsStop_Click(object sender, EventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.Stop();
    }

    private void tsRefresh_Click(object sender, EventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.Refresh();
    }

    private void tsSearch_Click(object sender, EventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.GoSearch();
    }

    private void tlbNewtab_Click(object sender, EventArgs e)
    {
    Create_a_new_tab();

    WebBrowser thiswebpage = GetCurrentWebBrowser();
    thiswebpage.Navigate(comboBoxurl.Text);
    }

    private void addNewFavouriteToolStripMenuItem_Click(object sender, EventArgs e)
    {
    Favorite theNewFavorite = new Favorite("displayName", "TheURL");

    theNewFavorite.DoAddFavorite(theNewFavorite);

    this.theFavoriteMenuToolStripItem.DropDown.Items.Add(theURL);

    this.theFavoriteMenuToolStripItem.DropDown.Items[this.theFavoriteMenuToolStripItem.DropDown.Items.Count - 1].ToolTipText = theNewFavorite.TheURL;
    }

    private void theFavoriteMenuToolStripItem_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();

    thiswebpage.Navigate(theURL);
    }

    //*********************** Navigation Commands **************************//

    private void ModifyNavigationButtons()
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    if (thiswebpage.CanGoBack)
    tsBack.Enabled = true;
    else
    tsBack.Enabled = false;

    if (thiswebpage.CanGoForward)
    tsForward.Enabled = true;
    else
    tsForward.Enabled = false;
    }

    //*************************** Tab Controls ****************************//

    private void Create_a_new_tab()
    {

    TabPage newpage = new TabPage("Loading...");

    tabpages.Add(newpage);

    tabControl1.TabPages.Add(newpage);

    if (current_tab_count == 10) return;

    WebBrowser webpage = new WebBrowser();

    webpage.Parent = newpage;

    webpage.Dock = DockStyle.Fill;

    webpages.Add(webpage);

    webpage.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webpage_DocumentCompleted);

    webpage.GoHome();

    tabControl1.SelectedTab = newpage;
    }

    void webpage_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
    UpdateAllNames();
    ModifyNavigationButtons();
    }

    private WebBrowser GetCurrentWebBrowser()
    {
    TabPage current_tab = tabControl1.SelectedTab;
    WebBrowser thiswebpage = (WebBrowser)webpages[tabpages.IndexOf(current_tab)];
    return thiswebpage;
    }

    private void UpdateName(TabPage tb)
    {

    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);
    }
    }

    private void comboBoxurl_KeyUp(object sender, KeyEventArgs e)
    {
    WebBrowser thiswebpage = GetCurrentWebBrowser();
    if (e.KeyCode == Keys.Enter)
    thiswebpage.Navigate(comboBoxurl.Text);
    }

    public void BrowserExplorer_Load(object sender, EventArgs e)
    {
    this.tsBack.Enabled = false;
    this.tsbForward.Enabled = false;
    this.theFavorites = Favorite.DoLoadFavorites();

    foreach (Favorite currentItem in this.theFavorites.TheFavoritesCollection)
    {
    this.theFavoriteMenuToolStripItem.DropDown.Items.Add(currentItem.TheDisplayName);
    this.theFavoriteMenuToolStripItem.DropDown.Items[this.theFavoriteMenuToolStripItem.DropDown.Items.Count - 1].ToolTipText = currentItem.TheURL;
    }
    }



    }


    public class Favorite
    {
    //****************** String Declaration **********************//
    private string theDisplayName = string.Empty;
    private string theURL = String.Empty;
    private List<Favorite> theFavorites = new List<Favorite>();

    //****************** String Definition ***********************//
    public List<Favorite> TheFavoritesCollection
    {
    get
    {
    return this.theFavorites;
    }
    }

    public string TheDisplayName
    {
    get
    {
    return theDisplayName;
    }
    set
    {
    theDisplayName = value;
    }
    }

    public string TheURL
    {
    get
    {
    return theURL;
    }
    set
    {
    theURL = value;
    }
    }

    public Favorite() { }

    public Favorite(string displayName, string url)
    {
    theDisplayName = displayName;
    theURL = url;
    }

    public int CompareTo(Favorite fav)
    {
    return TheDisplayName.CompareTo(TheDisplayName);
    }
    //**************Favourite Add/Load/Save Functions ****************//
    public void DoAddFavorite(Favorite theFavToAdd)
    {
    this.theFavorites.Add(theFavToAdd);
    }
    public static Favorite DoLoadFavorites()
    {
    if (System.IO.File.Exists(System.Windows.Forms.Application.StartupPath + "\\favorites.xml"))
    {
    //deserialize Type[] theTypes = new Type[1];
    Type[] theTypes = new Type[1];
    theTypes[0] = typeof(List<Favorite>);
    XmlSerializer theSerializer = new XmlSerializer(typeof(Favorite), theTypes);
    System.IO.StreamReader theReader = new System.IO.StreamReader(System.Windows.Forms.Application.StartupPath + "\\favorites.xml");
    Favorite theFavorites = (Favorite)theSerializer.Deserialize(theReader);
    theReader.Close();
    return theFavorites;
    }
    else
    {
    return new Favorite();
    }
    }
    public void DoSaveFavorites()
    {
    //serialize Type[] theTypes = new Type[1];
    Type[] theTypes = new Type[1];
    theTypes[0] = typeof(List<Favorite>);
    XmlSerializer theSerializer = new XmlSerializer(typeof(Favorite), theTypes);
    System.IO.StreamWriter theWriter = new System.IO.StreamWriter(System.Windows.Forms.Application.StartupPath + "\\favorites.xml");

    theSerializer.Serialize(theWriter, this);
    theWriter.Close();
    }
    }
    }

    is this clear enough

    So what am i doing wrong


  • ScipBe

    well if you are using a webbrowser control then to navigate to a webpage:

    this.theWebBrowserControl.Navigate("http://someurl.com");

    in your case it may well be:

    this.theWebBrowserControl.Navigate(this.TheURL);



  • Eric Twietmeyer

    do you have any idead on how i can set the String TheURL to return the text from the comboBox THanks Zulbaric

  • mhorton

    Try this:

    public string TheURL
    {
    get
    {
    return this.theURL;

    }
    set
    {
    this.theURL = value;

    comboBoxName.Text = this.theURL;
    }
    }

    I hope this is what you need!

    Best Regards,



  • evdberg

    Can you explain your question

    Regards.


  • iljanated

    May be System.Diagonostics.Process.Start("http://www.microsoft.com")

  • Xancholy

    Well this is the code i'm trying to make work :

    public string TheURL
    {
    get
    {
    return ComboBox;
    }
    set
    {
    this.theURL = value;
    }
    }
    but i get the error message ;
    'System.Windows.Forms.ComboBox' is a 'type' but is used like a 'variable' 45 24

    So i want to make the type ComboBox into a Variable so that it works. Ps have you got any other ideas on how to get it to work


  • Types and Variables