Comprendre et Utiliser le Fichier Form1.Designer.cs dans Visual Studio
Le fichier Form1.Designer.cs est crucial dans les applications Windows Forms. Il est automatiquement généré par Visual Studio pour définir l’interface utilisateur d’un formulaire. Voici un aperçu de son fonctionnement et comment créer des composants avec l’outil de conception (Designer) et la boîte à outils (Toolbox) de Visual Studio.

Fonctionnement du fichier Form1.Designer.cs
- Déclaration des Composants : Les composants de l’interface sont déclarés comme membres privés de la classe partielle
Form1. - Méthode
InitializeComponent: Cette méthode initialise les composants du formulaire, définit leurs propriétés (taille, position, etc.), et les ajoute au formulaire. - Méthode
Dispose: Cette méthode nettoie les ressources utilisées par les composants.
Création de Composants avec Visual Studio
- Ouvrir le Designer :
- Double-cliquez sur
Form1.cspour ouvrir le formulaire en mode design.
- Ajouter des Composants :
- Faites glisser des composants depuis la boîte à outils (Toolbox) et déposez-les sur le formulaire.
- Configurer les Propriétés :
- Utilisez la fenêtre des propriétés pour configurer les attributs des composants, comme le texte affiché, la taille et la position.
- Génération Automatique de Code :
- Visual Studio génère automatiquement le code correspondant dans
Form1.Designer.cslorsque vous ajoutez ou modifiez des composants.
Exemple d’Ajout d’un Bouton
- Ajouter le Bouton :
- Faites glisser un bouton depuis la boîte à outils et déposez-le sur le formulaire.
- Configurer le Bouton :
- Changez le
NameenbtnNew, leTextenNouveau Bouton, et ajustez d’autres propriétés dans la fenêtre des propriétés.
- Code Généré :
- Le Designer ajoutera automatiquement le code pour le nouveau bouton dans
Form1.Designer.cs.
- Gestion des Événements :
- Double-cliquez sur le bouton pour générer une méthode de gestion d’événements dans
Form1.cs.
Utiliser le Designer et la Toolbox de Visual Studio permet de créer rapidement des interfaces utilisateur sans écrire manuellement le code. Les changements sont automatiquement intégrés dans le fichier Form1.Designer.cs, facilitant ainsi le développement.
Fichier Form1.Designer.cs
namespace MyCalendarToDoToday
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
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()
{
btnRefresh = new Button();
titleDate = new Label();
listBoxEventToday = new ListBox();
titleAddEvent = new Label();
btnAdd = new Button();
pickerDateStart = new DateTimePicker();
textBoxTitleNewEvent = new TextBox();
labelTitleNewEvent = new Label();
labelStartTitle = new Label();
labelTitleEnd = new Label();
textBoxDescription = new TextBox();
labelTitleDescription = new Label();
pickerDateEnd = new DateTimePicker();
labelTitleLocation = new Label();
textBoxLocation = new TextBox();
pickerTimeStart = new DateTimePicker();
pickerTimeEnd = new DateTimePicker();
checkBoxAllDay = new CheckBox();
searchDateTimePicker = new DateTimePicker();
SuspendLayout();
//
// btnRefresh
//
btnRefresh.BackColor = SystemColors.Desktop;
btnRefresh.Font = new Font("Segoe UI Semibold", 9F, FontStyle.Bold, GraphicsUnit.Point);
btnRefresh.ForeColor = SystemColors.ControlLight;
btnRefresh.Location = new Point(12, 375);
btnRefresh.Name = "btnRefresh";
btnRefresh.Size = new Size(248, 40);
btnRefresh.TabIndex = 2;
btnRefresh.Text = "Actualiser";
btnRefresh.UseVisualStyleBackColor = false;
btnRefresh.Click += btnRefresh_Click;
//
// titleDate
//
titleDate.AutoSize = true;
titleDate.Font = new Font("Segoe UI", 10F, FontStyle.Bold, GraphicsUnit.Point);
titleDate.ForeColor = SystemColors.ControlLight;
titleDate.Location = new Point(12, 9);
titleDate.Name = "titleDate";
titleDate.Size = new Size(40, 19);
titleDate.TabIndex = 0;
titleDate.Text = "Date";
titleDate.TextAlign = ContentAlignment.MiddleLeft;
//
// listBoxEventToday
//
listBoxEventToday.BackColor = Color.DarkGreen;
listBoxEventToday.Font = new Font("Segoe UI Semibold", 9F, FontStyle.Bold, GraphicsUnit.Point);
listBoxEventToday.ForeColor = Color.GreenYellow;
listBoxEventToday.FormattingEnabled = true;
listBoxEventToday.HorizontalScrollbar = true;
listBoxEventToday.ItemHeight = 15;
listBoxEventToday.Location = new Point(12, 62);
listBoxEventToday.Name = "listBoxEventToday";
listBoxEventToday.ScrollAlwaysVisible = true;
listBoxEventToday.Size = new Size(248, 304);
listBoxEventToday.TabIndex = 1;
//
// titleAddEvent
//
titleAddEvent.AutoSize = true;
titleAddEvent.Font = new Font("Segoe UI", 10F, FontStyle.Bold, GraphicsUnit.Point);
titleAddEvent.ForeColor = SystemColors.ControlLight;
titleAddEvent.Location = new Point(12, 434);
titleAddEvent.Name = "titleAddEvent";
titleAddEvent.Size = new Size(157, 19);
titleAddEvent.TabIndex = 3;
titleAddEvent.Text = "Ajouter un événement";
titleAddEvent.TextAlign = ContentAlignment.MiddleLeft;
//
// btnAdd
//
btnAdd.BackColor = SystemColors.Desktop;
btnAdd.Font = new Font("Segoe UI Semibold", 9F, FontStyle.Bold, GraphicsUnit.Point);
btnAdd.ForeColor = SystemColors.ControlLight;
btnAdd.Location = new Point(12, 742);
btnAdd.Name = "btnAdd";
btnAdd.Size = new Size(248, 40);
btnAdd.TabIndex = 18;
btnAdd.Text = "Ajouter";
btnAdd.UseVisualStyleBackColor = false;
btnAdd.Click += btnAdd_Click;
//
// pickerDateStart
//
pickerDateStart.CalendarFont = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point);
pickerDateStart.CustomFormat = "dddd dd/MM/yyyy";
pickerDateStart.Format = DateTimePickerFormat.Custom;
pickerDateStart.Location = new Point(12, 519);
pickerDateStart.MaxDate = new DateTime(2100, 12, 31, 0, 0, 0, 0);
pickerDateStart.MinDate = new DateTime(2000, 1, 1, 0, 0, 0, 0);
pickerDateStart.Name = "pickerDateStart";
pickerDateStart.Size = new Size(171, 23);
pickerDateStart.TabIndex = 8;
pickerDateStart.Value = new DateTime(2024, 4, 20, 15, 16, 9, 0);
pickerDateStart.ValueChanged += pickerDateStart_ValueChanged;
//
// textBoxTitleNewEvent
//
textBoxTitleNewEvent.BackColor = Color.DarkGreen;
textBoxTitleNewEvent.BorderStyle = BorderStyle.FixedSingle;
textBoxTitleNewEvent.Font = new Font("Segoe UI Semibold", 9F, FontStyle.Bold, GraphicsUnit.Point);
textBoxTitleNewEvent.ForeColor = Color.GreenYellow;
textBoxTitleNewEvent.Location = new Point(12, 473);
textBoxTitleNewEvent.Name = "textBoxTitleNewEvent";
textBoxTitleNewEvent.Size = new Size(248, 23);
textBoxTitleNewEvent.TabIndex = 6;
//
// labelTitleNewEvent
//
labelTitleNewEvent.AutoSize = true;
labelTitleNewEvent.BackColor = SystemColors.Desktop;
labelTitleNewEvent.Font = new Font("Segoe UI", 9.75F, FontStyle.Bold, GraphicsUnit.Point);
labelTitleNewEvent.ForeColor = SystemColors.ControlLight;
labelTitleNewEvent.Location = new Point(12, 453);
labelTitleNewEvent.Name = "labelTitleNewEvent";
labelTitleNewEvent.Size = new Size(37, 17);
labelTitleNewEvent.TabIndex = 5;
labelTitleNewEvent.Text = "Titre";
//
// labelStartTitle
//
labelStartTitle.AutoSize = true;
labelStartTitle.BackColor = SystemColors.Desktop;
labelStartTitle.Font = new Font("Segoe UI", 9.75F, FontStyle.Bold, GraphicsUnit.Point);
labelStartTitle.ForeColor = SystemColors.ControlLight;
labelStartTitle.Location = new Point(12, 499);
labelStartTitle.Name = "labelStartTitle";
labelStartTitle.Size = new Size(46, 17);
labelStartTitle.TabIndex = 7;
labelStartTitle.Text = "Début";
//
// labelTitleEnd
//
labelTitleEnd.AutoSize = true;
labelTitleEnd.BackColor = SystemColors.Desktop;
labelTitleEnd.Font = new Font("Segoe UI", 9.75F, FontStyle.Bold, GraphicsUnit.Point);
labelTitleEnd.ForeColor = SystemColors.ControlLight;
labelTitleEnd.Location = new Point(12, 545);
labelTitleEnd.Name = "labelTitleEnd";
labelTitleEnd.Size = new Size(27, 17);
labelTitleEnd.TabIndex = 10;
labelTitleEnd.Text = "Fin";
//
// textBoxDescription
//
textBoxDescription.BackColor = Color.DarkGreen;
textBoxDescription.BorderStyle = BorderStyle.FixedSingle;
textBoxDescription.Font = new Font("Segoe UI Semibold", 9F, FontStyle.Bold, GraphicsUnit.Point);
textBoxDescription.ForeColor = Color.GreenYellow;
textBoxDescription.Location = new Point(12, 657);
textBoxDescription.Multiline = true;
textBoxDescription.Name = "textBoxDescription";
textBoxDescription.Size = new Size(248, 79);
textBoxDescription.TabIndex = 17;
//
// labelTitleDescription
//
labelTitleDescription.AutoSize = true;
labelTitleDescription.BackColor = SystemColors.Desktop;
labelTitleDescription.Font = new Font("Segoe UI", 9.75F, FontStyle.Bold, GraphicsUnit.Point);
labelTitleDescription.ForeColor = SystemColors.ControlLight;
labelTitleDescription.Location = new Point(12, 637);
labelTitleDescription.Name = "labelTitleDescription";
labelTitleDescription.Size = new Size(79, 17);
labelTitleDescription.TabIndex = 16;
labelTitleDescription.Text = "Description";
//
// pickerDateEnd
//
pickerDateEnd.CalendarFont = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point);
pickerDateEnd.CalendarForeColor = SystemColors.Window;
pickerDateEnd.CalendarMonthBackground = SystemColors.ControlText;
pickerDateEnd.CustomFormat = "dddd dd/MM/yyyy";
pickerDateEnd.Font = new Font("Segoe UI", 9F, FontStyle.Regular, GraphicsUnit.Point);
pickerDateEnd.Format = DateTimePickerFormat.Custom;
pickerDateEnd.Location = new Point(12, 565);
pickerDateEnd.MaxDate = new DateTime(2100, 12, 31, 0, 0, 0, 0);
pickerDateEnd.MinDate = new DateTime(2000, 1, 1, 0, 0, 0, 0);
pickerDateEnd.Name = "pickerDateEnd";
pickerDateEnd.Size = new Size(171, 23);
pickerDateEnd.TabIndex = 11;
pickerDateEnd.Value = new DateTime(2024, 4, 20, 0, 0, 0, 0);
pickerDateEnd.ValueChanged += pickerDateEnd_ValueChanged;
//
// labelTitleLocation
//
labelTitleLocation.AutoSize = true;
labelTitleLocation.BackColor = SystemColors.Desktop;
labelTitleLocation.Font = new Font("Segoe UI", 9.75F, FontStyle.Bold, GraphicsUnit.Point);
labelTitleLocation.ForeColor = SystemColors.ControlLight;
labelTitleLocation.Location = new Point(12, 591);
labelTitleLocation.Name = "labelTitleLocation";
labelTitleLocation.Size = new Size(34, 17);
labelTitleLocation.TabIndex = 13;
labelTitleLocation.Text = "Lieu";
//
// textBoxLocation
//
textBoxLocation.BackColor = Color.DarkGreen;
textBoxLocation.BorderStyle = BorderStyle.FixedSingle;
textBoxLocation.Font = new Font("Segoe UI Semibold", 9F, FontStyle.Bold, GraphicsUnit.Point);
textBoxLocation.ForeColor = Color.GreenYellow;
textBoxLocation.Location = new Point(12, 611);
textBoxLocation.Name = "textBoxLocation";
textBoxLocation.Size = new Size(248, 23);
textBoxLocation.TabIndex = 14;
//
// pickerTimeStart
//
pickerTimeStart.CustomFormat = "HH:mm";
pickerTimeStart.Format = DateTimePickerFormat.Custom;
pickerTimeStart.Location = new Point(189, 519);
pickerTimeStart.MaxDate = new DateTime(2100, 12, 31, 0, 0, 0, 0);
pickerTimeStart.MinDate = new DateTime(2000, 1, 1, 0, 0, 0, 0);
pickerTimeStart.Name = "pickerTimeStart";
pickerTimeStart.ShowUpDown = true;
pickerTimeStart.Size = new Size(71, 23);
pickerTimeStart.TabIndex = 9;
pickerTimeStart.Value = new DateTime(2023, 2, 19, 12, 0, 0, 0);
pickerTimeStart.ValueChanged += pickerTimeStart_ValueChanged;
//
// pickerTimeEnd
//
pickerTimeEnd.CustomFormat = "HH:mm";
pickerTimeEnd.Format = DateTimePickerFormat.Custom;
pickerTimeEnd.Location = new Point(189, 565);
pickerTimeEnd.MaxDate = new DateTime(2100, 12, 31, 0, 0, 0, 0);
pickerTimeEnd.MinDate = new DateTime(2000, 1, 1, 0, 0, 0, 0);
pickerTimeEnd.Name = "pickerTimeEnd";
pickerTimeEnd.ShowUpDown = true;
pickerTimeEnd.Size = new Size(71, 23);
pickerTimeEnd.TabIndex = 12;
pickerTimeEnd.Value = new DateTime(2023, 2, 19, 13, 0, 0, 0);
pickerTimeEnd.ValueChanged += pickerTimeEnd_ValueChanged;
//
// checkBoxAllDay
//
checkBoxAllDay.AutoSize = true;
checkBoxAllDay.BackColor = SystemColors.Desktop;
checkBoxAllDay.Font = new Font("Segoe UI Semibold", 9F, FontStyle.Bold, GraphicsUnit.Point);
checkBoxAllDay.ForeColor = SystemColors.ControlLight;
checkBoxAllDay.Location = new Point(149, 499);
checkBoxAllDay.Name = "checkBoxAllDay";
checkBoxAllDay.Size = new Size(111, 19);
checkBoxAllDay.TabIndex = 19;
checkBoxAllDay.Text = "Toute la journée";
checkBoxAllDay.UseVisualStyleBackColor = false;
checkBoxAllDay.CheckedChanged += checkBoxAllDay_CheckedChanged;
//
// searchDateTimePicker
//
searchDateTimePicker.CustomFormat = "dddd dd/MM/yyyy";
searchDateTimePicker.Format = DateTimePickerFormat.Custom;
searchDateTimePicker.Location = new Point(12, 31);
searchDateTimePicker.Name = "searchDateTimePicker";
searchDateTimePicker.Size = new Size(248, 23);
searchDateTimePicker.TabIndex = 20;
searchDateTimePicker.Value = new DateTime(2024, 4, 20, 9, 35, 53, 0);
searchDateTimePicker.ValueChanged += searchDateTimePicker_ValueChanged;
//
// Form1
//
AutoScaleDimensions = new SizeF(7F, 15F);
AutoScaleMode = AutoScaleMode.Font;
AutoScroll = true;
BackColor = SystemColors.Desktop;
ClientSize = new Size(272, 794);
Controls.Add(searchDateTimePicker);
Controls.Add(checkBoxAllDay);
Controls.Add(pickerTimeEnd);
Controls.Add(pickerTimeStart);
Controls.Add(labelTitleLocation);
Controls.Add(textBoxLocation);
Controls.Add(labelTitleDescription);
Controls.Add(textBoxDescription);
Controls.Add(pickerDateEnd);
Controls.Add(labelTitleEnd);
Controls.Add(labelStartTitle);
Controls.Add(labelTitleNewEvent);
Controls.Add(textBoxTitleNewEvent);
Controls.Add(pickerDateStart);
Controls.Add(btnAdd);
Controls.Add(titleAddEvent);
Controls.Add(titleDate);
Controls.Add(btnRefresh);
Controls.Add(listBoxEventToday);
Name = "Form1";
SizeGripStyle = SizeGripStyle.Show;
Text = "To Do Today";
ResumeLayout(false);
PerformLayout();
}
#endregion
private Label titleDate;
private ListBox listBoxEventToday;
private Button btnRefresh;
private Label titleAddEvent;
private Label labelTitleNewEvent;
private TextBox textBoxTitleNewEvent;
private Label labelStartTitle;
private DateTimePicker pickerDateStart;
private DateTimePicker pickerTimeStart;
private Label labelTitleEnd;
private DateTimePicker pickerDateEnd;
private DateTimePicker pickerTimeEnd;
private Label labelTitleLocation;
private TextBox textBoxLocation;
private Label labelTitleDescription;
private TextBox textBoxDescription;
private Button btnAdd;
private CheckBox checkBoxAllDay;
private DateTimePicker searchDateTimePicker;
}
}
