Delete 2 Dynamic Buttons from one Dynamic Button - c#

So I have an Image Map and on it I want to appear 3 buttons each time I clicked on a location. Those 3 button would be : hotspot, delete hotspot, save hotspot. These Buttons are Dynamically generated. The question is, how can I the hotspot from delete hotspot Button and also close the other 2 Buttons.
Some code for a little bit of understanding of what am I doing:
private void PictureBox1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
//Locatia
PictureBox C = new PictureBox();
int i = 0;
C.Location = new Point(e.X-13, e.Y-30);
C.Name = "Problema_" + (i + 1).ToString();
C.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_GPS_500px.png";
C.Size = new Size(26, 30);
C.SizeMode = PictureBoxSizeMode.StretchImage;
C.BackColor = Color.Transparent;
C.Cursor = Cursors.Hand;
// C.Click += new EventHandler(this.StartRecordingToolStripMenuItem_Click_1);;
PictureBox1.Controls.Add(C);
//salveaza Locatia
PictureBox S = new PictureBox();
S.Name = "Salveaza_" + (i + 1).ToString();
S.Location = new Point(e.X - 45, e.Y+10);
S.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Checked_Checkbox_500px.png";
S.Size = new Size(35, 35);
S.SizeMode = PictureBoxSizeMode.StretchImage;
S.BackColor = Color.Transparent;
S.Cursor = Cursors.Hand;
PictureBox1.Controls.Add(S);
//sterge Locatia
PictureBox St = new PictureBox();
St.Name = "Sterge_" + (i + 1).ToString();
St.Location = new Point(e.X +10, e.Y+10);
St.Cursor = Cursors.Hand;
St.ImageLocation = #"C:\Users\Starrux\Pictures\PNGs\Planner\icons8_Close_Window_500px.png";
St.Size = new Size(35, 35);
St.SizeMode = PictureBoxSizeMode.StretchImage;
St.BackColor = Color.Transparent;
PictureBox1.Controls.Add(St);
S.Click += new EventHandler(this.stergeAprob);
C.Click += new EventHandler(this.clickHotspot);
}

The solution could be that when creating dynamically you also delete it dynamically using anonymous functions
S.Click += (o, e) => {
//....actions
PictureBox1.Controls.remove(C)
//... other actions
}
we can have access to dynamically created variables since the compiler creates inline functions

Related

C#/ Visual studio 2019 || windows form autoscroll flickering

I am trying to make a Windows Form that shows display data from data a table. Every thing is working fine until I start scrolling in the Form using the auto scroll property.
I don't know why that's happening. I hope someone can help me.
private void allShowsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// Load All the Shows from the database
DataTable dt = accesse.LoadAllShows();
// Create the allShows From
Form allShows = new Form();
// Create the Controls for the AllShows form.
PictureBox[] pic = new PictureBox[dt.Rows.Count];
Label[] showName = new Label[dt.Rows.Count], season = new Label[dt.Rows.Count], eps = new Label[dt.Rows.Count], typ = new Label[dt.Rows.Count];
TextBox[] txtShowName = new TextBox[dt.Rows.Count], txtSeason = new TextBox[dt.Rows.Count], txtEps = new TextBox[dt.Rows.Count], txtTyp = new TextBox[dt.Rows.Count];
// AllShows Form properties
allShows.BackColor = Color.White;
allShows.Font = new Font("Calibri", 14f, FontStyle.Regular);
allShows.ForeColor = Color.Black;
allShows.Size = new Size(1050, 700);
allShows.StartPosition = FormStartPosition.CenterScreen;
allShows.AutoScroll = true;
allShows.AutoScrollMargin = new Size(0, 18);
// Variables
int y = 325; // the distens bettwen the controls on the Y and X.
bool xTurn = false; // the axies turn.
int yTurnNum = 0; // the y turn number.
for (int i = 0; i < dt.Rows.Count; i++)
{
// PictureBox Poster Properties
pic[i] = new PictureBox();
pic[i].BorderStyle = BorderStyle.FixedSingle;
pic[i].Size = new Size(162, 288);
pic[i].SizeMode = PictureBoxSizeMode.StretchImage;
pic[i].Image = Image.FromFile(dt.Rows[i][4].ToString());
// Label showName Properties
showName[i] = new Label();
showName[i].Text = "Show Name: " + dt.Rows[i][0];
showName[i].AutoSize = true;
// Label Season Properties
season[i] = new Label();
season[i].Text = "Season: " + dt.Rows[i][1];
season[i].AutoSize = true;
// Label Eps Properties
eps[i] = new Label();
eps[i].Text = "Episodes: " + dt.Rows[i][2];
eps[i].AutoSize = true;
// Label Typ Properties
typ[i] = new Label();
typ[i].Text = "Typ: " + dt.Rows[i][3];
typ[i].AutoSize = true;
if (xTurn)
{
// Sitting the location of the controls on the X turn
pic[i].Location = new Point(515, pic[i - 1].Location.Y);
showName[i].Location = new Point(687, showName[i - 1].Location.Y);
season[i].Location = new Point(687, season[i - 1].Location.Y);
eps[i].Location = new Point(687, eps[i - 1].Location.Y);
typ[i].Location = new Point(687, typ[i - 1].Location.Y);
xTurn = false;
}
else
{
// Sitting the location of the controls on the Y turn
pic[i].Location = new Point(15, 15 + (yTurnNum * y));
showName[i].Location = new Point(187, 20 + (yTurnNum * y));
season[i].Location = new Point(187, 55 + (yTurnNum * y));
eps[i].Location = new Point(187, 90 + (yTurnNum * y));
typ[i].Location = new Point(187, 125 + (yTurnNum * y));
yTurnNum += 1;
xTurn = true;
}
allShows.Controls.Add(pic[i]);
allShows.Controls.Add(showName[i]);
allShows.Controls.Add(season[i]);
allShows.Controls.Add(eps[i]);
allShows.Controls.Add(typ[i]);
}
allShows.ShowDialog();
}
catch (Exception ex)
{
tools.ShowErrorMessageBox(ex.Message, "Error");
}
}
I hope this gif will help you to understand what is happening.
enter image description here

Removing from the list with button click

I'm printing out items from the list each to the label, and a button nearby for removing them from the list. The removal button doesn't seem to work.
private void cart_Click(object sender, EventArgs e)
{
krepselioPanel.Visible = !krepselioPanel.Visible;
krepselioPav.Visible = !krepselioPav.Visible;
int i = 0;
double s = 0;
foreach (Patiekalas preke in prekes)
{
Label prekiulist = new Label();
prekiulist.Location = new Point(0, 26 * i);
prekiulist.Text = preke.GetPatiekalas() + " | " + preke.GetKaina() + "€";
prekiulist.Size = new Size(200, 20);
krepselioPanel.Controls.Add(prekiulist);
s += Convert.ToDouble(preke.GetKaina());
Button removeButton = new Button();
removeButton.Text = "x";
removeButton.Location = new Point(200, 26 * i);
removeButton.Font = new Font(FontFamily.GenericSansSerif, 9);
removeButton.Size = new Size(20, 22);
removeButton.Click += removeButton_Click;
removeButton.Tag = preke;
krepselioPanel.Controls.Add(removeButton);
i++;
}
Label suma = new Label();
suma.Location = new Point(krepselioPanel.Right - 140, 0);
suma.Font = new Font(FontFamily.GenericSansSerif, 13);
suma.Text = "Total: " + s + "€";
suma.Size = new Size(130, 25);
krepselioPanel.Controls.Add(suma);
}
private void removeButton_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
Patiekalas preke = (Patiekalas)b.Tag;
prekes.Remove(preke);
cart_Click(sender, e);
cart_Click(sender, e);
}
}
Just for the interest sake I changed the functions removeButton_Click line
prekes.Remove(preke);
to
prekes.Add(preke);
and this creates a new entry to the list but removal doesn't work however.
The code doesn't seem to remove the previous controls from the panel so each time the controls are added new ones are created. If you have only one item in the cart then the next time nothing seems to happen, nothing added nor removed, but if there's multiple items they will start to multiply.
So remove the controls from the panel and then add new ones and the item will disappear.

Change Background Color of Panel pro-grammatically

I have four panels and in each click events I have loaded different User controls. So when I click a particular panel I want the clicked panel background changed and rest panel to be same.
How can I do it pro-grammatically?
Panel pan1 = new Panel();
Panel pan2 = new Panel();
Panel pan3 = new Panel();
Panel pan4 = new Panel();
private void Form1_Load(object sender, EventArgs e)
{
pan1.Name = "pan1";
pan1.Location = new Point(0, 0);
pan1.Size = new Size(100, 100);
pan1.BackColor = Color.LightGray;
pan1.Click += new EventHandler(this.Panel_Click);
pan2.Name = "pan2";
pan2.Location = new Point(110, 0);
pan2.Size = new Size(100, 100);
pan2.BackColor = Color.LightGray;
pan2.Click += new EventHandler(this.Panel_Click);
pan3.Name = "pan3";
pan3.Location = new Point(220, 0);
pan3.Size = new Size(100, 100);
pan3.BackColor = Color.LightGray;
pan3.Click += new EventHandler(this.Panel_Click);
pan4.Name = "pan4";
pan4.Location = new Point(330, 0);
pan4.Size = new Size(100, 100);
pan4.BackColor = Color.LightGray;
pan4.Click += new EventHandler(this.Panel_Click);
this.Controls.Add(pan1);
this.Controls.Add(pan2);
this.Controls.Add(pan3);
this.Controls.Add(pan4);
}
private void Panel_Click(object sender , EventArgs e)
{
this.pan1.BackColor = Color.LightGray;
this.pan2.BackColor = Color.LightGray;
this.pan3.BackColor = Color.LightGray;
this.pan4.BackColor = Color.LightGray;
Panel pan = (Panel)sender;
pan.BackColor = Color.Red;
}

I need to create buttons dynamically. But each button needs a different code

I need to program about adding rooms. Its like this, when I press a Add button, there has to show a label, a textbox, a checkbox and a button(to reset the others). But when I dynamically create a button and press reset, it will reset the other textboxes and checkboxes also.
What do I have to type in my 'New button event'?
Can you guys help me out?
//this is my code
private void btnAdd_Click(object sender, EventArgs e) {
Label label = new Label();
int count = tableLayoutPanel1.Controls.OfType<Label>().ToList().Count;
label.Location = new Point(10, (25 * count) + 2);
label.Size = new Size(40, 20);
label.Name = "lbl" + (count + 1);
label.Text = "Kamer " + (count + 1);
label.Anchor = AnchorStyles.None;
tableLayoutPanel1.Controls.Add(label);
TextBox textbox = new TextBox();
int count1 = tableLayoutPanel1.Controls.OfType<TextBox>().ToList().Count;
textbox.Location = new System.Drawing.Point(60, 25 * count1);
textbox.Size = new System.Drawing.Size(80, 20);
textbox.Name = "textbox" + (count1 + 1);
textbox.Anchor = AnchorStyles.None;
tableLayoutPanel1.Controls.Add(textbox);
CheckBox checkbox = new CheckBox();
int count2 = tableLayoutPanel1.Controls.OfType<CheckBox>().ToList().Count;
checkbox.Location = new System.Drawing.Point(60, 25 * count2);
checkbox.Size = new System.Drawing.Size(80, 20);
checkbox.Name = "checkbox" + (count2 + 1);
checkbox.Anchor = AnchorStyles.None;
tableLayoutPanel1.Controls.Add(checkbox);
Button button = new Button();
int count3 = tableLayoutPanel1.Controls.OfType<Button>().ToList().Count;
button.Location = new Point(10, (25 * count3) + 2);
button.Size = new Size(140, 25);
button.Name = "button" + (count3 + 1);
button.Text = "Reset";
button.Anchor = AnchorStyles.None;
button.Click += new System.EventHandler(this.Button_Click);
tableLayoutPanel1.Controls.Add(button);
}

Drawing inside a picturebox (control) inside foreach

I'm trying to making dynamic controls (labels, pictureboxes and buttons) by making controls in a foreach. The foreach is controlled by datarows, which are created from an SQL function that I call for.
The problem is that my graphics don't seem to work on my pictureboxes as it is now.
So far I've got this as code:
Global variables:
private int i = 0, beginningHeight = 70, addingToHeight = 55;
PictureBox picturebox = new PictureBox();
The functions:
private void tonenAlleCategorieen()
{
foreach (DataRow dr in blCategorie.getAlleCategorieenMetLimieten())
{
//making labels dyanmic and fill them with the correct text (from database)
string categorie = (string)dr.Field<string>("Omschrijving");
Label label = new Label();
label.BackColor = Color.Transparent;
label.ForeColor = Color.FromArgb(97, 97, 97);
label.Font = new Font("Myriam Pro", 10, FontStyle.Bold);
label.Width = 200;
label.Name = categorie;
label.Text = categorie;
label.BackColor = Color.Transparent;
label.Location = new Point(30, beginningHeight + addingToHeight);
this.Controls.Add(label);
// getting the figures (max figures) from the db to show in a label
double limiet = (double)dr.Field<double>("maximumBedrag");
Label labeltest = new Label();
labeltest.BackColor = Color.Transparent;
labeltest.ForeColor = Color.FromArgb(97, 97, 97);
labeltest.Font = new Font("Myriam Pro", 8, FontStyle.Bold);
labeltest.Width = 200;
labeltest.Name = Convert.ToString(limiet);
labeltest.Text = "Limiet: " + Convert.ToString(limiet) + "€";
labeltest.BackColor = Color.Transparent;
labeltest.Location = new Point(30, (beginningHeight + 27) + addingToHeight);
this.Controls.Add(labeltest);
//making pictureboxes for every single row in the db
PictureBox picturebox = new PictureBox();
picturebox.Width = 400;
picturebox.Name = "picturebox" + i;
picturebox.Height = 15;
picturebox.Location = new Point(30, (beginningHeight + 27) + addingToHeight);
this.Controls.Add(picturebox);
//calling the paint event for drawing inside the pictureboxes
picturebox.Paint += new PaintEventHandler(picturebox_Paint);
//adjusting height (55px extra per new row)
beginningHeight += 55;
i++;
}
}
private void picturebox_Paint(object sender, PaintEventArgs e)
{
Graphics g = e.Graphics;
//draw here
//Graphics g = picturebox.CreateGraphics();
int x = 30;
int y = (beginningHeight + 27) + addingToHeight;
int breedteGebruikt = 200;
int breedteNietGebruikt = picturebox.Width - breedteGebruikt;
int hoogteBalk = picturebox.Height;
g.DrawRectangle(new Pen(Color.Red), new Rectangle(10, 5, 50, 5));
g.FillRectangle(Brushes.Green, x, y, breedteNietGebruikt, hoogteBalk);
g.FillRectangle(Brushes.Red, x, y, breedteGebruikt, hoogteBalk);
picturebox.Refresh();
}
Can anybody help me out here and tell me how I can add the graphics into my pictureboxes so I can see how much percentage of my picturebox should be filled?
Here is a picture example to have a good look onto it:
As you see in the above image it currently doesn't work, and I've put data in the database for the first record named "Boodschappen" which should now be filled in by my graphics for 30% in this example.
Does anybody know a solution please? :)
Thanks
Now the troubles only rise on this part: I'm not allowed to add the g
to this.Controls.Add(g); it gives me the error Argument 1: cannot
convert from 'System.Drawing.Graphics' to
'System.Windows.Forms.Control
It's clear you can't add Graphics as a control. Also if you draw that way your drawing will disappear when picturebox or form repainted. So you should draw inside the Paint event of picturebox.
double maxLimit = 0;
int maxleftpos = 0;
private void tonenAlleCategorieen()
{
foreach (DataRow dr in blCategorie.getAlleCategorieenMetLimieten())
{
//making labels dyanmic and fill them with the correct text (from database)
string categorie = (string)dr.Field<string>("Omschrijving");
Label label = new Label();
label.BackColor = Color.Transparent;
label.ForeColor = Color.FromArgb(97, 97, 97);
label.Font = new Font("Myriam Pro", 10, FontStyle.Bold);
label.Width = 200;
label.Name = categorie;
label.Text = categorie;
label.BackColor = Color.Transparent;
label.Location = new Point(10, beginningHeight + addingToHeight);
maxleftpos = Math.Max(label.Left + label.Width, maxleftpos);
this.Controls.Add(label);
// getting the figures (max figures) from the db to show in a label
double limiet = (double)dr.Field<double>("maximumBedrag");
maxLimit = Math.Max(limiet, maxLimit);
Label labeltest = new Label();
labeltest.BackColor = Color.Transparent;
labeltest.ForeColor = Color.FromArgb(97, 97, 97);
labeltest.Font = new Font("Myriam Pro", 8, FontStyle.Bold);
labeltest.Width = 200;
labeltest.Name = Convert.ToString(limiet);
labeltest.Text = "Limiet: " + Convert.ToString(limiet) + "€";
labeltest.BackColor = Color.Transparent;
labeltest.Location = new Point(30, (beginningHeight + 27) + addingToHeight);
this.Controls.Add(labeltest);
//making pictureboxes for every single row in the db
PictureBox picturebox = new PictureBox();
picturebox.Width = 200;
picturebox.Name = "picturebox" + i;
picturebox.Height = 15;
picturebox.Tag = limiet;
picturebox.Location = new Point(100, (beginningHeight + 27) + addingToHeight);
this.Controls.Add(picturebox);
picturebox.BringToFront();
//calling the paint event for drawing inside the pictureboxes
picturebox.Paint += new PaintEventHandler(picturebox_Paint);
//adjusting height (55px extra per new row)
beginningHeight += 55;
i++;
}
foreach (Control c in this.Controls)
{
if (c is PictureBox)
{
c.Location = new Point(maxleftpos, c.Top);
}
}
if (this.Width<maxleftpos+150)
{
this.Width = maxleftpos + 50;
}
this.Refresh();
}
private void picturebox_Paint(object sender, PaintEventArgs e)
{
PictureBox p = sender as PictureBox;
Graphics gr = e.Graphics;
gr.ResetTransform();
//Graphics g = picturebox.CreateGraphics();
int breedteGebruikt = Convert.ToInt32((double)p.Tag);
int max = Convert.ToInt32(maxLimit);
int grwidht = breedteGebruikt * p.Width / max;
gr.DrawRectangle(new Pen(Color.Red), new Rectangle(10, 5, 50, 5));
gr.FillRectangle(Brushes.Green, 0, 0, p.Width, p.Height);
gr.FillRectangle(Brushes.Red, 0, 0, grwidht, p.Height);
}

Categories

Resources