i have this assignement when i need to create a winform of all classes that i ve implementedalready did form add but got issues in my list form cant access list of borrowers from form add1
here is my form list1.cs
namespace TP.A { public partial class list1 : Form{
private Button Exit; private GestionEmprunteur _gestionEmprunteur;
public list1()
{
_gestionEmprunteur = new GestionEmprunteur();
InitializeComponent_D();
InitializeDataGridView();
// Abonnement à l'événement OnAddEmprunteur de l'objet add1
add1 addForm = new add1();
addForm.OnAddEmprunteur += AddEmprunteur;
}
private DataGridView dgvBorrowers;
private void InitializeDataGridView()
{
{
//create DataGridView to display borrowers
dgvBorrowers = new DataGridView();
dgvBorrowers.Location = new Point(100,200);
dgvBorrowers.Size = new Size(798, 450);
dgvBorrowers.AutoGenerateColumns = true;
dgvBorrowers.DataSource = _gestionEmprunteur.Lister2();
this.Controls.Add(dgvBorrowers);
}
}
private void AddEmprunteur(Emprunteur emprunteur)
{
_gestionEmprunteur.AddEmprunteur(emprunteur);
dgvBorrowers.DataSource = _gestionEmprunteur.Lister2();
}
private void InitializeComponent_D()
{
// DESIGN1.ApplyDesign(this);
//create submit button
Exit = new Button();
Exit.Text = "Exit";
Exit.Font = new Font("Times New Roman", 12, FontStyle.Regular);
Exit.Location = new Point((this.Width - Exit.Width) / 2, 500);
Exit.Size = new Size(100, 50);
Exit.Click += new EventHandler(exit_Click);
this.Controls.Add(Exit);
Exit.BringToFront();
Exit.RightToLeft = RightToLeft.Yes;
}
private void exit_Click(object? sender, EventArgs e)
{
this.Close();
} }}
here is my form add1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Validation.services;
using serie2.Validation.services;
using TP.main;
using System.Windows.Forms;
namespace TP.A
{
public partial class add1 : Form
{
public delegate void AddEmprunteurHandler(Emprunteur emprunteur);
public event AddEmprunteurHandler OnAddEmprunteur;
private Emprunteur emprunteur;
private GestionEmprunteur _gestionEmprunteur;
private TextBox _nameTextBox;
private TextBox _firstNameTextBox;
private Button _submitButton;
private Button _cancelButton;
private Label _nameLabel;
private Label _loginLabel;
private Label _firstNameLabel;
private List<Emprunteur> _emprunteurs;
public add1()
{
_emprunteurs = new List<Emprunteur>();
_gestionEmprunteur = new GestionEmprunteur();
InitializeComponent(this);
}
private void InitializeComponent( Form form)
{
DESIGN1.ApplyDesign(this);
//create label for name
Label loginLabel = new Label();
loginLabel.Text = "Login :";
loginLabel.Left = 0;
loginLabel.ForeColor = Color.White;
loginLabel.BackColor = Color.Black;
loginLabel.BackColor = Color.FromArgb(128, 0, 0, 0);
loginLabel.Location = new Point((form.Width - loginLabel.Width) / 2, 100);
loginLabel.Font = new Font("Andalus", 22, FontStyle.Bold);
loginLabel.AutoSize = true;
// nameLabel.Location = new Point((form.Width - nameLabel.Width) / 2, 250);
this.Controls.Add(loginLabel);
loginLabel.BringToFront();
//create label for name
Label nameLabel = new Label();
nameLabel.Text = "Name:";
nameLabel.Left = 0 ;
nameLabel.ForeColor = Color.White;
nameLabel.BackColor = Color.Black;
nameLabel.BackColor = Color.FromArgb(128, 0, 0, 0);
nameLabel.Location = new Point((form.Width - nameLabel.Width) / 2, 250);
nameLabel.Font = new Font("Times New Roman",16 ,FontStyle.Bold);
nameLabel.AutoSize = true;
// nameLabel.Location = new Point((form.Width - nameLabel.Width) / 2, 250);
this.Controls.Add(nameLabel);
nameLabel.BringToFront();
//create textbox for name
_nameTextBox = new TextBox();
// _nameTextBox.Size = new Size(150, 30);
_nameTextBox.Location = new Point((form.Width - _nameTextBox.Width) / 2, 300);
_nameTextBox.Size = new Size(150, 25);
form.Controls.Add(_nameTextBox);
_nameTextBox.BringToFront();
//create label for first name
Label firstNameLabel = new Label();
firstNameLabel.Text = "First Name:";
firstNameLabel.Left = 0;
firstNameLabel.ForeColor = Color.White;
firstNameLabel.BackColor = Color.Black;
firstNameLabel.BackColor = Color.FromArgb(128, 0, 0, 0);
firstNameLabel.Location = new Point((form.Width - firstNameLabel.Width) / 2, 350);
firstNameLabel.Font = new Font("Times New Roman", 16, FontStyle.Bold);
firstNameLabel.AutoSize = true;
this.Controls.Add(firstNameLabel);
firstNameLabel.BringToFront();
//create textbox for first name
_firstNameTextBox = new TextBox();
_firstNameTextBox.Location = new Point((form.Width - _firstNameTextBox.Width) / 2, 400);
_firstNameTextBox.Size = new Size(150, 25);
form.Controls.Add(_firstNameTextBox);
_firstNameTextBox.BringToFront();
//create submit button
_submitButton = new Button();
_submitButton.Text = "Submit";
_submitButton.Font = new Font("Times New Roman", 12, FontStyle.Regular);
_submitButton.Location = new Point((form.Width - _submitButton.Width) / 2, 500);
_submitButton.Size = new Size(100, 50);
_submitButton.Click += new EventHandler(SubmitButton_Click);
form.Controls.Add(_submitButton);
_submitButton.BringToFront();
_submitButton.RightToLeft= RightToLeft.Yes;
//create cancel button
_cancelButton = new Button();
_cancelButton.Text = "Cancel";
_cancelButton.Font = new Font("Times New Roman", 12, FontStyle.Regular);
_cancelButton.Location = new Point((form.Width - _submitButton.Width) / 180, 500);
_cancelButton.Size = new Size(100, 50);
_cancelButton.Click += new EventHandler(cancel_Click);
form.Controls.Add(_cancelButton);
_cancelButton.BringToFront();
}
private void cancel_Click(object? sender, EventArgs e)
{
this.Close();
}
private void SubmitButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(_nameTextBox.Text) && !string.IsNullOrWhiteSpace(_firstNameTextBox.Text))
{
Emprunteur emp = new Emprunteur { Nom = _nameTextBox.Text, Prenom = _firstNameTextBox.Text };
bool success = _gestionEmprunteur.AddEmprunteur(emp);
// Appel de l'événement OnAddEmprunteur
OnAddEmprunteur?.Invoke(emp);
if (success)
{
MessageBox.Show("Borrower added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
_nameTextBox.Text = "";
_firstNameTextBox.Text = "";
}
else
{
MessageBox.Show("An error occurred while adding the borrower.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
_gestionEmprunteur.Lister();
}
else
{
MessageBox.Show("Please enter a name and first name for the borrower.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
HERE ARE THE MODICATION THAT I UNCLUDED i want to share data of datagrid from my form add1.cs to my form list1.cs i added a datagrid in my form1 to ensure that my data are displayed correctly but m facing issues to connect both forms
public partial class add1 : Form
{
private Emprunteur emprunteur;
static GestionEmprunteur _gestionEmprunteur;
private TextBox _nameTextBox;
private TextBox _firstNameTextBox;
private Button _submitButton;
private Button _cancelButton;
private Label _nameLabel;
private Label _loginLabel;
private Label _firstNameLabel;
static List<Emprunteur> _emprunteurs;
private DataGridView dgvBorrowers;
public add1()
{
dgvBorrowers = new DataGridView();
dgvBorrowers.Location = new Point(40, 190);
dgvBorrowers.Size = new Size(450, 300);
dgvBorrowers.AutoGenerateColumns = true;
this.Controls.Add(dgvBorrowers);
_emprunteurs = new List<Emprunteur>();
_gestionEmprunteur = new GestionEmprunteur();
InitializeComponent(this);
}
private void InitializeComponent(Form form)
{
DESIGN1.ApplyDesign(this);
//create label for name
Label loginLabel = new Label();
loginLabel.Text = "Login :";
loginLabel.Left = 0;
loginLabel.ForeColor = Color.White;
loginLabel.BackColor = Color.Black;
loginLabel.BackColor = Color.FromArgb(128, 0, 0, 0);
loginLabel.Location = new Point((form.Width - loginLabel.Width) / 2, 100);
loginLabel.Font = new Font("Andalus", 22, FontStyle.Bold);
loginLabel.AutoSize = true;
// nameLabel.Location = new Point((form.Width - nameLabel.Width) / 2, 250);
this.Controls.Add(loginLabel);
loginLabel.BringToFront();
//create label for name
Label nameLabel = new Label();
nameLabel.Text = "Name:";
nameLabel.Left = 0;
nameLabel.ForeColor = Color.White;
nameLabel.BackColor = Color.Black;
nameLabel.BackColor = Color.FromArgb(128, 0, 0, 0);
nameLabel.Location = new Point((form.Width - nameLabel.Width) / 2, 250);
nameLabel.Font = new Font("Times New Roman", 16, FontStyle.Bold);
nameLabel.AutoSize = true;
// nameLabel.Location = new Point((form.Width - nameLabel.Width) / 2, 250);
this.Controls.Add(nameLabel);
nameLabel.BringToFront();
//create textbox for name
_nameTextBox = new TextBox();
// _nameTextBox.Size = new Size(150, 30);
_nameTextBox.Location = new Point((form.Width - _nameTextBox.Width) / 2, 300);
_nameTextBox.Size = new Size(150, 25);
form.Controls.Add(_nameTextBox);
_nameTextBox.BringToFront();
//create label for first name
Label firstNameLabel = new Label();
firstNameLabel.Text = "First Name:";
firstNameLabel.Left = 0;
firstNameLabel.ForeColor = Color.White;
firstNameLabel.BackColor = Color.Black;
firstNameLabel.BackColor = Color.FromArgb(128, 0, 0, 0);
firstNameLabel.Location = new Point((form.Width - firstNameLabel.Width) / 2, 350);
firstNameLabel.Font = new Font("Times New Roman", 16, FontStyle.Bold);
firstNameLabel.AutoSize = true;
this.Controls.Add(firstNameLabel);
firstNameLabel.BringToFront();
//create textbox for first name
_firstNameTextBox = new TextBox();
_firstNameTextBox.Location = new Point((form.Width - _firstNameTextBox.Width) / 2, 400);
_firstNameTextBox.Size = new Size(150, 25);
form.Controls.Add(_firstNameTextBox);
_firstNameTextBox.BringToFront();
//create submit button
_submitButton = new Button();
_submitButton.Text = "Submit";
_submitButton.Font = new Font("Times New Roman", 12, FontStyle.Regular);
_submitButton.Location = new Point((form.Width - _submitButton.Width) / 2, 500);
_submitButton.Size = new Size(100, 50);
_submitButton.Click += new EventHandler(SubmitButton_Click);
form.Controls.Add(_submitButton);
_submitButton.BringToFront();
_submitButton.RightToLeft = RightToLeft.Yes;
//create cancel button
_cancelButton = new Button();
_cancelButton.Text = "Cancel";
_cancelButton.Font = new Font("Times New Roman", 12, FontStyle.Regular);
_cancelButton.Location = new Point((form.Width - _submitButton.Width) / 180, 500);
_cancelButton.Size = new Size(100, 50);
_cancelButton.Click += new EventHandler(cancel_Click);
form.Controls.Add(_cancelButton);
_cancelButton.BringToFront();
}
private void cancel_Click(object? sender, EventArgs e)
{
this.Close();
}
private void SubmitButton_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(_nameTextBox.Text) && !string.IsNullOrWhiteSpace(_firstNameTextBox.Text))
{
Emprunteur emp = new Emprunteur(_nameTextBox.Text, _firstNameTextBox.Text);
bool success = _gestionEmprunteur.AddEmprunteur(emp);
if (success)
{
MessageBox.Show("Borrower added successfully.", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
dgvBorrowers.DataSource = _gestionEmprunteur._emprunteurs;
_nameTextBox.Text = "";
_firstNameTextBox.Text = "";
}
else
{
MessageBox.Show("An error occurred while adding the borrower.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Please enter a name and first name for the borrower.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
public list1()
{
_gestionEmprunteur = new GestionEmprunteur();
InitializeComponent_D();
InitializeDataGridView();
}
private void InitializeDataGridView()
{
dgvBorrowers = new DataGridView();
dgvBorrowers.Location = new Point(100, 200);
dgvBorrowers.Size = new Size(798, 450);
dgvBorrowers.AutoGenerateColumns = true;
dgvBorrowers.DataSource = _gestionEmprunteur._emprunteurs;
this.Controls.Add(dgvBorrowers);
}