Answered Combo Box and URL Control

  • Tuesday, September 11, 2012 4:38 PM
     
     

    I'm building a combobox with a list of Friendly Names (i.e. Client 1, Client 2, etc)

    When the user clicks on one of them, i want it to go to a specific designated URL for that choice.

    I have the list and Go! set, but not sure how to designate that it should load the URL based on the choice in the list

    For example, they click on Client1 from the drop-down and it will load www.client1.com in the default browser. Is this possible in C# to designate this kind of event?

All Replies

  • Tuesday, September 11, 2012 5:02 PM
     
     Answered Has Code

    Hi,

    I would suggest you to create a custom class with properties like MyClient, and UrlAddress.

    Then create a List<T>, where T will be this custom class. Then bind this list to comboBox, and specify the dataSource to this list.

    Also define the DisplayMember to "MyClient", and ValueMember to "UrlAddess". This would mean that url would be a selected value of selected item in comboBox. 

    Now, to load (open) the url in webbroswer use Process.Start("url in here") method.

    I will give you an example:

    class MyClient
    {
       public string ClientName {get;set;}
       public string UrlAddess {get;set;}
    }
    
    //
    // Put this code bellow into some method (like constrctor):
    //create list and add data into it:
    List<MyClient> clients = new List<MyClient>();
    clients.Add(new MyClient{ClientName = "Client 1", UrlAddress = @"http://www.someurl.com"});
    //add others in same way
    
    //bind to comboBox:
    comboBox1.DataSource = clients;
    comboBox1.DisplayMember = "ClientName";
    comboBox1.ValueMember ="UrlAddress";
    
    //subcribe to SelectedIndexChanged event of comboBox, to retrevie data!!
    
    //
    //TO HERE!
    
    /inisde comboBox selectedindexchanged event:
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        MyClient c = comboBox1.SelectedItem as MyClient;
        if(c != null)
        {
            string url = c.ClientName;
            Process.Start(url);
        }
    }

    I hope it helps.

    bye


    Mitja

  • Wednesday, September 12, 2012 10:13 PM
     
      Has Code

    Sorry to bother you again Mitja, but couldn't I use the Public Form1() as the construtor like below:

    public partial class Form1 : Form
        {
            public Form1()
            {
                List<MyClient> clients = new List<MyClient>();
                clients.Add(new MyClient { ClientName = "Client 1", UrlAddress = @"http://www.google.com" });
                InitializeComponent();
            }
            class MyClient
            {
                public string ClientName { get; set; }
                public string UrlAddress { get; set; }
            }
            private void BindBigClientsList()
            {
                BigClientsList.DataSource = clients;
                BigClientsList.DisplayMember = "ClientName";
                BigClientsList.ValueMember = "UrlAddress";
            }
            private void BigClientsList_SelectedIndexChanged(object sender, EventArgs e)
            {
                MyClient c = BigClientsList.SelectedItem as MyClient;
                if (c != null)
                {
                    string url = c.ClientName;
                    Process.Start(url);
                }

  • Wednesday, September 12, 2012 11:09 PM
     
     

    You can do this with simple javascript also. On the selectedindexchanged event, call a javascript function passing the selected item url value and then use window.open to redirect to the url site in new window.

    Hope this executs more faster.


    Mark it as helpful if so!!! thanks, Mithilesh

  • Thursday, September 13, 2012 6:51 AM
     
     Answered Has Code

    Change your code to:

    public partial class Form1 : Form { List<MyClient> clients; public Form1() { InitializeComponent(); clients = new List<MyClient>(); clients.Add(new MyClient { ClientName = "Client 1", UrlAddress = @"http://www.google.com" }); //you can add more in here... //and call a method to bind the list to comboBox: BindBigClientsList(); } private void BindBigClientsList() { BigClientsList.DataSource = clients; BigClientsList.DisplayMember = "ClientName"; BigClientsList.ValueMember = "UrlAddress"; }

    private void BigClientsList_SelectedIndexChanged(object sender, EventArgs e) { MyClient c = BigClientsList.SelectedItem as MyClient; if (c != null) { string url = c.ClientName; Process.Start(url); } } } class MyClient { public string ClientName { get; set; } public string UrlAddress { get; set; } }




    Mitja

  • Thursday, September 13, 2012 2:28 PM
     
      Has Code

    Change your code to:

    public partial class Form1 : Form { List<MyClient> clients; public Form1() { InitializeComponent(); clients = new List<MyClient>(); clients.Add(new MyClient { ClientName = "Client 1", UrlAddress = @"http://www.google.com" }); //you can add more in here... //and call a method to bind the list to comboBox: BindBigClientsList(); } private void BindBigClientsList() { BigClientsList.DataSource = clients; BigClientsList.DisplayMember = "ClientName"; BigClientsList.ValueMember = "UrlAddress"; }

    private void BigClientsList_SelectedIndexChanged(object sender, EventArgs e) { MyClient c = BigClientsList.SelectedItem as MyClient; if (c != null) { string url = c.ClientName; Process.Start(url); } } } class MyClient { public string ClientName { get; set; } public string UrlAddress { get; set; } }




    Mitja


    I think I see, if I call the clients outside of the proper constructor, than it doesn't know what to populate into the drop-down list so it will come up blank basically even though data is present. Am I somehwere in the ball park?
  • Thursday, September 13, 2012 8:21 PM
     
     

    I altered the code and generated the necessary System.Diagnostics

    However, I'm getting a win32 exception error at the Process.Start(url);

    Saying the system cannot find the file specified. If I bypass the error, it loads but of course the Process.Start doesn't run