Change image in picturebox array by button click - c#

I want to generate and change the image in the PictureBox array by button click.
Here is what I have tried so far.
public Form1() {
InitializeComponent();
}
PictureBox[] boxes = new PictureBox[6];
private void GenerateButton1_Click(object sender, EventArgs e) {
for (int i = 0; i < boxes.Length; i++) {
boxes[i] = new PictureBox(); //set the pointer to a new PictureBox instance
if (i == 0) boxes[i].Location = new System.Drawing.Point(3, 3);
if (i == 1) boxes[i].Location = new System.Drawing.Point(221, 3);
if (i == 2) boxes[i].Location = new System.Drawing.Point(439, 3);
if (i == 3) boxes[i].Location = new System.Drawing.Point(3, 210);
if (i == 4) boxes[i].Location = new System.Drawing.Point(221, 210);
if (i == 5) boxes[i].Location = new System.Drawing.Point(439, 210);
boxes[i].Size = new System.Drawing.Size(200, 200);
boxes[i].Image = Image.FromFile(Application.StartupPath + "\\red.PNG"); //for setting its image
}
this.Controls.AddRange(boxes);
}
private void button1_Click(object sender, EventArgs e) {
int i = 3;
int signal = 1;
boxes[i].SizeMode = PictureBoxSizeMode.StretchImage;
if (signal == 0) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\red.PNG");
if (signal == 1) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\green.PNG");
if (signal == 2) boxes[i].Image = Image.FromFile(Application.StartupPath + "\\grey.PNG");
}
}
}
by doing button1 click, the image will change from red.PNG to green.PNG or grey.PNG depending on the condition, however, I have to redo the image Properties declaration, for example, boxes[i].SizeMode = PictureBoxSizeMode.StretchImage;. Otherwise the PictureBox lose its properties.
Is there any simpler way to do this.
Thank you in advance.

Related

Sending wrong paper size to printer

We have started a project for printing, however we are completely stuck when it comes to telling the printer what paper size is selected.
Everytime we select the paper size and hit print, the printer preview is showing A4 everytime and not our selected size although if we open the print preferences the correct size is selected.
namespace CPrint
{
/// <summary>
/// Логика взаимодействия для ucPrint.xaml
/// </summary>
public partial class ucPrint : UserControl
{
bool SystemChange = false;
double? PaperHeight = null;
double? PaperWidth = null;
public ucPrint()
{
InitializeComponent();
App.Localization.AddControls(this, new string[]
{
"cHeader", "lPrinter", "lCopies","lLayout", "bPrintSettings","lColorManagement","lPrinterProfile", "lPositionSize", "cCenter", "lTop", "lLeft"
});
}
public static BitmapSource ConvertColorProfile(BitmapSource bitmapSource, ColorContext sourceProfile, ColorContext destinationProfile)
{
var bitmapConverted = new ColorConvertedBitmap();
bitmapConverted.BeginInit();
bitmapConverted.Source = bitmapSource;
//bitmapConverted.SourceColorContext = new ColorContext(PixelFormats.Pbgra32);// bitmapSourceFrame.ColorContexts == null ? sourceProfile : bitmapSourceFrame.ColorContexts[0];
bitmapConverted.SourceColorContext = sourceProfile;
bitmapConverted.DestinationColorContext = destinationProfile;
bitmapConverted.DestinationFormat = PixelFormats.Bgra32;
bitmapConverted.EndInit();
return bitmapConverted;
}
private void BPrint_Click(object sender, RoutedEventArgs e)
{
if (cPrinter.SelectedItem == null) { MessageBox.Show("Printer not set"); return; }
if (cPaperSize.SelectedItem == null) { MessageBox.Show("Paper size not set"); return; }
double marging = 30;
if (App.CurrentTemplateControl != null)
{
var img = App.CurrentTemplateControl.GetImage(true);
if (img == null) return;
var image = new Image() { Source = img };
if (cColorProfile != null && cColorProfile.SelectedItem != null && cColorProfile.SelectedIndex > 0)
{
Uri sourceProfileUri = new Uri((cColorProfile.SelectedItem as FileInfo).FullName);
image.Source = ConvertColorProfile(image.Source as BitmapSource, new ColorContext(PixelFormats.Pbgra32), new ColorContext(sourceProfileUri));
}
if (cMirror.IsChecked == true)
{
var transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(-1, 1, img.Width / 2, img.Height / 2));
image.RenderTransform = transformGroup;
}
PrintDialog printDialog2 = new PrintDialog();
Size size = (Size)(cPaperSize.SelectedItem as ComboBoxItem).DataContext;
printDialog2.PrintQueue = new PrintQueue(new PrintServer(), cPrinter.Text);
//if (printDialog2.ShowDialog() == true)
//{
//Size size = new Size(printDialog2.PrintableAreaWidth, printDialog2.PrintableAreaHeight);
printDialog2.PrintTicket = new PrintTicket()
{
PageMediaSize = new PageMediaSize(size.Width, size.Height)
};
//printDialog2.PrintTicket
Canvas canvas = new Canvas()
{
//Height = PrintContext.ToPx(size.Height),
//Width = PrintContext.ToPx(size.Width),
Height = size.Height,
Width = size.Width,
Background = Brushes.White
};
canvas.Children.Add(image);
double scaleW = (size.Width - marging * 2) / img.Width;
double scaleH = (size.Height - marging * 2) / img.Height;
if (scaleW < 1 || scaleH < 1)
{
Canvas.SetLeft(image, marging);
Canvas.SetTop(image, marging);
double scale = scaleW > scaleH ? scaleH : scaleW;
var transformGroup = new TransformGroup();
transformGroup.Children.Add(new ScaleTransform(scale, scale, 0, 0));
image.RenderTransform = transformGroup;
}
else if (cCenter.IsChecked == true)
{
Canvas.SetLeft(image, size.Width / 2 - img.Width / 2);
Canvas.SetTop(image, size.Height / 2 - img.Height / 2);
}
else
{
Canvas.SetLeft(image, marging);
Canvas.SetTop(image, marging);
}
printDialog2.PrintVisual(canvas, "Print");
//}
}
return;
}
private void CPrinter_DropDownOpened(object sender, EventArgs e)
{
SystemChange = true;
var lastPrinterName = cPrinter.Text;
cPrinter.Items.Clear();
int index = -1;
cPrinter.SelectedIndex = index;
foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
index++;
cPrinter.Items.Add(strPrinter);
if (strPrinter == lastPrinterName)
cPrinter.SelectedIndex = index;
}
SystemChange = false;
}
private void CPrinter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0 && SystemChange == false)
{
var printer = new System.Drawing.Printing.PrinterSettings();
printer.PrinterName = e.AddedItems[0].ToString();
var lastPaperName = cPaperSize.Text;
cPaperSize.Items.Clear();
int index = -1;
cPaperSize.SelectedIndex = index;
foreach (System.Drawing.Printing.PaperSize paper in printer.PaperSizes)
{
index++;
cPaperSize.Items.Add(new ComboBoxItem() { Content = paper.PaperName, DataContext = new Size(paper.Width, paper.Height) });
if (paper.PaperName == lastPaperName)
cPaperSize.SelectedIndex = index;
}
Properties.Settings.Default.DefaultDirectPrinter = printer.PrinterName;
Properties.Settings.Default.Save();
}
}
private void CPaperSize_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
Properties.Settings.Default.DefaultDirectPaper = ((ComboBoxItem)e.AddedItems[0]).Content.ToString();
Properties.Settings.Default.Save();
}
}
public void UpdateControls()
{
SystemChange = true;
if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectPrinter))
{
SystemChange = true;
var lastPrinterName = cPrinter.Text;
cPrinter.Items.Clear();
int index = -1;
cPrinter.SelectedIndex = index;
foreach (string strPrinter in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
{
index++;
cPrinter.Items.Add(strPrinter);
if (strPrinter == Properties.Settings.Default.DefaultDirectPrinter)
cPrinter.SelectedIndex = index;
}
SystemChange = false;
if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectPaper))
{
var printer = new System.Drawing.Printing.PrinterSettings();
printer.PrinterName = Properties.Settings.Default.DefaultDirectPrinter;
string lastPaperName = Properties.Settings.Default.DefaultDirectPaper;
cPaperSize.Items.Clear();
int indexP = -1;
cPaperSize.SelectedIndex = indexP;
foreach (System.Drawing.Printing.PaperSize paper in printer.PaperSizes)
{
indexP++;
cPaperSize.Items.Add(new ComboBoxItem() { Content = paper.PaperName, DataContext = new Size(paper.Width, paper.Height) });
if (paper.PaperName == lastPaperName)
cPaperSize.SelectedIndex = indexP;
}
}
}
if (!String.IsNullOrWhiteSpace(Properties.Settings.Default.DefaultDirectColorProfile))
{
var lastValue = Properties.Settings.Default.DefaultDirectColorProfile;
cColorProfile.Items.Clear();
int index = -1;
cColorProfile.SelectedIndex = index;
cColorProfile.Items.Add("");
index++;
foreach (var file in App.Icc.items)
{
index++;
cColorProfile.Items.Add(file);
if (file.FullName == lastValue)
cColorProfile.SelectedIndex = index;
}
}
SystemChange = false;
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
}
private void CColorProfile_DropDownOpened(object sender, EventArgs e)
{
var lastValue = cColorProfile.Text;
cColorProfile.Items.Clear();
int index = -1;
cColorProfile.SelectedIndex = index;
cColorProfile.Items.Add("");
index++;
foreach (var file in App.Icc.items)
{
index++;
cColorProfile.Items.Add(file);
if (file.Name == lastValue)
cColorProfile.SelectedIndex = index;
}
}
private void CColorProfile_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (!SystemChange)
{
Properties.Settings.Default.DefaultDirectColorProfile = (cColorProfile.SelectedItem as FileInfo)?.FullName;
Properties.Settings.Default.Save();
}
}
}
}
I expect if we select A5, it tells the print driver to print A5,
If we select a custom "user defined" paper size, it tells the printer which size is selected. And not fixing this at A4 everytime
We cant seem to set the paper size outside the print dialog.
I believe; following this and this MSDN article; you are going to want to do something along the lines of:
System.Drawing.Printing.PaperSize paperSize = new System.Drawing.Printing.PaperSize("custom", width, height);
PrintDocument printDoc = new PrintDocument();
printDoc.DefaultPageSettings.PaperSize = paperSize;
I have walk through your code,
I think some event raise by front end (XAML) on specific use cases that is the overriding the actual value in "Properties.Settings.Default."
It would be better to resolve if you provide a XAML code for this issue.
I can look into it and will give you better solution.
You can share me code here is my skype : shsakariya

Delete 2 Dynamic Buttons from one Dynamic Button

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

To clear(reset) dynamically generated picture box

` private void getbtn_Click(object sender, EventArgs e) // To generate Images
{
if (cmbDocType.SelectedIndex > 0)
{
if (con.State != ConnectionState.Open)
con.Open();
string directory = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory().ToString());
string FileNamePath = directory + "MembersDocuments\\" + GlobalValues.Member_ID + "\\" + cmbDocType.Text;
string[] list = Directory.GetFiles(FileNamePath);
if (list.Length > 0)
{
label1.Text = "";
PictureBox[] picturebox = new PictureBox[list.Length];
int y = 0;
for (int index = 0; index < picturebox.Length; index++)
{
picturebox[index] = new PictureBox();
if (x % 3 == 0)
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
picturebox[index].Location = new Point(x * 230 + 20, y);
picturebox[index].Size = new Size(200, 150);
x++;
picturebox[index].Size = new Size(200, 100);
picturebox[index].Image = Image.FromFile(list[index]);
picturebox[index].SizeMode = PictureBoxSizeMode.StretchImage;
picturebox[index].Click += new EventHandler(picturebox_Click);
cmbDocType_SelectedIndexChanged(picturebox[index], e);
this.Controls.Add(picturebox[index]);
}
}
else
{
label1.Text = "No Images to display";
label1.ForeColor = Color.Red;
}
con.Close();
}
else
{
MessageBox.Show("Please select the Document Type");
}
}
`
Can anyone tell me how to clear previous images(result of first call) in dynamically created pictureboxes on a new call. On making new call,previous images should not be seen..in c#
I have combobox named Type.
lets say if i have Aminals,Birds in my Combobox.
on first call pictures of animals will be displayed and on choosing combobox for second time i.e birds, Pictures of Both the types are getting displayed.
i Need to display Pictures of single type at a time. in c#
Thanks;
As suggested by TaW in the comments:
private List<PictureBox> PBs = new List<PictureBox>();
private void getbtn_Click(object sender, EventArgs e) // To generate Images
{
if (cmbDocType.SelectedIndex > 0)
{
foreach(PictureBox pb in PBs)
{
pb.Dispose();
}
PBs.Clear();
if (con.State != ConnectionState.Open)
con.Open();
string directory = System.IO.Directory.GetDirectoryRoot(System.IO.Directory.GetCurrentDirectory().ToString());
string FileNamePath = directory + "MembersDocuments\\" + GlobalValues.Member_ID + "\\" + cmbDocType.Text;
string[] list = Directory.GetFiles(FileNamePath);
if (list.Length > 0)
{
label1.Text = "";
PictureBox PB;
int y = 0;
for (int index = 0; index < list.Length; index++)
{
PB = new PictureBox();
if (x % 3 == 0)
{
y = y + 150; // 3 images per rows, first image will be at (20,150)
x = 0;
}
PB.Location = new Point(x * 230 + 20, y);
PB.Size = new Size(200, 150);
x++;
PB.Size = new Size(200, 100);
PB.Image = Image.FromFile(list[index]);
PB.SizeMode = PictureBoxSizeMode.StretchImage;
PB.Click += new EventHandler(picturebox_Click);
PBs.Add(PB);
this.Controls.Add(PB)
cmbDocType_SelectedIndexChanged(PB, e);
}
}
else
{
label1.Text = "No Images to display";
label1.ForeColor = Color.Red;
}
con.Close();
}
else
{
MessageBox.Show("Please select the Document Type");
}
}

How to enable a start button after clicking stop buttons

I want to enable my start button after clicking all the three stop buttons.
I tried to place the button btn4.enabled = false inside the (sender == btn3), but the start button will be enabled if I first clicked on that button.
The three stop buttons can be clicked in random order.
Here's my code so far:
namespace SlotMachine
{
class SlotMac
{
private Form f;
Button btn1 = new Button(); // First stop
Button btn2 = new Button(); // Second stop
Button btn3 = new Button(); // Third stop
Button btn4 = new Button(); // Start
Timer Clock; // Tick
Timer Clock1; // Tick
Timer Clock2; // Tick
Int32 tick = 0;
Label tb = new Label();
int[] nNum = new int[3];
public SlotMac()
{
f = new Form();
f.Text = "Slot Machine";
//f.Size = new Size(800, 700);
f.FormBorderStyle = FormBorderStyle.FixedSingle;
}
PictureBox[] pics = new PictureBox[7];
PictureBox[] pics1 = new PictureBox[7];
PictureBox[] pics2 = new PictureBox[7];
//PictureBox pb = new PictureBox();
public void Launch()
{
Random r = new Random();
int i = 0;
//int x = 0;
//int x = 50;
tb.Location = new Point(205,20);
f.Controls.Add(tb);
Clock = new Timer();
Clock.Interval = 800;
Clock.Tick += new EventHandler(Clock_Tick);
Clock1 = new Timer();
Clock1.Interval = 800;
Clock1.Tick += new EventHandler(Clock1_Tick);
Clock2 = new Timer();
Clock2.Interval = 800;
Clock2.Tick += new EventHandler(Clock2_Tick);
for (i = 0; i < pics.Length; i++)
{
pics[i] = new PictureBox();
pics[0].Image = Image.FromFile(i + ".jpg");
pics[i].SetBounds(50, 100, 100, 100);
//x += 150;
f.Controls.Add(pics[i]);
}
for (i = 0; i < pics1.Length; i++)
{
pics1[i] = new PictureBox();
pics1[i].Image = Image.FromFile(i + ".jpg");
pics1[i].SetBounds(200, 100, 100, 100);
//x += 50;
f.Controls.Add(pics1[i]);
}
for (i = 0; i < pics2.Length; i++)
{
pics2[i] = new PictureBox();
pics2[i].Image = Image.FromFile(i + ".jpg");
pics2[i].SetBounds(350, 100, 100, 100);
//x += 50;
f.Controls.Add(pics2[i]);
}
f.SetBounds(10, 20, 500, 500);
// STOP
btn1.Location = new Point(50, 250);
btn1.Height = 40;
btn1.Width = 100;
f.Controls.Add(btn1);
btn1.Text = "STOP";
this.btn1.Click += new EventHandler(this.MyButtonClick);
// STOP
btn2.Location = new Point(200, 250);
btn2.Height = 40;
btn2.Width = 100;
btn2.Text = "STOP";
f.Controls.Add(btn2);
this.btn2.Click += new EventHandler(this.MyButtonClick);
// STOP
btn3.Location = new Point(350, 250);
btn3.Height = 40;
btn3.Width = 100;
btn3.Text = "STOP";
f.Controls.Add(btn3);
this.btn3.Click += new EventHandler(this.MyButtonClick);
// START
btn4.Location = new Point(200, 370);
btn4.Height = 40;
btn4.Width = 100;
btn4.Text = "START";
f.Controls.Add(btn4);
this.btn4.Click += new EventHandler(btn4_Click);
f.ShowDialog();
}
public void Clock_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[0] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics[0].Image = Image.FromFile(nNum[0] + ".jpg");
pics[1].Image = Image.FromFile(nNum[0] + ".jpg");
pics[2].Image = Image.FromFile(nNum[0] + ".jpg");
pics[3].Image = Image.FromFile(nNum[0] + ".jpg");
pics[4].Image = Image.FromFile(nNum[0] + ".jpg");
pics[5].Image = Image.FromFile(nNum[0] + ".jpg");
pics[6].Image = Image.FromFile(nNum[0] + ".jpg");
}
}
public void Clock1_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[1] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics1[0].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[1].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[2].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[3].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[4].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[5].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[6].Image = Image.FromFile(nNum[1] + ".jpg");
}
}
public void Clock2_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
nNum[2] = r.Next(0, 6);
for (int i = 0; i < pics.Length; i++)
{
pics2[0].Image = Image.FromFile(nNum[2] + ".jpg");
pics1[1].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[2].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[3].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[4].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[5].Image = Image.FromFile(nNum[1] + ".jpg");
pics1[6].Image = Image.FromFile(nNum[1] + ".jpg");
}
}
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
}
public void btn4_Click(object sender, EventArgs e)
{
Clock.Start();
Clock1.Start();
Clock2.Start();
btn4.Enabled = false;
}
public void Finish()
{
if (nNum[0] == nNum[1] && nNum[0] == nNum[2])
{
this.tb.Text = "Congratulations!";
}
}
}
}
You can check if all timers already stopped, like this:
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
if(!Clock.Enabled && !Clock1.Enabled && !Clock2.Enabled)
btn4.Enabled = true;
}
Try this for method MyButtonClick:
public void MyButtonClick(object sender, EventArgs e)
{
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
Finish();
if (!Clock.Enabled && !Clock1.Enabled && !Clock2.Enabled) btn4.Enabled = true;
}
You could disable each button as they are clicked (which would make sense) and then after they are all disabled, enable the Start button.
if (sender == btn1)
{
Clock.Stop();
btn1.Enabled = false; // disable the buttons after they are clicked.
}
if (sender == btn2)
{
Clock1.Stop();
btn2.Enabled = false;
}
if (sender == btn3)
{
Clock2.Stop();
btn3.Enabled = false;
}
// If all buttons are disabled then enable the Start button.
if (!btn1.Enabled && !btn2.Enabled && !btn3.Enabled)
{
btn4.Enabled = true;
}
Finish();

How to stop shuffle of images using timers in C#

I'm creating a simple slot machine. One button for the start, and three stop buttons on each tile of a Picturebox. My problem is every time I click each of the stop buttons, the Picturebox won't stop.
I need help in which, if I click the stop button on a corresponding Picturebox, it will stop and the two will continue to shuffle pictures. Then if I clicked the other stop button, another will stop and so on.
Here's what I have for now:
namespace SlotMachine
{
class SlotMac
{
private Form f;
Button btn1 = new Button(); // First stop
Button btn2 = new Button(); // Second stop
Button btn3 = new Button(); // Third stop
Button btn4 = new Button(); // Start
Timer Clock; // Tick
Timer Clock1; // Tick
Timer Clock2; // Tick
Int32 tick = 0;
public SlotMac()
{
f = new Form();
f.Text = "Slot Machine";
//f.Size = new Size(800, 700);
f.FormBorderStyle = FormBorderStyle.FixedSingle;
}
PictureBox[] pics = new PictureBox[7];
PictureBox pb = new PictureBox();
public void Launch()
{
int i = 0;
Clock = new Timer();
Clock.Interval = 1000;
Clock.Tick += new EventHandler(Clock_Tick);
Clock1 = new Timer();
Clock1.Interval = 1000;
Clock1.Tick += new EventHandler(Clock1_Tick);
Clock2 = new Timer();
Clock2.Interval = 1000;
Clock2.Tick += new EventHandler(Clock2_Tick);
int x = 50;
for (i = 0; i < pics.Length; i++)
{
pics[i] = new PictureBox();
pics[i].Image = Image.FromFile(i+".jpg");
pics[i].SetBounds(x, 100, 100, 100);
x += 150;
f.Controls.Add(pics[i]);
}
f.SetBounds(10, 20, 500, 500);
// STOP
btn1.Location = new Point(50, 250);
btn1.Height = 40;
btn1.Width = 100;
f.Controls.Add(btn1);
btn1.Text = "STOP";
this.btn1.Click += new EventHandler(this.MyButtonClick);
// STOP
btn2.Location = new Point(200, 250);
btn2.Height = 40;
btn2.Width = 100;
btn2.Text = "STOP";
f.Controls.Add(btn2);
this.btn2.Click += new EventHandler(this.MyButtonClick);
// STOP
btn3.Location = new Point(350, 250);
btn3.Height = 40;
btn3.Width = 100;
btn3.Text = "STOP";
f.Controls.Add(btn3);
this.btn3.Click += new EventHandler(this.MyButtonClick);
// START
btn4.Location = new Point(200, 370);
btn4.Height = 40;
btn4.Width = 100;
btn4.Text = "START";
f.Controls.Add(btn4);
this.btn4.Click += new EventHandler(btn4_Click);
f.ShowDialog();
}
public void Stop_Click(object sender, EventArgs e)
{
}
public void Clock_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
}
public void Clock1_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
}
public void Clock2_Tick(object sender, EventArgs e)
{
tick++;
Random r = new Random();
pics[0].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[1].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[2].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[3].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[4].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[5].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
pics[6].Image = Image.FromFile(r.Next(0, 6) + ".jpg");
}
public void MyButtonClick(object sender, EventArgs e)
{
// I am having troubles in this part
if (sender == btn1)
{
Clock.Stop();
}
if (sender == btn2)
{
Clock1.Stop();
}
if (sender == btn3)
{
Clock2.Stop();
}
}
public void btn4_Click(object sender, EventArgs e)
{
Clock.Start();
Clock1.Start();
Clock2.Start();
}
}
}
Each ticker sets all of the seven images in pics. It won't help to stop one of them if the others still update all of the pictures.
I think you need to create a separate PictureBox arrays for each clock. Otherwise, the PictureBox for what is getting shown will continually reflect that changes made by the other two timers.
Basically, if they are all based on the same array, then when the other clocks change the array, it doesn't matter if the first clock changes it or not; it still gets changed.

Categories

Resources