How to change properties just one button - c#

Hey,
I have multiple buttons.I want to reduce the code so that I can set the properties of a button, but only the one I click on will change.So that I don't click on button_1 and change all the others
public static void SetProp()
{
for (int i = 0; i < buttons.Count; i++)
{
buttons[i].Image = Properties.Resources.test;
buttons[i].Width = buttons[i].Image.Width;
buttons[i].Height = buttons[i].Image.Height;
buttons[i].ImageAlign = ContentAlignment.MiddleCenter;
buttons[i].Text = null;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(0, 0, buttons[i].Width, buttons[i].Height);
buttons[i].Region = new Region(gp);
gp.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
SetProp();
}

public static void SetProp(Button button)
{
for (int i = 0; i < buttons.Count; i++)
{
button.Image = Properties.Resources.test;
button.Width = buttons[i].Image.Width;
button.Height = buttons[i].Image.Height;
button.ImageAlign = ContentAlignment.MiddleCenter;
button.Text = null;
GraphicsPath gp = new GraphicsPath();
gp.AddEllipse(0, 0, button.Width, button.Height);
button.Region = new Region(gp);
gp.Dispose();
}
}
private void button1_Click(object sender, EventArgs e)
{
SetProp(sender as Button);
}

Related

Labels don't move using Mouse events

I created 5 labels using a for. Inside the for I declared the properties of the labels, such as location, size, color, etc. and the possibility to move the labels using the mouse events:
private void Form1_Load(object sender, EventArgs e)
{
int alto = 100;
int ancho = 65;
for (byte fila = 0; fila < 5; fila++)
{
Label letras = new Label();
letras.Height = alto;
letras.Width = ancho;
letras.BackColor = Color.Blue;
letras.ForeColor = Color.AntiqueWhite;
letras.Font = new Font("Arial", 60);
letras.TextAlign = ContentAlignment.MiddleCenter;
letras.BorderStyle = BorderStyle.FixedSingle;
letras.Left = 200 + (ancho + 20) * fila;
letras.Top = 60;
letras.Text = null;
letras.MouseDown += new MouseEventHandler(this.letras_MouseDown);
letras.MouseMove += new MouseEventHandler(this.letras_MouseMove);
letras.MouseUp += new MouseEventHandler(this.letras_MouseUp);
this.Controls.Add(letras);
}
}
Later in the code, I create the events which will make the labels move.
private void letras_MouseDown(object sender, MouseEventArgs e)
{
mousedown = true;
}
private void letras_MouseMove(object sender, MouseEventArgs e)
{
if (mousedown)
{
letras.Location = new Point(letras.Location.X + e.Location.X, letras.Location.Y + e.Location.Y);
}
}
private void letras_MouseUp(object sender, MouseEventArgs e)
{
mousedown = false;
}
but it isn't working. Any ideas? I think that could be an Indentation problem, but I'm not sure.
The sender argument received by the letras_MouseMove method is the Label so the first thing to do is perform a cast to a usable Label object. Then simply test the static Control.MouseButtons property to determine if the Left button is currently active. You calculated the offset correctly. I believe that all you really needed was to have the correct Label object. (Of course I tested it)
Some of the code you posted is not necessary and should be removed. This shows a minimal working example.
public partial class MainForm : Form
{
public MainForm() => InitializeComponent();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
// This code stays the same
int alto = 100;
int ancho = 65;
for (byte fila = 0; fila < 5; fila++)
{
Label letras = new Label();
letras.Height = alto;
letras.Width = ancho;
letras.BackColor = Color.Blue;
letras.ForeColor = Color.AntiqueWhite;
letras.Font = new Font("Arial", 60);
letras.TextAlign = ContentAlignment.MiddleCenter;
letras.BorderStyle = BorderStyle.FixedSingle;
letras.Left = 200 + (ancho + 20) * fila;
letras.Top = 60;
letras.Text = null;
letras.MouseMove += new MouseEventHandler(this.letras_MouseMove);
this.Controls.Add(letras);
}
}
// This code is different
private void letras_MouseMove(object sender, MouseEventArgs e)
{
Label letra = (Label)sender;
if(MouseButtons == MouseButtons.Left)
{
// Here, your original calculation works OK once you have the correct Label object
letra.Location = new Point(letra.Location.X + e.Location.X, letra.Location.Y + e.Location.Y);
}
}
}
Haven't tested but replace:
private void Form1_Load(object sender, EventArgs e)
{
int alto = 100;
int ancho = 65;
for (byte fila = 0; fila < 5; fila++)
{
letras = new Label();
With:
Label[] labels = Array.Empty<Label>();
private void Form1_Load(object sender, EventArgs e)
{
int alto = 100;
int ancho = 65;
for (byte fila = 0; fila < 5; fila++)
{
labels[fila] = new Label();
And replace every letras in the file with labels[(number)].

Printing out Panel in C#

Again, I am asking a question about a USB Printer. This time however, the issue is more on the programming end of the spectrum.
I have a windows forms application in which I generate a receipt, which is then supposed to be sent to the printer. The Following things work: Generating the receipt, Sending the data to the printer.
The data however is only an empty strip of paper. I have the feeling that the Software is ignoring the Panel contents and just sends the empty panel to the printer.
The Sourcecode is as follows:
namespace MakeyMakey
{
public partial class Form1 : Form
{
public void Gridsettings()
{
GVReceipt.ScrollBars = ScrollBars.None;
GVReceipt.RowHeadersVisible = false;
GVReceipt.ColumnCount = 3;
GVReceipt.Columns[2].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
GVReceipt.CellBorderStyle = DataGridViewCellBorderStyle.None;
}
public Form1()
{
InitializeComponent();
pCompanyImage.SizeMode = PictureBoxSizeMode.StretchImage;
Bitmap Logo = new Bitmap(Bitmap.FromFile(#"C:/Users/manue/source/repos/MakeyMakey/MakeyMakey/logo.png"));
pCompanyImage.Image = MakeBW(Logo);
QRCodeGenerator qrGenerator = new QRCodeGenerator();
QRCodeData qrCodeData = qrGenerator.CreateQrCode("Hier kann so gut wie alles stehen", QRCodeGenerator.ECCLevel.Q);
QRCode qrCode = new QRCode(qrCodeData);
Bitmap qrCodeImage = qrCode.GetGraphic(20);
pQRCode.Image = qrCodeImage;
panel1.Height = GVProduct.Height + 220;
GVReceipt.Columns.Add("Name", "Name");
GVReceipt.Columns.Add("QTY", "QTY");
GVReceipt.Columns.Add("Price", "Price");
GVReceipt.Columns[0].Width = 50;
GVReceipt.Columns[1].Width = 15;
GVReceipt.Columns[2].Width = 50;
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label4_Click(object sender, EventArgs e)
{
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string[] row = new string[] {txtName.Text, txtQty.Text, txtPrice.Text };
GVReceipt.Rows.Add(row);
GVProduct.Rows.Add(row);
Gridsettings();
Calc();
txtName.Clear();
txtQty.Clear();
txtPrice.Clear();
txtName.Focus();
}
private void GVResize()
{
//MessageBox.Show(GVReceipt.Height.ToString());
GVReceipt.Height = GVReceipt.ColumnHeadersHeight + GVReceipt.RowCount * GVReceipt.Rows[0].Height;
//MessageBox.Show(GVReceipt.Height.ToString());
panel1.Height += GVReceipt.Rows[0].Height;
//MessageBox.Show(panel1.Height.ToString());
GVReceipt.ClearSelection();
Refresh();
}
private void GVReceipt_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
{
}
private void GVReceipt_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
{
panel1.Height = 389;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
public Bitmap MakeBW(Bitmap Map)
{
Bitmap newbmp = new Bitmap(Map.Width, Map.Height); // New image
for (int row = 0; row < Map.Width; row++) // Indicates row number
{
for (int column = 0; column < Map.Height; column++) // Indicate column number
{
var colorValue = Map.GetPixel(row, column); // Get the color pixel
var averageValue = ((int)colorValue.R + (int)colorValue.B + (int)colorValue.G) / 3; // get the average for black and white
newbmp.SetPixel(row, column, Color.FromArgb(averageValue, averageValue, averageValue)); // Set the value to new pixel
}
}
return newbmp;
}
public void Calc()
{
decimal total = 0;
for(int i = 0; i < GVReceipt.RowCount-1; i++)
{
total += Convert.ToDecimal(GVReceipt.Rows[i].Cells[2].Value) * Convert.ToDecimal(GVReceipt.Rows[i].Cells[1].Value);
}
//Console.WriteLine("HELLO");
//MessageBox.Show(total.ToString());
lbltotal.Text = total.ToString() + "€";
Refresh();
GVResize();
}
private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
Print(panel1);
}
Bitmap MemoryImage;
PrintDocument printdoc1 = new PrintDocument();
PrintPreviewDialog previewdlg = new PrintPreviewDialog();
public void GetPrintArea(Panel pnl)
{
MemoryImage = new Bitmap(pnl.Width, pnl.Height);
Rectangle rect = new Rectangle(0, 0, pnl.Width, pnl.Height);
pnl.DrawToBitmap(MemoryImage, new Rectangle(0, 0, pnl.Width, pnl.Height));
}
public void printdoc1_PrintPage(object sender, PrintPageEventArgs e)
{
e.Graphics.DrawImage(MemoryImage, 0, 0);
}
public void Print(Panel pnl)
{
GetPrintArea(pnl);
printdoc1.PrinterSettings.PrinterName = "Generico";
printdoc1.DefaultPageSettings.PaperSize = new PaperSize("Custom", panel1.Width, panel1.Height);
previewdlg.Document = printdoc1;
previewdlg.ShowDialog();
printdoc1.Print();
}
private void btnPrinter_Click(object sender, EventArgs e)
{
printDocument1.Print();
GVProduct.Rows.Clear();
GVReceipt.Rows.Clear();
Calc();
}
private void GVReceipt_RowsAdded_1(object sender, DataGridViewRowsAddedEventArgs e)
{
//GVResize();
}
}
I have also included two images, one shows what the receipt looks like, the other what the print preview generated.
I'd highy appreciate any help on that topic.

c# Picturebox Array Selection

I have created an array of picture boxes and an event for when one is clicked.
public void TicTac_Load(object sender, EventArgs e)
{
PictureBox[] PBox = new PictureBox[9];
PBox[0] = this.pictureBox1;
PBox[1] = this.pictureBox2;
PBox[2] = this.pictureBox3;
PBox[3] = this.pictureBox4;
PBox[4] = this.pictureBox5;
PBox[5] = this.pictureBox6;
PBox[6] = this.pictureBox7;
PBox[7] = this.pictureBox8;
PBox[8] = this.pictureBox9;
for (int i = 0; i < 9; i++)
{
PBox[i].Click += new System.EventHandler(PBoxes_Click);
}
}
public void PBoxes_Click(object sender, EventArgs e)
{
PictureBox myPictureBox = sender as PictureBox;
//if(Pbox[1].click){
//^^ Looking for something like this
}
My question is how can I tell which one of my pictureboxes has been clicked as i am unable to access any of them. I would just like to be able to tell which has been clicked inside the method instead of creating many.
pictureBox1_Click(object sender, EventArgs e)
Like Events
There a multiple ways to solve the issue.
You could cast sender to the correct type (here PictureBox):
public void TicTac_Load(object sender, EventArgs e)
{
PictureBox[] PBox = new PictureBox[9];
PBox[0] = this.pictureBox1;
PBox[1] = this.pictureBox2;
PBox[2] = this.pictureBox3;
PBox[3] = this.pictureBox4;
PBox[4] = this.pictureBox5;
PBox[5] = this.pictureBox6;
PBox[6] = this.pictureBox7;
PBox[7] = this.pictureBox8;
PBox[8] = this.pictureBox9;
for (int i = 0; i < 9; i++)
{
PBox[i].Click += new System.EventHandler(PBoxes_Click);
}
}
public void PBoxes_Click(object sender, EventArgs e)
{
PictureBox myPictureBox = sender as PictureBox;
}
Alternatively (less-recommended), you could move PBox to a class-level array:
PictureBox[] PBox = new PictureBox[9];
public void TicTac_Load(object sender, EventArgs e)
{
PBox[0] = this.pictureBox1;
PBox[1] = this.pictureBox2;
PBox[2] = this.pictureBox3;
PBox[3] = this.pictureBox4;
PBox[4] = this.pictureBox5;
PBox[5] = this.pictureBox6;
PBox[6] = this.pictureBox7;
PBox[7] = this.pictureBox8;
PBox[8] = this.pictureBox9;
for (int i = 0; i < 9; i++)
{
PBox[i].Click += new System.EventHandler(PBoxes_Click);
}
}
public void PBoxes_Click(object sender, EventArgs e)
{
PictureBox myPictureBox = PBox[PBox.indexOf(sender)];
}

Error using massive of panels: Object reference not set to an instance of an object

I want to try using several panels. But there is the error. When I click button_1, it should show one of the panel and hide another. I initialize my panels in Form_Load. Here is my code:
public partial class Form1 : Form
{
public Panel[] p = new Panel[2];
public int iter = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (iter%2 == 1)
{
p[0].Visible = true;
p[1].Visible = false;
}
else if (iter%2 == 0)
{
p[0].Visible = false;
p[1].Visible = true;
}
iter++;
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
p[i] = new Panel();
p[i].Visible = false;
p[i].Size = new Size(200, 100);
p[i].Location = new Point(41, 103);
}
p[0].BackColor = System.Drawing.Color.Red;
p[1].BackColor = System.Drawing.Color.Blue;
}
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
p[i] = new Panel();
p[i].Visible = false;
p[i].Size = new Size(200, 100);
p[i].Location = new Point(41, 103);
this.Controls.Add(p[i]); // You need this line to add panel to form
}
p[0].BackColor = System.Drawing.Color.Red;
p[1].BackColor = System.Drawing.Color.Blue;
}

dynamically adding buttons and button objects in panel

private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
int h = Convert.ToInt32(a);
for (int i = 0; i <= h; i++)
{
buttonArray[i] = new Button();
buttonArray[i].Size = new Size(60, 23);
buttonArray[i].Location = new Point(40,20);
panel1.Controls.Add(buttonArray[i]);
}
}
my task is if user enter 3 in text box. 3 buttons should be created dynamically and added to panel how to do that?????? i am using button array please suggest me
For example you can use this.
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
string a = textBox1.Text;
int h = Convert.ToInt32(a);
for (int i = 0; i <= h; i++)
{
var btn = new Button {Size = new Size(60, 23), Dock=DockStyle.Left, Text=h.ToString() };
btn.Click+= delegate(object sender, EventArgs e) { //your commands };
panel1.Controls.Add(btn);
}
}
In this post already answered a similar question:
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
int h = Convert.ToInt32(a);
for (int i = 0; i <= h; i++)
{
var b = new Button { Size = new Size(60, 23), Location = new Point(4 + i * 57, 20), Text = string.Format("button{0}", i) };
b.Click += b_Click;
panel1.Controls.Add(b);
}
}
void b_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
it is desirable to test the validator to always have been a number
string a = textBox1.Text;
May be using a List is more situable?
List<Button>Buttons=new List<Buttons>();
private void button1_Click(object sender, EventArgs e)
{
Buttons.Clear();
string a = textBox1.Text;
//here should be checking if "a" is digit and is not empty
int h = Convert.ToInt32(a);
for (int i = 0; i <= h; i++)
{
Button btn=new Button();
btn.Parent=panel1;
btn.Size=new Size(60, 23);
btn.Location = new Point(40,5+25*i); //arrange verically
btn.Text = "Button "+i.ToString();
btn.Click+=btn_Click;
btn.Tag="Some Value you want to restore after button click";
Buttons.Add(btn)
}
}

Categories