Click here to Skip to main content
14,698,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How do you code a Combo Box (Drop down link box) with a submit button in windows form application in C#, in visual studios 2013.

This is what I want exactly but in c# - http://www.itechies.net/tutorials/jscript/jsexample.php-pid-combo.htm

I have to save space on my application, this would be the best route.
After selecting from the list box, click on the button and it would load a new webpage with the item selected.

Edit:
This is the code now, and Its still not working. Getting errors with "(this.DropDownList1.SelectedValue) " dropdownList1 getting errors.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            // set this.FormBorderStyle to None here if needed
            // if set to none, make sure you have a way to close the form!
        }
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == WM_NCHITTEST)
                m.Result = (IntPtr)(HT_CAPTION);
        }
 
        private const int WM_NCHITTEST = 0x84;
        private const int HT_CLIENT = 0x1;
        private const int HT_CAPTION = 0x2;
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
        }
 
        private void button1_Click(object sender, EventArgs e)
        {
            {
                switch (this.DropDownList1.SelectedValue) 
                {
                    case "Google":
                        this.Response.Redirect("http://www.google.com");
                        break;
                    case "Yahoo":
                        this.Response.Redirect("http://www.yahoo.com");
                        break;
                    case "Codeproject":
                        this.Response.Redirect("http://www.codeproject.com");
                        break;
                }
            }
        }
 
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
 
        }
    }
}


Edit:
I'm very confused with this, I tried this but no success...I know its totally wrong, does anyone know how to solve this. I have a combobox1 that has three websites in it, and I have a button that submits what you pick in the combobox1, in c# not any other language please. Thanks in advance if you know how to solve this.
private void button1_Click(object sender, EventArgs e)
        {
            {

                switch (this.comboBox1.Text)
                {
                    case "Google":
                        this.comboBox1("http://www.google.com");
                        break;
                    case "Yahoo":
                        this.comboBox1("http://www.yahoo.com");
                        break;
                    case "Codeproject":
                        this.comboBox1("http://www.codeproject.com");
                        break;
                }
            }
        }
Posted
Updated 11-Oct-14 10:03am
v3
Comments
Sergey Alexandrovich Kryukov 10-Oct-14 18:13pm
   
It depends on what do you mean by the class "ComboBox". Which one? Full type name, please. Or do you mean it the <select> HTML element. This is not exactly the "ComboBox"...
—SA
BillWoodruff 10-Oct-14 22:46pm
   
Load a web-page into what ? If this is, as you say below, a WinForms app, the keyword 'this is going to refer to the Form itself.

'Response.Redirect is a method used with ASP.NET.

Please clarify.

In Javascript, you can use either
window.location="http://www.MyWebSite.org/some_URL_taken_from_the_selected_item"
// or
window.navigate("http://www.MyWebSite.org/some_other_URL_taken_from_the_selected_item");

How you calculate the URL is up to you. You can read the value property of the control in question, or calculate the index is some array with URL, anything like that, depending on your design.

—SA
   
Try this:
in aspx:
<asp:dropdownlist id="DropDownList1" runat="server" xmlns:asp="#unknown">
       <asp:listitem>Google</asp:listitem>
       <asp:listitem>Yahoo</asp:listitem>
       <asp:listitem>Codeproject</asp:listitem>
   </asp:dropdownlist>
   <asp:button id="Button1" runat="server" onclick="Button1_Click" text="Button" xmlns:asp="#unknown" />


in code behind:

protected void Button1_Click(object sender, EventArgs e)
       {
           switch (this.DropDownList1.SelectedValue)
           {
               case "Google":
                   this.Response.Redirect("http://www.google.com");
                   break;
               case "Yahoo":
                   this.Response.Redirect("http://www.yahoo.com");
                   break;
               case "Codeproject":
                   this.Response.Redirect("http://www.codeproject.com");
                   break;
           }
       }
   
Comments
BillWoodruff 10-Oct-14 22:39pm
   
This is information you should add to your original post.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900