Je vous suggère de cette Application, elle vous aidera à la programmation :)
http://cut-urls.com/8JAlG
private void Form1_Load(object sender, EventArgs e) { listBox_couleurs.Items.Add("Rouge"); listBox_couleurs.Items.Add("Vert"); listBox_couleurs.Items.Add("Bleu"); listBox_couleurs.Items.Add("Jaune"); }
private void button_Colorer(object sender, EventArgs e) { if (listBox_couleurs.SelectedIndex == -1) MessageBox.Show("Aucune couleur", "", MessageBoxButtons.OK, MessageBoxIcon.Warning); else if (listBox_couleurs.SelectedIndex == 0) this.BackColor = Color.Red; else if (listBox_couleurs.SelectedIndex == 1) this.BackColor = Color.Green; else if (listBox_couleurs.SelectedIndex == 2) this.BackColor = Color.Blue; else this.BackColor = Color.Yellow; }
private void button_annuler(object sender, EventArgs e) { this.BackColor = DefaultBackColor; listBox_couleurs.SelectedIndex = -1; }
Exercice 2: (avec Corrigé): Evénementielle:
- private void Form1_Load(object sender, EventArgs e)
- {
- button_commander.Select();
- this.AcceptButton = button_commander;
- this.CancelButton = button_quitter;
- }
- private void button_cocher_Click(object sender, EventArgs e)
- {
- //Méthode 1
- //checkBox1.Checked = true;
- //checkBox2.Checked = true;
- //checkBox3.Checked = true;
- //checkBox4.Checked = true;
- //checkBox5.Checked = true;
- //checkBox6.Checked = true;
- //checkBox7.Checked = true;
- //checkBox8.Checked = true;
- //checkBox9.Checked = true;
- //checkBox10.Checked = true;
- //Méthode 2
- foreach (CheckBox c in this.groupBox1.Controls)
- {
- c.Checked=true;
- }
- }
- private void button_decocher_Click(object sender, EventArgs e)
- {
- //Méthode 1
- //checkBox1.Checked = false;
- //checkBox2.Checked = false;
- //checkBox3.Checked = false;
- //checkBox4.Checked = false;
- //checkBox5.Checked = false;
- //checkBox6.Checked = false;
- //checkBox7.Checked = false;
- //checkBox8.Checked = false;
- //checkBox9.Checked = false;
- //checkBox10.Checked = false;
- //Méthode 2
- foreach (CheckBox c in this.groupBox1.Controls)
- {
- c.Checked = false;
- }
- }
- private void button_commander_Click(object sender, EventArgs e)
- {
- string plat = "";
- //Méthode 1
- //if (checkBox1.Checked)
- // plat +="-"+ checkBox1.Text + "\r\n";
- //if (checkBox2.Checked)
- // plat += "-" + checkBox2.Text + "\r\n";
- //if (checkBox3.Checked)
- // plat += "-" + checkBox3.Text + "\r\n";
- //if (checkBox4.Checked)
- // plat += "-" + checkBox4.Text + "\r\n";
- //if (checkBox5.Checked)
- // plat += "-" + checkBox5.Text + "\r\n";
- //if (checkBox6.Checked)
- // plat += "-" + checkBox6.Text + "\r\n";
- //if (checkBox7.Checked)
- // plat += "-" + checkBox7.Text + "\r\n";
- //if (checkBox8.Checked)
- // plat += "-" + checkBox8.Text + "\r\n";
- //if (checkBox9.Checked)
- // plat += "-" + checkBox9.Text + "\r\n";
- //if (checkBox10.Checked)
- // plat += "-" + checkBox10.Text + "\r\n";
- //if(plat=="")
- // MessageBox.Show("Vous n'avez rien commandé!!","commande",MessageBoxButtons.OK,MessageBoxIcon.Question);
- //else
- //MessageBox.Show("Votre plat contient :\r\n" + plat,"commande",MessageBoxButtons.OK,MessageBoxIcon.Information);
- //Méthode 2
- foreach (CheckBox c in this.groupBox1.Controls)
- {
- if(c.Checked)
- plat += "-" + c.Text + "\r\n";
- }
- if (plat == "")
- MessageBox.Show("Vous n'avez rien commandé!!", "commande", MessageBoxButtons.OK, MessageBoxIcon.Question);
- else
- MessageBox.Show("Votre plat contient :\r\n" + plat, "commande", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
private void button_quitter_Click(object sender, EventArgs e) { this.Close(); }
C# program that uses Match, Regex
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
Regex regex = new Regex(@"\d+");
Match match = regex.Match("Dot 55 Perls");
if (match.Success)
{
Console.WriteLine(match.Value);
}
}
}
Output
55
*********************************************************************************C# program that uses Regex.Match
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// First we see the input string.
string input = "/content/alternate-1.aspx";
// Here we call Regex.Match.
Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$",
RegexOptions.IgnoreCase);
// Here we check the Match instance.
if (match.Success)
{
// Finally, we get the Group value and display it.
string key = match.Groups[1].Value;
Console.WriteLine(key);
}
}
}
Output
alternate-1
Pattern details
@" This starts a verbatim string literal.
content/ The group must follow this string.
[A-Za-z0-9\-]+ One or more alphanumeric characters.
(...) A separate group.
\.aspx This must come after the group.
$ Matches the end of the string.
*********************************************************************************
NextMatch. More than one match may be found. We can call the NextMatch method to search for a match that comes after the current one in the text. NextMatch can be used in a loop.C# program that uses NextMatch
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
string value = "4 AND 5";
// Get first match.
Match match = Regex.Match(value, @"\d");
if (match.Success)
{
Console.WriteLine(match.Value);
}
// Get second match.
match = match.NextMatch();
if (match.Success)
{
Console.WriteLine(match.Value);
}
}
}
Output
4
5
*********************************************************************************
Preprocess. Sometimes we can preprocess strings before using Match() on them. This can be faster and clearer. Experiment. I found using ToLower to normalize chars was a good choice.ToLowerC# program that uses ToLower, Match
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// This is the input string.
string input = "/content/alternate-1.aspx";
// Here we lowercase our input first.
input = input.ToLower();
Match match = Regex.Match(input, @"content/([A-Za-z0-9\-]+)\.aspx$");
}
}
C# program that uses static Regex
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// The input string again.
string input = "/content/alternate-1.aspx";
// This calls the static method specified.
Console.WriteLine(RegexUtil.MatchKey(input));
}
}
static class RegexUtil
{
static Regex _regex = new Regex(@"/content/([a-z0-9\-]+)\.aspx$");
/// <summary>
/// This returns the key that is matched within the input.
/// </summary>
static public string MatchKey(string input)
{
Match match = _regex.Match(input.ToLower());
if (match.Success)
{
return match.Groups[1].Value;
}
else
{
return null;
}
}
}
Output
alternate-1
C# program that matches numbers
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
// ... Input string.
string input = "Dot Net 100 Perls";
// ... One or more digits.
Match m = Regex.Match(input, @"\d+");
// ... Write value.
Console.WriteLine(m.Value);
}
}
Output
100
C# that shows value, length, index
using System;
using System.Text.RegularExpressions;
class Program
{
static void Main()
{
Match m = Regex.Match("123 Axxxxy", @"A.*y");
if (m.Success)
{
Console.WriteLine("Value = " + m.Value);
Console.WriteLine("Length = " + m.Length);
Console.WriteLine("Index = " + m.Index);
}
}
}
Output
Value = Axxxxy
Length = 6
Index = 4
*********************************************************************************
IsMatch: This method tests for a matching pattern. It does not capture groups from this pattern. It just sees if the pattern exists in a valid form in the input string.C# that uses Regex.IsMatch method
using System;
using System.Text.RegularExpressions;
class Program
{
/// <summary>
/// Test string using Regex.IsMatch static method.
/// </summary>
static bool IsValid(string value)
{
return Regex.IsMatch(value, @"^[a-zA-Z0-9]*$");
}
static void Main()
{
// Test the strings with the IsValid method.
Console.WriteLine(IsValid("dotnetperls0123"));
Console.WriteLine(IsValid("DotNetPerls"));
Console.WriteLine(IsValid(":-)"));
// Console.WriteLine(IsValid(null)); // Throws an exception
}
}
Output
True
True
False