Why do we use this.load in c#? - c#

I'm working on a multimedia assignment and I found that the Dr is using this.load in the code so...
I don't understand why do we need this event handler as I think we can work without it.
I don't get why do I need such a ready made function and how it would be useful in multimedia programming or gaming programming.
This is my code :
namespace changecolors
{
public class Actor
{
public int X, Y, W, H;
public Color cl;
}
public partial class Form1 : Form
{
List<Actor> L = new List<Actor>();
int pos = -1;
public Form1()
{
this.WindowState = FormWindowState.Maximized;
this.Load += new EventHandler(Form1_Load);
this.KeyDown += new KeyEventHandler(Form1_KeyDown);
this.MouseDown += new MouseEventHandler(Form1_MouseDown);
}
void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
pos = -1;
for (int i = 0; i < L.Count; i++)
{
if (e.X >= L[i].X
&& e.X <= L[i].X + L[i].W
&& e.Y >=L[i].Y
&& e.Y <= L[i].Y + L[i].H)
{
pos = i;
}
}
}
else
{
if (pos > -1)
{
L[pos].Y += 10;
}
}
DrawScene();
}
void Form1_KeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Space:
DrawScene();
break;
}
}
void Form1_Load(object sender, EventArgs e)
{
int V = 100;
Color[] z = { Color.DarkGray, Color.Red, Color.Blue };
for (int i = 0; i < 3; i++)
{
Actor pnn = new Actor();
pnn.X = V;
pnn.Y = 200;
pnn.W = 70;
pnn.H = 170;
pnn.cl = z[i];
V += pnn.W;
L.Add(pnn);
}
}
void DrawScene()
{
Graphics g = this.CreateGraphics();
g.Clear(Color.MistyRose);
for (int i = 0; i < L.Count; i++)
{
SolidBrush br = new SolidBrush(L[i].cl);
g.FillRectangle(br,
L[i].X, L[i].Y,
L[i].W, L[i].H);
}
}
}
}

Related

Winforms KeyDown function does not work even though I added it to the events

Here is the function:
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
if (SpielField[ypos + 1][xpos] != '#')
{
SpielField[ypos].Replace('#', ' ');
ypos++;
string temp = new string(SpielField[ypos].ToCharArray(), 0, xpos);
temp += '#';
temp += SpielField[ypos].Substring(xpos + 1);
SpielField[ypos] = temp;
}
}
}
and here is how I added it to the Events in the constructor:
this.KeyDown += Form1_KeyDown;
I am implementing winforms functions to ConsoleApp but that makes no difference I believe.
Here is the complete code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.IO;
using System.Drawing;
namespace MazeProject
{
class Form1 : Form
{
string[] SpielField;
int column;
int line;
int xpos;
int ypos;
public Form1(int c,int l,string[] input)
{
this.Width = 800;
this.Height = 800;
this.column = c;
this.line = l;
this.KeyPreview = true;
SpielField = new string[column];
SpielField = input;
for (int i = 0; i < column; i++) {
for(int j = 0; j < line; j++)
{
if(SpielField[i][j] == '#')
{
xpos = j;
ypos = i;
break;
}
}
}
this.Paint += Form1_Paint;
this.KeyDown += Form1_KeyDown;
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
Console.WriteLine(column);
for (int i = 0; i < column; i++)
{
Font f = new Font(FontFamily.GenericMonospace, 20, FontStyle.Regular);
Brush b = new SolidBrush(Color.Black);
e.Graphics.DrawString(SpielField[i], f, b , 10, 650-(i+2)*40);
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.W)
{
if (SpielField[ypos + 1][xpos] != '#')
{
SpielField[ypos].Replace('#', ' ');
ypos++;
string temp = new string(SpielField[ypos].ToCharArray(), 0, xpos);
temp += '#';
temp += SpielField[ypos].Substring(xpos + 1);
SpielField[ypos] = temp;
}
}
}
}
class Program
{
[STAThread]
static void Main(string[] args)
{
string filePath = System.IO.Directory.GetCurrentDirectory() + "\\maze.txt";
string[] input = File.ReadAllLines(filePath);
int l = Convert.ToInt32(input[0]);
int c = Convert.ToInt32(input[1]);
string[] data = new string[c];
for(int i = 0; i < c; i++)
{
data[i] = input[i + 2];
}
Form1 game = new Form1(c, l, data);
Console.WriteLine(c);
Console.WriteLine(l);
Application.EnableVisualStyles();
Application.Run(game);
Console.ReadLine();
}
}
}
If you want to intercept the key events from the form itself you need to set the
Form1.KeyPreview = true;
otherwise the keys will be intercepted by the current focused control
Microsoft Docs on KeyPreview

Puzzle with PictureBoxes bug

I have an image that is chopped into 4 pieces (2x2). I draw them in a random order and if the order of the small pics are correct write out "You win".
If you click on an image then it should be swap with the another next to it.
If you click the left button first change position with the third (right button 2-4). What is the problem with the code?
[![enter image description here][1]][1]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace SimplePuzzle
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonVéletlen_Click(object sender, EventArgs e)
{
Mix();
pictureBox1.Image = Image.FromFile("img\\1.png");
pictureBox2.Image = Image.FromFile("img\\2.png");
pictureBox3.Image = Image.FromFile("img\\3.png");
pictureBox4.Image = Image.FromFile("img\\4.png");
}
int[] arr = new int[4];
void Mix()
{
Random rnd = new Random();
int n = 4;
// Fill array 1-4
for (int i = 0; i < n; i++)
{
arr[i] = i + 1;
}
// Rnd array
for (int j = 0; j < n; j++)
{
int one = rnd.Next(n);
int another = rnd.Next(n);
int temp = arr[one];
arr[one] = arr[another];
arr[another] = temp;
}
for (int i = 0; i < 4; i++)
{
Console.WriteLine(arr[i].ToString());
}
// PictureBox1
if (arr[0]==0)
{
pictureBox1.Left = 0;
pictureBox1.Top = 0;
}
if (arr[0] == 1)
{
pictureBox1.Left = 150;
pictureBox1.Top = 0;
}
if (arr[0] == 2)
{
pictureBox1.Left = 0;
pictureBox1.Top = 150;
}
if (arr[0] == 3)
{
pictureBox1.Left = 150;
pictureBox1.Top = 150;
}
// PictureBox2
if (arr[1] == 0)
{
pictureBox2.Left = 0;
pictureBox2.Top = 0;
}
if (arr[1] == 1)
{
pictureBox2.Left = 150;
pictureBox2.Top = 0;
}
if (arr[1] == 2)
{
pictureBox2.Left = 0;
pictureBox2.Top = 150;
}
if (arr[1] == 3)
{
pictureBox2.Left = 150;
pictureBox2.Top = 150;
}
// PictureBox3
if (arr[2] == 0)
{
pictureBox3.Left = 0;
pictureBox3.Top = 0;
}
if (arr[2] == 1)
{
pictureBox3.Left = 150;
pictureBox3.Top = 0;
}
if (arr[2] == 2)
{
pictureBox3.Left = 0;
pictureBox3.Top = 150;
}
if (arr[2] == 3)
{
pictureBox3.Left = 150;
pictureBox3.Top = 150;
}
// PictureBox4
if (arr[3] == 0)
{
pictureBox4.Left = 0;
pictureBox4.Top = 0;
}
if (arr[3] == 1)
{
pictureBox4.Left = 150;
pictureBox4.Top = 0;
}
if (arr[3] == 2)
{
pictureBox4.Left = 0;
pictureBox4.Top = 150;
}
if (arr[3] == 3)
{
pictureBox4.Left = 150;
pictureBox4.Top = 150;
}
}
void CheckWin()
{
if (pictureBox1.Left==0 && pictureBox1.Top == 0 &&
pictureBox2.Left == 0 && pictureBox2.Top == 0 &&
pictureBox3.Left == 0 && pictureBox3.Top == 0 &&
pictureBox4.Left == 0 && pictureBox4.Top == 0)
{
MessageBox.Show("You have won!");
}
}
private void pictureBox1_Click(object sender, EventArgs e)
{
pictureBoxKöztes.Image = pictureBox1.Image;
pictureBox1.Image = pictureBox2.Image;
pictureBox2.Image = pictureBoxKöztes.Image;
CheckWin();
}
private void pictureBox2_Click(object sender, EventArgs e)
{
pictureBoxKöztes.Image = pictureBox2.Image;
pictureBox2.Image = pictureBox1.Image;
pictureBox1.Image = pictureBoxKöztes.Image;
CheckWin();
}
private void pictureBox3_Click_1(object sender, EventArgs e)
{
pictureBoxKöztes.Image = pictureBox3.Image;
pictureBox3.Image = pictureBox4.Image;
pictureBox4.Image = pictureBoxKöztes.Image;
CheckWin();
}
private void pictureBox4_Click(object sender, EventArgs e)
{
pictureBoxKöztes.Image = pictureBox4.Image;
pictureBox4.Image = pictureBox3.Image;
pictureBox3.Image = pictureBoxKöztes.Image;
CheckWin();
}
private void buttonFirstCol_Click(object sender, EventArgs e)
{
pictureBoxKöztes.Image = pictureBox1.Image;
pictureBox1.Image = pictureBox3.Image;
pictureBox3.Image = pictureBoxKöztes.Image;
CheckWin();
}
private void buttonSecondCol_Click(object sender, EventArgs e)
{
pictureBoxKöztes.Image = pictureBox2.Image;
pictureBox2.Image = pictureBox4.Image;
pictureBox4.Image = pictureBoxKöztes.Image;
CheckWin();
}
}
}
[1]: https://i.stack.imgur.com/G9dMy.png
The CheckWin() method is the problem.
All you are doing is checking that the left and top of the picture boxes == 0.
But there's no code moving them around, so it can't ever equal true.

Difficulty on passing a variable along with event

I need to pass the value for maxColumns to a method that will be used repeatedly by other items. How can I go about passing maxColumns in this scenario?
public partial class ValResults : Form
{
public ValResults()
{
InitializeComponent();
this.Table1Requirements();
}
private void Table1Requirements()
{
int maxColumns = 6;
this.table1LayoutPanelPrime.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPaint);
}
void tableLayoutPaint(object sender, TableLayoutCellPaintEventArgs e)
{
for (int i = 0; i < maxColumns; i++)
{
if (e.Row == 0 && e.Column == i)
{
Graphics g = e.Graphics;
Rectangle r = e.CellBounds;
g.FillRectangle(Brushes.LightGray, r);
}
}
}
}
If its in the same class, just define the variable at the class level.
private int _maxColumns;
public ValResults()
{
InitializeComponent();
this.Table1Requirements();
SetColumnCount();
}
private void SetColumnCount(){
_maxColumns= 6;
}
void tableLayoutPaint(object sender, TableLayoutCellPaintEventArgs e)
{
for (int i = 0; i < _maxColumns; i++)
{
if (e.Row == 0 && e.Column == i)
{
Graphics g = e.Graphics;
Rectangle r = e.CellBounds;
g.FillRectangle(Brushes.LightGray, r);
}
}
}
you can essentially pre build a function using lambdas...
private void Table1Requirements()
{
int maxColumns = 6;
this.table1LayoutPanelPrime.CellPaint += new TableLayoutCellPaintEventHandler(tableLayoutPaint(maxColumns));
}
Action<object,TableLayoutCellPaintEventArgs> tableLayoutPaint(int columns)
return (sender, e) =>
{
for (int i = 0; i < columns; i++)
{
if (e.Row == 0 && e.Column == i)
{
Graphics g = e.Graphics;
Rectangle r = e.CellBounds;
g.FillRectangle(Brushes.LightGray, r);
}
}
}
that way you can generate different event handles with different max column settings

How to add multiple canvases to a picturebox?

Let me explain my problem:
I have a picturebox and this should be used to draw several elements of a listbox.
I gave the picturebox a canvas. by left and rightclick u can put up a rectangle which should preview the element u want to draw on the canvas of the picturebox.
Now this is my problem:
this rectangle should just be a preview and should be erased every time i click on a another position of the picturebox and put up a new rectangle. it should just be drawn on the canvas when i click on the inputbutton.
So how can i preview the chosen elements in the rectangle made by left and rightclick?
I hope my question is clealy enough : /
This is what i did so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Interaktive_systeme_sta
{
public partial class Bildeditor : Form
{
void drawRect()
{
pbxBild.Refresh();
using (Graphics g = this.pbxBild.CreateGraphics())
{
Pen blackPen = new Pen(Color.Black, 1);
blackPen.DashPattern = new float[] {2,2};
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawRectangle(blackPen, X, Y, B, H);
g.Dispose();
}
}
public Bildeditor()
{
InitializeComponent();
this.MinimumSize = new Size(630,420);
this.MaximumSize = new Size(630, 420);
int canvasWidth = pbxBild.Width;
int canvasHeight = pbxBild.Height;
nupBreite.Maximum = canvasWidth;
nupHoehe.Maximum = canvasHeight;
nupBreite.Minimum = 0;
nupHoehe.Minimum = 0;
nupBreite.Value = canvasWidth;
nupHoehe.Value = canvasHeight;
nupX.Maximum = canvasWidth;
nupY.Maximum = canvasHeight;
nupX.Minimum = 0;
nupY.Minimum = 0;
nupX.Value = 0;
nupY.Value = 0;
drawRect();
}
private void btnZuruecksetzten_Click(object sender, EventArgs e)
{
int canvasWidth = pbxBild.Width;
int canvasHeight = pbxBild.Height;
nupX.Value = 0;
nupY.Value = 0;
nupBreite.Value = canvasWidth-1;
nupHoehe.Value = canvasHeight-1;
}
private void numericUpDown2_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void pbxBild_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e)
{
Point mouseDownLocation = new Point(e.X, e.Y);
switch (e.Button)
{
case MouseButtons.Left:
if (e.X < 0)
{
nupX.Value = 0;
}
else
{
nupX.Value = e.X;
}
if (e.Y < 0)
{
nupY.Value = 0;
}
else
{
nupY.Value = e.Y;
}
break;
case MouseButtons.Right:
if (e.X - nupX.Value < 1)
{
nupBreite.Value = 1;
}
else
{
nupBreite.Value = e.X - nupX.Value;
}
if (e.Y - nupY.Value < 1)
{
nupHoehe.Value = 1;
}
else
{
nupHoehe.Value = e.Y - nupY.Value;
}
break;
}
drawRect();
}
private void nupY_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void nupBreite_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void nupHoehe_ValueChanged(object sender, EventArgs e)
{
drawRect();
}
private void btnEinfuegen_Click(object sender, EventArgs e)
{
if (lbBildelemente.SelectedIndex == 0)
{
zeichneBild();
}
else if (lbBildelemente.SelectedIndex == 1)
{
zeichneLine();
}
else if (lbBildelemente.SelectedIndex == 2)
{
zeichneRect();
}
else if (lbBildelemente.SelectedIndex == 3)
{
zeichneText();
}
else if (lbBildelemente.SelectedIndex == 4)
{
zeichneKreis();
}
}
private void lbBildelemente_SelectedIndexChanged(object sender, EventArgs e)
{
if (lbBildelemente.SelectedIndex == 0)
{
tbFarbe.Enabled = false;
btnFarbe.Enabled = false;
nupPen.Enabled = false;
tbBild.Enabled = true;
btnBild.Enabled = true;
tbText.Enabled = false;
tbSchrift.Enabled = false;
btnSchrift.Enabled = false;
}
else if (lbBildelemente.SelectedIndex == 1 || lbBildelemente.SelectedIndex == 2 || lbBildelemente.SelectedIndex == 4)
{
tbFarbe.Enabled = true;
btnFarbe.Enabled = true;
nupPen.Enabled = true;
tbBild.Enabled = false;
btnBild.Enabled = false;
tbText.Enabled = false;
tbSchrift.Enabled = false;
btnSchrift.Enabled = false;
}
else if (lbBildelemente.SelectedIndex == 3)
{
tbFarbe.Enabled = true;
btnFarbe.Enabled = true;
nupPen.Enabled = true;
tbBild.Enabled = false;
btnBild.Enabled = false;
tbText.Enabled = true;
tbSchrift.Enabled = true;
btnSchrift.Enabled = true;
}
}
void zeichneBild()
{
Graphics g = pbxBild.CreateGraphics();
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
Bitmap bitmap = new Bitmap(tbBild.Text);
g.DrawImage(bitmap, X, Y, B, H);
}
// Als zusatzfunktion kann man die Pinseldicke auswählen
void zeichneRect()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawRectangle(Pen, X, Y, B, H);
}
//zusatzfunktion
void zeichneKreis()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawEllipse(Pen, X, Y, B, H);
}
void zeichneLine()
{
Graphics g = pbxBild.CreateGraphics();
Pen Pen = new Pen(tbFarbe.BackColor, Convert.ToInt32(nupPen.Value));
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
int B = Convert.ToInt32(nupBreite.Value);
int H = Convert.ToInt32(nupHoehe.Value);
g.DrawLine(Pen, X, Y, X+B, Y+H);
}
void zeichneText()
{
Graphics g = pbxBild.CreateGraphics();
Brush brush = new SolidBrush(tbFarbe.BackColor);
int X = Convert.ToInt32(nupX.Value);
int Y = Convert.ToInt32(nupY.Value);
g.DrawString(tbText.Text, fontDialog1.Font, brush, X, Y);
}
private void btnFarbe_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
tbFarbe.BackColor = colorDialog1.Color;
}
}
private void btnSchrift_Click(object sender, EventArgs e)
{
// Show the dialog.
DialogResult result = fontDialog1.ShowDialog();
// See if OK was pressed.
if (result == DialogResult.OK)
{
// Get Font.
Font font = fontDialog1.Font;
// Set TextBox properties.
this.tbSchrift.Text = string.Format(font.Name +"; "+ font.Size +"pt");
}
}
private void btnBild_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
+ "EMF Enhanced Metafile Format (*.emf)|*.emf|"
+ "Graphics Interchange Format (*.gif)|*.gif|"
+ "Bitmap (*.bmp)|*.bmp|"
+ "W3C Portable Network Graphics (*.png)|*.png";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
tbBild.Text = openFileDialog.FileName;
}
}
private void bildbereichLöschenToolStripMenuItem_Click(object sender, EventArgs e)
{
pbxBild.Refresh();
}
private void bildLadenToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Joint Photographic Experts Group (*.jpg)|*.jpg|"
+ "EMF Enhanced Metafile Format (*.emf)|*.emf|"
+ "Graphics Interchange Format (*.gif)|*.gif|"
+ "Bitmap (*.bmp)|*.bmp|"
+ "W3C Portable Network Graphics (*.png)|*.png";
if (openFileDialog.ShowDialog(this) == DialogResult.OK)
{
Graphics g = pbxBild.CreateGraphics();
int X = 0;
int Y = 0;
int B = Convert.ToInt32(pbxBild.Width);
int H = Convert.ToInt32(pbxBild.Height);
Bitmap bitmap = new Bitmap(openFileDialog.FileName);
g.DrawImage(bitmap, X, Y, B, H);
}
}
}
}
I think this is what you need
private void Form1_Load(object sender, EventArgs e)
{
listBox1.SelectedIndexChanged += listBox1_SelectedIndexChanged;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem == "Circle")
{
pictureBox1.Image = circle;
}
}

How do i trigger the mouse middle button in enter event of the mouse?

I have this code:
public void globalPbsMouseEnterEvent(object sender, System.EventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
MessageBox.Show("hi");
}
PictureBox p = sender as PictureBox;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
if (file_array != null)
{
if (file_array.Length > 0)
{
for (int i = 0; i < file_array.Length; i++)
{
if (p == pbs[i])
pb.Animate(file_array[i]);
}
}
}
pb.Visible = true;
pb.BringToFront();
leave = true;
}
But e.Button not exist Button not exist as property for e
This is the code in the constructor of form1 where i make the instance for the global enter event. The variable pbs is is array of AnimatedPictureBoxs:
pbs = new AnimatedPictureBox.AnimatedPictureBoxs[8];
progressbars = new ProgressBar[8];
for (int i = 0; i < pbs.Length; i++)
{
progressbars[i] = new ProgressBar();
progressbars[i].Size = new Size(100, 10);
progressbars[i].Margin = new Padding(0, 0, 0, 70);
progressbars[i].Dock = DockStyle.Top;
pbs[i] = new AnimatedPictureBox.AnimatedPictureBoxs();
pbs[i].MouseEnter += globalPbsMouseEnterEvent;
pbs[i].MouseLeave += globalPbsMouseLeaveEvent;
pbs[i].Tag = "PB" + i.ToString();
pbs[i].Size = new Size(100, 100);
pbs[i].Margin = new Padding(0, 0, 0, 60);
pbs[i].Dock = DockStyle.Top;
pbs[i].SizeMode = PictureBoxSizeMode.StretchImage;
Panel p = i < 4 ? panel1 : panel2;
p.Controls.Add(pbs[i]);
p.Controls.Add(progressbars[i]);
pbs[i].BringToFront();
progressbars[i].BringToFront();
}
This is the code in the class AnimatedPictureBox:
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DannyGeneral;
namespace WeatherMaps
{
class AnimatedPictureBox
{
//Use this custom PictureBox for convenience
public class AnimatedPictureBoxs : PictureBox
{
public static bool images;
List<string> imageFilenames;
Timer t = new Timer();
public AnimatedPictureBoxs()
{
images = false;
AnimationInterval = 10; //It's up to you, the smaller, the faster.
t.Tick += Tick_Animate;
}
public int AnimationInterval
{
get { return t.Interval; }
set { t.Interval = value; }
}
public void Animate(List<string> imageFilenames)
{
this.imageFilenames = imageFilenames;
t.Start();
}
public void StopAnimate()
{
t.Stop();
i = 0;
}
int i;
private void Tick_Animate(object sender, EventArgs e)
{
if (images == true)
{
imageFilenames = null;
}
if (imageFilenames == null)
{
return;
}
else
{
try
{
if (i >= imageFilenames.Count)
{
i = 0;
}
else
{
Load(imageFilenames[i]);
i = (i + 1) % imageFilenames.Count;
}
}
catch (Exception err)
{
Logger.Write(err.ToString());
}
}
}
}
}
}
What i want to do is when the user click on the middle mouse button it will pause the animation and when he click again on the middle button of the mouse the animation will continue.
But in the globalenter event e dosent have the Button property.
In your code, specifically here
public void globalPbsMouseEnterEvent(object sender, System.EventArgs e)
{
if (e.Button == MouseButtons.Middle)
{
MessageBox.Show("hi");
}
....................
}
You can't do it on MouseEnter actually need to use MouseDown, MouseWheel [for example] to have MouseEventArgs
public void globalPb_MouseDown(object sender, MouseEventArgs e)
{
if (e.MiddleButton = MouseButtonState.Pressed)
{
MessageBox.Show("hi");
}
....................
}

Categories