I Can't find any solution how to reset the index when last file has been opened.
I have a mediaelement that reads images from a listbox and when last image has been played I want it to restart. Like a repeatable Mediaelement playlist.
Please help, I really need help with this one.
Dictionary<string, string> Listbox1Dict = new Dictionary<string, string>();
public static List<string> images = new List<string> { ".JPG", ".JPE", ".BMP", ".GIF", ".PNG" }; // Bildtyper som stöds
public static List<string> movies = new List<string> { ".WMV", ".WAV", ".SWF", ".MP4", ".MPG", ".AVI" }; // Filmtyper som stöds
List<string> paths = new List<string>();
DispatcherTimer dispatcherTimer = new DispatcherTimer();
DispatcherTimer NextImageTimer = new DispatcherTimer();
int x = 20; //Ställa in antal sekunder per bild
private int currentSongIndex = -1;
private void dispatcherTimer_Tick(object sender, EventArgs e)
{
ShowNextImage();
}
private void dispatch()
{
dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
dispatcherTimer.Interval = new TimeSpan(0, 0, 0, x);
}
private void ShowNextImage()
{
if (currentSongIndex == -1)
{
currentSongIndex = Listbox1.SelectedIndex;
}
currentSongIndex++;
var selected = Listbox1.Items[currentSongIndex];
string s = selected.ToString();
if (Listbox1Dict.ContainsKey(s))
{
if (images.Contains(System.IO.Path.GetExtension(s).ToUpperInvariant()))
{
if (currentSongIndex < Listbox1.Items.Count)
{
mediaElement1.Visibility = Visibility.Visible;
SearchBtn.Visibility = Visibility.Hidden;
Listbox1.Visibility = Visibility.Hidden;
FileNameTextBox.Visibility = Visibility.Hidden;
mediaElement1.Source = new Uri(Listbox1Dict[s]);
mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.Background = new SolidColorBrush(Colors.Black);
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
mediaElement1.StretchDirection = StretchDirection.Both;
mediaElement1.Stretch = Stretch.Fill;
dispatcherTimer.Start();
}
}
else if (movies.Contains(System.IO.Path.GetExtension(s).ToUpperInvariant()))
{
if (currentSongIndex < Listbox1.Items.Count)
{
dispatcherTimer.Stop();
mediaElement1.Visibility = Visibility.Visible;
Listbox1.Visibility = Visibility.Hidden;
FileNameTextBox.Visibility = Visibility.Hidden;
mediaElement1.Source = new Uri(Listbox1Dict[s]);
mediaElement1.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
mediaElement1.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
this.Background = new SolidColorBrush(Colors.Black);
this.WindowStyle = WindowStyle.None;
this.WindowState = WindowState.Maximized;
mediaElement1.StretchDirection = StretchDirection.Both;
mediaElement1.Stretch = Stretch.Fill;
}
}
}
}
Cant you do something like:
private void ShowNextImage()
{
if (currentSongIndex == -1)
{
currentSongIndex = Listbox1.SelectedIndex;
}
if (currentSongIndex == ListBox1.Items.Count)
{
currentSongIndex = 0;
}
currentSongIndex++;
....
}
Related
I'm going to preface this by saying I'm sure the question has already been asked but I've tried all the suggestions and nothing seems to work for me. It just keeps returning a blank message box. If the answer were just a nice snippet of code I can add to my answer that would be fantastic.
private string Text1;
public void Button_Click(object sender, RoutedEventArgs e)
{
TextBox txtbx = new TextBox();
txtbx.Height = 50;
txtbx.Width = 200;
txtbx.Margin = new Thickness(771, 282, 0, 0);
txtbx.Background = new SolidColorBrush(Colors.White);
txtbx.Foreground = new SolidColorBrush(Colors.Black);
Text1 = txtbx.Text;
if (EditChecked == true)
{
LayoutRoot.Children.Add(txtbx);
Button Save = new Button();
Save.Height = 25;
Save.Width = 50;
Save.Content = "Save";
Save.Margin = new Thickness(771, 382, 0, 0);
LayoutRoot.Children.Add(Save);
txtbx.Text = Text1;
Save.Click += delegate
{
txtbx.Visibility = Visibility.Collapsed;
Save.Visibility = Visibility.Collapsed;
};
}
else if (ViewChecked == true)
{
MessageBox.Show(Text1);
}
}
Edit: Thanks to mjwills I moved the "Text1 = txtbx.Text;" line so this is what ended up working fyi:
private string Text1;
public void Button_Click(object sender, RoutedEventArgs e)
{
TextBox txtbx = new TextBox();
txtbx.Height = 50;
txtbx.Width = 200;
txtbx.Margin = new Thickness(771, 282, 0, 0);
txtbx.Background = new SolidColorBrush(Colors.White);
txtbx.Foreground = new SolidColorBrush(Colors.Black);
if (EditChecked == true)
{
LayoutRoot.Children.Add(txtbx);
Button Save = new Button();
Save.Height = 25;
Save.Width = 50;
Save.Content = "Save";
Save.Margin = new Thickness(771, 382, 0, 0);
LayoutRoot.Children.Add(Save);
txtbx.Text = Text1;
Save.Click += delegate
{
txtbx.Visibility = Visibility.Collapsed;
Save.Visibility = Visibility.Collapsed;
Text1 = txtbx.Text;
};
}
You declare:
private string Text1;
However you only assign it on the chance:
EditChecked = True
And you only display the message if
EditChecked = False
So, you need to assign Text1 on if the EditChecked = False. Perhaps on the declaration:
private string Text1 = "My default message";
I want to start the form in some case in the tray icon.
So in the constructor I did:
public Form1()
{
InitializeComponent();
settingsFileDirectory = Path.GetDirectoryName(Application.LocalUserAppDataPath) +
settingsFileDirectory;
if (!Directory.Exists(settingsFileDirectory))
{
Directory.CreateDirectory(settingsFileDirectory);
}
settingsFile = Path.Combine(settingsFileDirectory,settingsFile);
if (!File.Exists(settingsFile))
{
File.Create(settingsFile);
}
string[] values = System.IO.File.ReadAllText(settingsFile).Split(',');
downloadonstart = Convert.ToBoolean(Convert.ToInt32(values[0]));
loadonstart = Convert.ToBoolean(Convert.ToInt32(values[1]));
startminimized = Convert.ToBoolean(Convert.ToInt32(values[2]));
displaynotifications = Convert.ToBoolean(Convert.ToInt32(values[3]));
lvnf = new ListViewNF();
lvnf.Location = new Point(250, 18);
lvnf.Size = new Size(474, 168);
lvnf.View = View.Details;
this.SuspendLayout();
lvnf.LabelEdit = true;
lvnf.Columns.Add("From", 100, HorizontalAlignment.Left);
lvnf.Columns.Add("Subject", 200);
lvnf.Columns.Add("Date", 300);
lvnf.Sorting = SortOrder.None;
lvnf.ColumnClick += lvnf_ColumnClick;
lvnf.Click += lvnf_Click;
this.Controls.Add(lvnf);
this.ResumeLayout(false);
label8.Visible = false;
pbt.Size = new Size(216, 10);
pbt.Location = new Point(8, 330);
pbt1.Size = new Size(396, 10);
pbt1.Location = new Point(250, 198);
this.Controls.Add(pbt1);
groupBox1.Controls.Add(pbt);
pbt.Text = "0%";
pbt1.Text = "0%";
//LoadFullMessages();
if (startminimized == true)
{
this.WindowState = FormWindowState.Minimized;
}
if (loadonstart == true)
{
backgroundWorker2.RunWorkerAsync();
}
if (downloadonstart == true)
{
backgroundWorker1.RunWorkerAsync();
}
}
Then inside the Form1_Resize event I did this:
private void Form1_Resize(object sender, EventArgs e)
{
if (this.WindowState == FormWindowState.Minimized)
{
this.Hide();
this.ShowInTaskbar = false;
}
}
What makes the exception is the line:
this.ShowInTaskbar = false;
An unhandled exception of type 'System.StackOverflowException' occurred in System.Windows.Forms.dll
Then I click on break and I see this:
What am I doing wrong?
What I need is to set the Position property on WPF's MediaElement control. But when I play the video, (either via Play() or through some kind of animation on Opacity) it is is not working at all. It is showing 00:00:00 time, but I would expect it to be set to 00:00:05.
I have hard-coded Position value and it is not working at all.
Just In case I am going to put all my code I have so u can see the whole animation logic.
Any clue?
public partial class CrossFadeTransition : UserControl
{
DoubleAnimation _doubleAnimationFrontPlayer = new DoubleAnimation();
DoubleAnimation _doubleAnimationBackPlayer = new DoubleAnimation();
Storyboard _sb1 = new Storyboard();
Storyboard _sb2 = new Storyboard();
public TimeSpan Duration = TimeSpan.FromSeconds(2);
public Dictionary<string, Position2D> PlayerPosition { set; get; }
public CrossFadeTransition()
{
InitializeComponent();
Player1.LoadedBehavior = MediaState.Manual;
Player1.UnloadedBehavior = MediaState.Stop;
Player2.LoadedBehavior = MediaState.Manual;
Player2.UnloadedBehavior = MediaState.Stop;
PlayerPosition = new Dictionary<string, Position2D>();
PlayerPosition.Add("Player1", Position2D.Front);
PlayerPosition.Add("Player2", Position2D.Back);
}
Position2D positionPlayer1;
Position2D positionPlayer2;
public void Stop()
{
if (Player1.IsEnabled)
Player1.Stop();
if (Player2.IsEnabled)
Player2.Stop();
}
public void Start(Uri uri, int? position)
{
try
{
positionPlayer1 = PlayerPosition["Player1"];
positionPlayer2 = PlayerPosition["Player2"];
if (positionPlayer1 == Library.Position2D.Back)
{
Player1.Source = uri;
if (Player1.IsEnabled)
Player1.Stop();
Player1.Position = TimeSpan.FromSeconds(5); // IT IS NOT WORKING !!!
Player1.Play();
}
if (positionPlayer2 == Library.Position2D.Back)
{
Player2.Source = uri;
if (Player2.IsEnabled)
Player2.Stop();
Player2.Position = TimeSpan.FromSeconds(5); // IT IS NOT WORKING !!!
Player2.Play();
}
_sb1.Children.Clear();
_sb2.Children.Clear();
if (positionPlayer1 == Position2D.Front)
{
_doubleAnimationFrontPlayer.From = 1;
_doubleAnimationFrontPlayer.To = 0;
_doubleAnimationFrontPlayer.Duration = new Duration(Duration);
PlayerPosition["Player1"] = Position2D.Back;
}
else if (positionPlayer1 == Position2D.Back)
{
_doubleAnimationFrontPlayer.From = 0;
_doubleAnimationFrontPlayer.To = 1;
_doubleAnimationFrontPlayer.Duration = new Duration(Duration);
PlayerPosition["Player1"] = Position2D.Front;
}
if (positionPlayer2 == Position2D.Front)
{
_doubleAnimationBackPlayer.From = 1;
_doubleAnimationBackPlayer.To = 0;
_doubleAnimationBackPlayer.Duration = new Duration(Duration);
PlayerPosition["Player2"] = Position2D.Back;
}
else if (positionPlayer2 == Position2D.Back)
{
_doubleAnimationBackPlayer.From = 0;
_doubleAnimationBackPlayer.To = 1;
_doubleAnimationBackPlayer.Duration = new Duration(Duration);
PlayerPosition["Player2"] = Position2D.Front;
}
_sb1.Children.Add(_doubleAnimationFrontPlayer);
Storyboard.SetTargetProperty(_doubleAnimationFrontPlayer, new PropertyPath("(Panel.Opacity)"));
Storyboard.SetTarget(_doubleAnimationFrontPlayer, Player1);
_sb1.Completed += _sb1_Completed;
_sb1.Begin();
//
_sb2.Children.Add(_doubleAnimationBackPlayer);
Storyboard.SetTargetProperty(_doubleAnimationBackPlayer, new PropertyPath("(Panel.Opacity)"));
Storyboard.SetTarget(_doubleAnimationBackPlayer, Player2);
_sb2.Completed += _sb2_Completed;
_sb2.Begin();
}
catch (Exception)
{
throw;
}
}
void _sb2_Completed(object sender, EventArgs e)
{
_sb2.Completed -= _sb2_Completed;
Debug.WriteLine("Player2 COMPLETED " + DateTime.Now.TimeOfDay);
if (positionPlayer2 == Position2D.Front)
{
Player2.Stop();
}
}
void _sb1_Completed(object sender, EventArgs e)
{
_sb1.Completed -= _sb1_Completed;
Debug.WriteLine("Player1 COMPLETED " + DateTime.Now.TimeOfDay);
if (positionPlayer1 == Position2D.Front)
{
Player1.Stop();
}
}
}
I have tried to do like
Player2.Play();
Player2.Pause();
Player2.Position = TimeSpan.FromSeconds(5); // IT IS NOT WORKING !!!
Player2.Play();
But no joy...
I found some solution. It is not completely ideal because sometimes it shows the first video frame, but at least it is working.
We need those events where we can apply new Position.
void Player2_MediaOpened(object sender, RoutedEventArgs e)
{
Player2.Position = new TimeSpan(0, 0, 7);
}
void Player1_MediaOpened(object sender, RoutedEventArgs e)
{
Player1.Position = new TimeSpan(0, 0, 7);
}
We have to close Mediaelement like this.
Player1.Stop();
Player1.Close();
Player1.Source = uri;
Player1.Play();
Have fun! (beer)
I apply some LinearGradientBrush animation to MediaElement and after this video is freezing.
I try to reset it via Player1.OpacityMask = null; but no joy.
By the way if I animate Opacity of the MediaElement then video shows good as usually.
Any clue what's up? How to fix the video freezing after applying LinearGradientBrush animation to MediaElement ?
Thank you!
/// <summary>
/// Interaction logic for WipeTransition.xaml
/// </summary>
public partial class WipeTransition
{
public WipeDirections Direction { set; get; }
public TimeSpan Duration = TimeSpan.FromSeconds(2);
public bool StopPrevPlayerOnAnimationStarts;
Random rdm = new Random(1);
bool isPlayer1;
public WipeTransition()
{
InitializeComponent();
try
{
Loaded += WipeTransition_Loaded;
SizeChanged += WipeTransition_SizeChanged;
Player1.CacheMode = new BitmapCache();
Player1.LoadedBehavior = MediaState.Manual;
Player2.CacheMode = new BitmapCache();
Player2.LoadedBehavior = MediaState.Manual;
Direction = WipeDirections.RandomDirection;
Player1.Width = ActualWidth;
Player2.Width = ActualWidth;
Player1.Height = ActualHeight;
Player2.Height = ActualHeight;
isPlayer1 = true;
}
catch (Exception)
{
}
}
void WipeTransition_SizeChanged(object sender, SizeChangedEventArgs e)
{
Player1.Width = ActualWidth;
Player2.Width = ActualWidth;
Player1.Height = ActualHeight;
Player2.Height = ActualHeight;
}
void WipeTransition_Loaded(object sender, RoutedEventArgs e)
{
Player1.Width = ActualWidth;
Player2.Width = ActualWidth;
Player1.Height = ActualHeight;
Player2.Height = ActualHeight;
}
public void Start(Uri uri)
{
try
{
Debug.WriteLine("************** START ANIMATION ***************************************");
Player1.Width = ActualWidth;
Player2.Width = ActualWidth;
Player1.Height = ActualHeight;
Player2.Height = ActualHeight;
// We start to Animate one of the Player is on BACK
if (isPlayer1)
{
if (StopPrevPlayerOnAnimationStarts)
Player2.Stop();
Canvas.SetZIndex(Player2, 49);
Canvas.SetZIndex(Player1, 50);
Player1.Source = uri;
Debug.WriteLine("Player1 - ANIMATE");
WipeAnimation(Player1);
}
else // Player2
{
if (StopPrevPlayerOnAnimationStarts)
Player1.Stop();
Canvas.SetZIndex(Player1, 49);
Canvas.SetZIndex(Player2, 50);
Player2.Source = uri;
Debug.WriteLine("Player2 - ANIMATE");
WipeAnimation(Player2);
}
}
catch (Exception)
{
}
}
public void WipeAnimation(FrameworkElement ObjectToAnimate)
{
try
{
LinearGradientBrush OpacityBrush = new LinearGradientBrush();
#region Setup Wipe Direction
switch (Direction)
{
case WipeDirections.RightToLeft:
OpacityBrush.StartPoint = new Point(1, 0);
OpacityBrush.EndPoint = new Point(0, 0);
break;
case WipeDirections.LeftToRight:
OpacityBrush.StartPoint = new Point(0, 0);
OpacityBrush.EndPoint = new Point(1, 0);
break;
case WipeDirections.BottomToTop:
OpacityBrush.StartPoint = new Point(0, 1);
OpacityBrush.EndPoint = new Point(0, 0);
break;
case WipeDirections.TopToBottom:
OpacityBrush.StartPoint = new Point(0, 0);
OpacityBrush.EndPoint = new Point(0, 1);
break;
case WipeDirections.RandomDirection:
#region
int randomValue = rdm.Next(0, 3);
if (randomValue == 0)
{
OpacityBrush.StartPoint = new Point(1, 0);
OpacityBrush.EndPoint = new Point(0, 0);
}
else if (randomValue == 1)
{
OpacityBrush.StartPoint = new Point(0, 0);
OpacityBrush.EndPoint = new Point(0, 1);
}
else if (randomValue == 2)
{
OpacityBrush.StartPoint = new Point(0, 0);
OpacityBrush.EndPoint = new Point(1, 0);
}
else if (randomValue == 3)
{
OpacityBrush.StartPoint = new Point(0, 1);
OpacityBrush.EndPoint = new Point(0, 0);
}
#endregion
break;
}
#endregion
GradientStop BlackStop = new GradientStop(Colors.Black, 0);
GradientStop TransparentStop = new GradientStop(Colors.Transparent, 0);
if (FindName("TransparentStop") != null)
UnregisterName("TransparentStop");
RegisterName("TransparentStop", TransparentStop);
if (FindName("BlackStop") != null)
UnregisterName("BlackStop");
this.RegisterName("BlackStop", BlackStop);
OpacityBrush.GradientStops.Add(BlackStop);
OpacityBrush.GradientStops.Add(TransparentStop);
ObjectToAnimate.OpacityMask = OpacityBrush;
Storyboard sb = new Storyboard();
DoubleAnimation DA = new DoubleAnimation() { From = 0, To = 1, Duration = Duration };
DoubleAnimation DA2 = new DoubleAnimation() { From = 0, To = 1, Duration = Duration };
sb.Children.Add(DA); sb.Children.Add(DA2);
Storyboard.SetTargetName(DA, "TransparentStop");
Storyboard.SetTargetName(DA2, "BlackStop");
Storyboard.SetTargetProperty(DA, new PropertyPath("Offset"));
Storyboard.SetTargetProperty(DA2, new PropertyPath("Offset"));
sb.Duration = Duration;
sb.Completed += sb_Completed;
sb.Begin(this);
if (isPlayer1)
{
Player1.Play();
}
else // Player2
{
Player2.Play();
}
}
catch (Exception)
{
}
}
void sb_Completed(object sender, EventArgs e)
{
if (isPlayer1)
{
Player1.OpacityMask = null;
isPlayer1 = false;
Player2.Stop();
Player2.Source = null;
}
else // Player2
{
Player2.OpacityMask = null;
isPlayer1 = true;
Player1.Stop();
Player1.Source = null;
}
Debug.WriteLine("************** END ANIMATION ***************************************");
}
public void Stop()
{
if (Player1.IsEnabled)
Player1.Stop();
if (Player2.IsEnabled)
Player2.Stop();
}
}
UPDATE:
The same problem happens after I apply shader effect. So video goes well before it and after this it is jumping/freezing. It seems that we have somehow to reset MedieElement or?
private void Button_Click_1(object sender, RoutedEventArgs e)
{
TransitionEffect[] effectGroup = transitionEffects[1];
TransitionEffect effect = effectGroup[1];
DoubleAnimation da = new DoubleAnimation(0.0, 1.0, new Duration(TimeSpan.FromSeconds(2.0)), FillBehavior.HoldEnd);
da.AccelerationRatio = 0.5;
da.DecelerationRatio = 0.5;
da.Completed += da_Completed;
effect.BeginAnimation(TransitionEffect.ProgressProperty, da);
VisualBrush vb = new VisualBrush(this.Player1);
vb.Viewbox = new Rect(0, 0, this.Player1.ActualWidth, this.Player1.ActualHeight);
vb.ViewboxUnits = BrushMappingMode.Absolute;
effect.OldImage = vb;
this.Player1.Effect = effect;
}
I need a help on this,
There are 3 picture boxes, red should grow its Width to left, green should grow its height to top blue's width to right
also if one reaches the top border / any text boxes it should give an error / stop execution
I need to add more picture boxes in future and if two of them collides it should give an error / stop execution. I have managed to code them to go up, however unable to get other functions working. Please some one help me with this.
OR
https://www.box.com/s/d0d302c6c266f52e0abf
Thank you.
RR
My code below,
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace ThreadwithmovingPicbxmoving
{
public partial class Form1 : Form
{
Thread t1;
Thread t2;
Thread t3;
delegate void CTMethod(int val);
delegate void CTFinish(string t);
Queue<string> order = new Queue<string>();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int r = int.Parse(textBox1.Text);
int y = int.Parse(textBox2.Text);
int g = int.Parse(textBox3.Text);
t1 = new Thread(new ParameterizedThreadStart(loopred));
t2 = new Thread(new ParameterizedThreadStart(loopyel));
t3 = new Thread(new ParameterizedThreadStart(loopGree));
t1.Start(r);
t2.Start(y);
t3.Start(g);
}
private void updateRed(int val)
{
pictureBox1.Height = val;
pictureBox1.Refresh();
}
private void updateyell(int val)
{
pictureBox2.Height = val;
pictureBox2.Refresh();
}
private void updategree(int val)
{
pictureBox3.Height = val;
pictureBox3.Refresh();
}
private void loopred(object o)
{
int c = (int)o;
CTMethod ctred = new CTMethod(updateRed);
if (c < 500)
{
for (int i = 0; i < c; i++)
{
this.Invoke(ctred, i);
Thread.Sleep(20);
}
}
else
{
MessageBox.Show("Enter a value less than 500 for Red Box!!!");
}
CTFinish CTFin = new CTFinish(Threadfinish);
this.Invoke(CTFin, "Red");
}
private void loopyel(object o)
{
int c = (int)o;
CTMethod ctyell = new CTMethod(updateyell);
if (c < 500)
{
for (int i = 0; i < c; i++)
{
this.Invoke(ctyell, i);
Thread.Sleep(20);
}
}
else
{
MessageBox.Show("Enter a valure less than 500 for Yellow Box!!!");
}
CTFinish CTFin = new CTFinish(Threadfinish);
this.Invoke(CTFin, "Yell");
}
private void loopGree(object o)
{
int c = (int)o;
CTMethod ctgree = new CTMethod(updategree);
if (c < 500)
{
for (int i = 0; i < c; i++)
{
this.Invoke(ctgree, i);
Thread.Sleep(20);
}
}
else
{
MessageBox.Show("Enter a valure less than 500 for Green Box!!!");
}
CTFinish CTfin = new CTFinish(Threadfinish);
this.Invoke(CTfin, "Green");
}
private void Threadfinish(string t)
{
order.Enqueue(t);
if (order.Count == 3)
{
MessageBox.Show("Threads finished in this order: \n" + "1." + order.Dequeue() + "\n" + "2." + order.Dequeue()
+ "\n" + "3." + order.Dequeue() + "\n", "finished");
}
}
}
}
Try It. Its All Fine with three PictureBoxes
Create New Widows Application with name help and then replace Form1.cs code with Following
Run It. It also includes values from TextBoxes
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using System.Collections.Generic;
namespace help
{
public partial class Form1 : Form
{
Thread t1;
Thread t2;
Thread t3;
delegate void CTMethod(int val);
delegate void CTFinish(string t);
Queue<string> order = new Queue<string>();
#region Variables of Designer File
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.PictureBox pictureBox3;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button1;
#endregion
public Form1()
{
#region Designer Code I have Cut and Pasted Here
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.pictureBox3 = new System.Windows.Forms.PictureBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox3)).BeginInit();
this.SuspendLayout();
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.pictureBox3);
this.Controls.Add(this.pictureBox2);
this.Controls.Add(this.pictureBox1);
//
// pictureBox1
//
this.pictureBox1.BackColor = System.Drawing.Color.Red;
this.pictureBox1.Location = new System.Drawing.Point(161, 268);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(100, 50);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// pictureBox2
//
this.pictureBox2.BackColor = System.Drawing.Color.Green;
this.pictureBox2.Location = new System.Drawing.Point(383, 268);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(100, 50);
this.pictureBox2.TabIndex = 1;
this.pictureBox2.TabStop = false;
//
// pictureBox3
//
this.pictureBox3.BackColor = System.Drawing.Color.Blue;
this.pictureBox3.Location = new System.Drawing.Point(605, 268);
this.pictureBox3.Name = "pictureBox3";
this.pictureBox3.Size = new System.Drawing.Size(100, 50);
this.pictureBox3.TabIndex = 2;
this.pictureBox3.TabStop = false;
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(161, 26);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 3;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(383, 25);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 20);
this.textBox2.TabIndex = 4;
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(605, 26);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(100, 20);
this.textBox3.TabIndex = 5;
//
// button1
//
this.button1.Location = new System.Drawing.Point(37, 23);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 6;
this.button1.Text = "Go";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
this.PerformLayout();
#endregion
InitializeComponent();
textBox1.Text = "490";
textBox2.Text = "490";
textBox3.Text = "490";
textBox1.Leave += new EventHandler(textBox1_Leave);
textBox2.Leave += new EventHandler(textBox2_Leave);
textBox3.Leave += new EventHandler(textBox3_Leave);
}
// To input Only Valid int values in TextBoxes
#region TextBoxes Input Validation
int t1Val = 490;
int t2Val = 490;
int t3Val = 490;
void textBox1_Leave(object sender, EventArgs e)
{
try
{
int t1Val = Convert.ToInt32(textBox1.Text);
}
catch
{
textBox1.Focus();
}
}
void textBox2_Leave(object sender, EventArgs e)
{
try
{
int t2Val = Convert.ToInt32(textBox2.Text);
}
catch
{
textBox2.Focus();
}
}
void textBox3_Leave(object sender, EventArgs e)
{
try
{
int t3Val = Convert.ToInt32(textBox3.Text);
}
catch
{
textBox3.Focus();
}
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
int r = t1Val;
int y = t2Val;
int g = t3Val;
t1 = new Thread(new ParameterizedThreadStart(loopRed));
t2 = new Thread(new ParameterizedThreadStart(loopGreen));
t3 = new Thread(new ParameterizedThreadStart(loopBlue));
// It will avoid proble if you exit app when threads are working
t1.IsBackground = true;
t2.IsBackground = true;
t3.IsBackground = true;
t1.Start(r);
t2.Start(y);
t3.Start(g);
}
private void updateRed(int val)
{
pictureBox1.Width++;
pictureBox1.Left--;
pictureBox1.Refresh();
}
private void updateGreen(int val)
{
pictureBox2.Height++;
pictureBox2.Top--;
pictureBox2.Refresh();
}
private void updateBlue(int val)
{
pictureBox3.Width++;
pictureBox3.Refresh();
}
private void loopRed(object o)
{
int c = (int)o;
CTMethod ctRed = new CTMethod(updateRed);
if (c < 500)
{
for (int i = 0; i < c; i++)
{
if (pictureBox1.Left > 0)
{
this.Invoke(ctRed, i);
Thread.Sleep(20);
}
}
}
else
{
MessageBox.Show("Enter a value less than 500 for Red Box!!!");
}
CTFinish CTFin = new CTFinish(Threadfinish);
this.Invoke(CTFin, "Red");
}
private void loopGreen(object o)
{
int c = (int)o;
CTMethod ctGreen = new CTMethod(updateGreen);
if (c < 500)
{
for (int i = 0; i < c; i++)
{
if (pictureBox2.Top > 0 && pictureBox2.Top != textBox2.Top + textBox2.Height)
{
this.Invoke(ctGreen, i);
Thread.Sleep(20);
}
else
break;
}
}
else
{
MessageBox.Show("Enter a valure less than 500 for Green Box!!!");
}
CTFinish CTFin = new CTFinish(Threadfinish);
this.Invoke(CTFin, "Green");
}
private void loopBlue(object o)
{
int c = (int)o;
CTMethod ctBlue = new CTMethod(updateBlue);
if (c < 500)
{
for (int i = 0; i < c; i++)
{
if (pictureBox3.Left + pictureBox3.Width < this.Width)
{
this.Invoke(ctBlue, i);
Thread.Sleep(20);
}
else
break;
}
}
else
{
MessageBox.Show("Enter a valure less than 500 for Blue Box!!!");
}
CTFinish CTfin = new CTFinish(Threadfinish);
this.Invoke(CTfin, "Blue");
}
private void Threadfinish(string t)
{
order.Enqueue(t);
if (order.Count == 3)
{
MessageBox.Show("Threads finished in this order: \n" + "1." + order.Dequeue() + "\n" + "2." + order.Dequeue()
+ "\n" + "3." + order.Dequeue() + "\n", "finished");
}
}
}
}