Bonjour,
Je t'ai fait un petit programme qui illustre comment faire de 2 façons :
Première avec de if, else if, else
Deuxième avec switch(String).
Je te laisse le code designeur et de la form 1 pour que tu puisse recréer le programme et explorer.
Form1.cs
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;
namespace ComboBoxCouleur
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private int m_X;
private void btnValeurXIf_Click(object sender, EventArgs e)
{
if (cmbChoixCouleur.Text != "")
{
if (cmbChoixCouleur.Text == "Rouge")
m_X = 1;
else if (cmbChoixCouleur.Text == "Vert")
m_X = 2;
else
//C'est automatiquement bleau puisqu'Il y 3 couleur et que j'ai éliminer le texte vide.
//Si tu avais plus de couleur, tu continurais avec les else if.
m_X = 3;
txtValeurX.Text = m_X.ToString();
}
}
private void btnValeurXSwitch_Click(object sender, EventArgs e)
{
if (cmbChoixCouleur.Text != "")
{
switch (cmbChoixCouleur.Text)
{
case "Rouge":
m_X = 1;
break;
case "Vert":
m_X = 2;
break;
case "Bleu":
m_X = 3;
break;
default:
break;
}
txtValeurX.Text = m_X.ToString();
}
}
}
}
Designeur
namespace ComboBoxCouleur
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// true if managed resources should be disposed; otherwise, false.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.cmbChoixCouleur = new System.Windows.Forms.ComboBox();
this.txtValeurX = new System.Windows.Forms.TextBox();
this.btnValeurXIf = new System.Windows.Forms.Button();
this.btnValeurXSwitch = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// cmbChoixCouleur
//
this.cmbChoixCouleur.FormattingEnabled = true;
this.cmbChoixCouleur.Items.AddRange(new object[] {
"Rouge",
"Vert",
"Bleu"});
this.cmbChoixCouleur.Location = new System.Drawing.Point(12, 12);
this.cmbChoixCouleur.Name = "cmbChoixCouleur";
this.cmbChoixCouleur.Size = new System.Drawing.Size(206, 21);
this.cmbChoixCouleur.TabIndex = 0;
//
// txtValeurX
//
this.txtValeurX.Location = new System.Drawing.Point(224, 12);
this.txtValeurX.Name = "txtValeurX";
this.txtValeurX.Size = new System.Drawing.Size(56, 20);
this.txtValeurX.TabIndex = 1;
//
// btnValeurXIf
//
this.btnValeurXIf.Location = new System.Drawing.Point(12, 55);
this.btnValeurXIf.Name = "btnValeurXIf";
this.btnValeurXIf.Size = new System.Drawing.Size(135, 23);
this.btnValeurXIf.TabIndex = 2;
this.btnValeurXIf.Text = "Utiliser IF";
this.btnValeurXIf.UseVisualStyleBackColor = true;
this.btnValeurXIf.Click += new System.EventHandler(this.btnValeurXIf_Click);
//
// btnValeurXSwitch
//
this.btnValeurXSwitch.Location = new System.Drawing.Point(153, 55);
this.btnValeurXSwitch.Name = "btnValeurXSwitch";
this.btnValeurXSwitch.Size = new System.Drawing.Size(127, 23);
this.btnValeurXSwitch.TabIndex = 3;
this.btnValeurXSwitch.Text = "Utiliser Switch(String)";
this.btnValeurXSwitch.UseVisualStyleBackColor = true;
this.btnValeurXSwitch.Click += new System.EventHandler(this.btnValeurXSwitch_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 90);
this.Controls.Add(this.btnValeurXSwitch);
this.Controls.Add(this.btnValeurXIf);
this.Controls.Add(this.txtValeurX);
this.Controls.Add(this.cmbChoixCouleur);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ComboBox cmbChoixCouleur;
private System.Windows.Forms.TextBox txtValeurX;
private System.Windows.Forms.Button btnValeurXIf;
private System.Windows.Forms.Button btnValeurXSwitch;
}
}
J'espère que ca te convient
Pierre-Luc