Click here to Skip to main content
14,669,324 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hii All.

First I will like to tell a little about my self.
I'm a new Datatechnical apprentice and work right now on a front-line support department, but shortly I will move on to the Development department.
I'm currently working on a C# form application project, where I need to make a Form applicaiton using a SQL database and a Website (ASP) using a SQL database with C# code behind.

Now, im stuck for the first time, with a Switch-case statement where i'm using a if-else statement to do something if true. LoL

The problem is, that I maked a new form, where i'm using a comboBox to show many diffrent links. You press the comboBox, and it will show down some links, but the problem is, when i'm from the Baseform open the frmLink form, then it opens the website before I actually have chosen one. I'm using enums, and why i'm doing that is because I saw it from this link:http://blog.kayesd.com/?p=36[^], and I really like the mode he is using them, but maybe it could also be that I had misunderstood the why that wee actually should use them?

These are the three diffrent website its open:
case links_auto.Mercedes_Benz_Herlev:
    Process.Start(new ProcessStartInfo("iexplore", Link_Mercedes_Benz_Herlev));
    break;

case links_forhandler.Mercedes_Benz:
                   Process.Start(new ProcessStartInfo("iexplore", Link_Mercedes_Benz));
                   break;

case links_Reservedel.Mekonomen:
    Process.Start(new ProcessStartInfo("iexplore", Link_Mekonomen));
    break;


I no that the reason why it ceeps openning them, is because the are on the list already whem I open the frmLink form. (and this is olso a fail, but i'm also not able to remove them??)
I then, chosen to use a If-else statement to say, if I chose this website, then open it, and if I have chosen the other one, then do that, so they wont open as I open the frmLink form, but i fails all the time.

Like this:
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
    if (links_Reservedel.Mekonomen || links_Reservedel.Mekonomen = true)
    {
        switch ((links_Reservedel)comboBox3.SelectedItem)
        {
            case links_Reservedel.Mekonomen:
                Process.Start(new ProcessStartInfo("iexplore", Link_Mekonomen));
                break;
            case links_Reservedel.Thansen:
                Process.Start(new ProcessStartInfo("iexplore", Link_Thansen));
                break;
        }
    }
}



The complete soruce code without the if-else statement is here:

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

namespace Benzin_maaler.MenuLinje.MenuTools
{
    public partial class frmlinks : Form
    {

        private string Link_Mercedes_Benz_Herlev = "http://www.mercedes-benz.dk/applications/dialog/";
        private string Link_BMW_Terminalen_Brøndby = "http://www.terminalen.dk/terminalerne/koebenhavn/default.aspx";
        private string Link_VW_Glostrup = "http://vw.autohuset-glostrup.dk/";
        private string Link_Audi_Glostrup = "http://audi.autohuset-glostrup.dk/";
        private string Link_Mercedes_Benz = "http://www2.mercedes-benz.dk";
        private string Link_Bmw = "http://www.bmw.dk";
        private string Link_Audi = "http://www.audi.dk";
        private string Link_Ford = "http://www.ford.com/";
        private string Link_VW = "http://www.vw.com/en.html";
        private string Link_Opel = "http://www.opel.dk/flash.html";
        private string Link_Thansen = "http://www.thansen.dk/";
        private string Link_Mekonomen = "http://www.mekonomen.dk/";

        private enum links_auto
        {
            Mercedes_Benz_Herlev, BMW_Terminalen_Brøndby, VW_Glostrup, Audi_Glostrup
        }

        private enum links_forhandler
        {
            Mercedes_Benz, BMW, Audi, Ford, Opel, VW
        }

        private enum links_Reservedel
        {
            Thansen, Mekonomen
        }

        private enum links_div
        {

        }

        public frmlinks()
        {
            InitializeComponent();
            comboBox1.DataSource = Enum.GetValues(typeof(links_auto));
            comboBox2.DataSource = Enum.GetValues(typeof(links_forhandler));
            comboBox3.DataSource = Enum.GetValues(typeof(links_Reservedel));
            comboBox4.DataSource = Enum.GetValues(typeof(links_div));
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((links_auto)comboBox1.SelectedItem)
                {
                    case links_auto.Mercedes_Benz_Herlev:
                        Process.Start(new ProcessStartInfo("iexplore", Link_Mercedes_Benz_Herlev));
                        break;
                    case links_auto.BMW_Terminalen_Brøndby:
                        Process.Start(new ProcessStartInfo("iexplore", Link_BMW_Terminalen_Brøndby));
                        break;
                    case links_auto.Audi_Glostrup:
                        Process.Start(new ProcessStartInfo("iexplore", Link_Audi_Glostrup));
                        break;
                    case links_auto.VW_Glostrup:
                        Process.Start(new ProcessStartInfo("iexplore", Link_VW_Glostrup));
                        break;
                }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {

            switch ((links_forhandler)comboBox2.SelectedItem)
            {
                case links_forhandler.Mercedes_Benz:
                    Process.Start(new ProcessStartInfo("iexplore", Link_Mercedes_Benz));
                    break;
                case links_forhandler.BMW:
                    Process.Start(new ProcessStartInfo("iexplore", Link_Bmw));
                    break;
                case links_forhandler.Audi:
                    Process.Start(new ProcessStartInfo("iexplore", Link_Audi));
                    break;
                case links_forhandler.Ford:
                    Process.Start(new ProcessStartInfo("iexplore", Link_Ford));
                    break;
                case links_forhandler.VW:
                    Process.Start(new ProcessStartInfo("iexplore", Link_VW));
                    break;
                case links_forhandler.Opel:
                    Process.Start(new ProcessStartInfo("iexplore", Link_Opel));
                    break;
            }
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch ((links_Reservedel)comboBox3.SelectedItem)
            {
                case links_Reservedel.Mekonomen:
                    Process.Start(new ProcessStartInfo("iexplore", Link_Mekonomen));
                    break;
                case links_Reservedel.Thansen:
                    Process.Start(new ProcessStartInfo("iexplore", Link_Thansen));
                    break;
            }
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

     }
}



And a site note, when I decompile the program, then just when I open the other frmLink form, then the first thing it do is to open those three websites.

Hope there is someone out there who can give me a point of where to go, look or maybe something else :) ..


Best regard Abrek Okur
Posted
Comments
[no name] 26-Aug-11 5:48am
   
I'm not sure what you're trying to ask here. What does "fail all the time" mean? Can you rephrase your question and add more information.

Rate this:
Please Sign up or sign in to vote.

Solution 1

When the form is loaded and the data binding completed the combo box is set with a selected index of zero, which will fire the SelectedIndexChanged event. Try using SelectedValueChanged or DropDownClosed events instead
   
Comments
BillWoodruff 26-Aug-11 6:47am
   
In this case it is the databinding call alone that triggers firing of both SelectedIndexChanged and SelectedValueChanged events. In this particular code, the databinding is being done in the Form constructor, which is executed prior to the Form Load event. See my answer below for a simple remedy.
Tony Pazzard 26-Aug-11 7:33am
   
Or you can manually wire the event up in the form load method,

private void Form1_Load(object sender, EventArgs e)
{
comboBox1.SelectedIndexChanged += new EventHandlercomboBox1_SelectedIndexChanged);
}
Rate this:
Please Sign up or sign in to vote.

Solution 2

You can really simplify the code by assigning unique values to all of the enums:

private enum links_auto
{
    Mercedes_Benz_Herlev=0, BMW_Terminalen_Brandby, VW_Glostrup, Audi_Glostrup
}

private enum links_forhandler
{
    Mercedes_Benz=10, BMW, Audi, Ford, Opel, VW
}

private enum links_Reservedel
{
    Thansen=20, Mekonomen
}


And then putting the associated links into a dictionary:

Dictionary<string,> carLinks = new Dictionary<string,>();
carLinks.Add(links_auto.Mercedes_Benx_HerLev.ToString(), "http://www.mercedes-benz.dk/applications/dialog/");
... and so on...


And then you can create a single event handler for ComboBox Selected handlers:

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
    ComboBox combo = sender as ComboBox;
    string selected = combo.SelectedItem.ToString();
    string link = carLinks[selected].Value;
    Process.Start(new ProcessStartInfo("iexplore", link));
}


The end results are:

0) You no longer have to worry about the switch statement

1) You only have one method to handle what appears to be three similar combo boxes

2) It's infinitely more maintainable

Other than those comments, I see nothing wrong with your code
   
Comments
Philippe Mori 26-Aug-11 19:30pm
   
I don't know if it is possible with databinding but when item are added manually, they can ba any object which is interesting when other information should be associated with an item.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi, the 'glitch' you are running into here is caused by the fact that the 'ComboBox' Control selects its first item when you set its DataSource.

So, without your wanting it, your various calls to start Internet Explorer are being launched !

I regard this absolutely stupid behavior as a bug in the ComboBox control.

An easy work-around is to use a flag to ignore this 'spurious' selection: for example:
private bool initializing = true;

private void Form1_Load(object sender, EventArgs e)
{
    initializing = false;
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (initializing) return;
}

Welcome to the club of thousands who have been puzzled by such odd behavior of the older WinForm controls :)
   
v3
Comments
Philippe Mori 26-Aug-11 19:32pm
   
Effectively such flags are often needed... but WinForms are way much easier that it was before .NET.
Rate this:
Please Sign up or sign in to vote.

Solution 4

Thanx for your all solutions.

I think that Mr. Permalink solutions sounds like a great idea.
I will try it out, and then I report back again.

Thanx!
   

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




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100