How to make two pictures not visible after a certain time - c#

I'm making a memotest and I need to press two different images and they need to keep visible for 3-5 seconds. I've tried Thread.Sleep(5000) but it doesn't show me the second one. What should I do?
The only way I've founded to see for some seconds the images was by putting a MessageBox but that isn't the idea and I don't know other way to do it.
if (pic != null && pic.Name == fondos[i].Name)
{
if (CantClick == 0)
{
ParejaActual = listRandom[i].pareja;
CantClick = 1;
primerI = i;
picAnterior = pic;
imgAnterior = img;
pic.Visible = false;
}
else if (CantClick == 1)
{
pic.Visible = false;
if (ParejaActual == listRandom[i].pareja)
{
SoundPlayer simpleSound = (new SoundPlayer(Configuracion.RootFolder + "aplau.wav"));
simpleSound.Play();
Ganando++;
label3.Text = Ganando.ToString();
//MessageBox.Show("Si");
//NO SE DESTAPA LA SEGUNDA.
//Thread.Sleep(5000);
CantClick = 0;
img.Visible = false;
imgAnterior.Visible = false;
Application.DoEvents();
}
else
{
(new SoundPlayer(Configuracion.RootFolder + "sad.wav")).Play();
MessageBox.Show("No");
Mal++;
CantClick = 0;
label4.Text = Mal.ToString();
pic.Visible = true;
picAnterior.Visible = true;
}
}
}
Thank you!

Instead of using Thread.Sleep , use System.Timers class. After an interval, just hide one image and show the other. Tell me if you want any other help.

Related

Progress bar doesn't function well

I have an app that reads the smart cards and take a little bit long time
so I need to show a progress bar. However, I followed some tutorials it didn't work for me. The problem is,as seen in the picture, the progress bar remains at the same point even after filling the fields with the data.
These are my codes, as shown within them, I want the progress bar to be completed when data displayed equals to true, and then hide it along with the "Pleas Wait !!!" label.
public void DataDisplayed()
{
//bool displayedData = false;
int t;
progressBar1.Minimum = 0;
progressBar1.Maximum = 200;
if (IDNumbertxt.Text != "" || FullNametxt.Text != "")
{
displayedData = true;
}
else
{
displayedData = false;
}
if (displayedData == false)
{
for (t = 0; t <= 100; t++)
{
progressBar1.Value = t;
}
}
else if (displayedData == true)
{
progressBar1.Value = 200;
waitlbl.Hide();
progressBar1.Hide();
}
Pleas, anyone could help ?
Thanks in advance
Try This.
for (t = 0; t <= 200; t++)
{
progressBar1.Value = t;
}
because your for loop stop at 100 which is the middle value of your progress bar
UPDATE
if (displayedData == false)
{
for (t = 0; t <= 100; t++)
{
progressBar1.Value = t;
}
}
else
{
progressBar1.Value = 200;
waitlbl.Hide();
progressBar1.Hide();
}
if you are using boolean you don't need to use an else if statement because boolean have only two answer it's either True or False
Also because your progressBar1.Hide(); was inside your else condition the progress bar will hide almost instantly after the progress bar value change to 200

System out of memory exception mediaelement wpf

I have screensaver app this app playing video and photo work normally on two hours and i get System out of memory exception how to resolve this problem?
i set the timer for 10 secodns and call SlideShow() method..
private void SlideShow()
{
if (animating == false)
{
animating = true;
if (images.Count <= imgIndex)
imgIndex = 0;
index = images.Count > imgIndex ? imgIndex : 0;
imgIndex++;
fadeOUT.From = 1;
fadeOUT.To = 0;
fadeOUT.Duration = TimeSpan.FromSeconds(CrossFadeTime);
fadeOUT.Completed += new EventHandler(fadeIN_Completed);
fadeIN.From = 0;
fadeIN.To = 1;
fadeIN.Duration = TimeSpan.FromSeconds(CrossFadeTime);
fadeIN.Completed += new EventHandler(fadeIN_Completed);
if (images[index].Contains(".wmv") || images[index].Contains(".mkv") || images[index].Contains(".mp4"))
myMediaElement.Visibility = System.Windows.Visibility.Hidden;
else
{
if (myMediaElement.Visibility == System.Windows.Visibility.Hidden)
myMediaElement.Visibility = System.Windows.Visibility.Visible;
}
myMediaElement.UnloadedBehavior = MediaState.Manual;
myMediaElement.Stop();
myMediaElement.Source = null;
myMediaElement.Close();
myMediaElement.Source = new Uri(images[index], UriKind.RelativeOrAbsolute);
myMediaElement.Stretch = Stretch.Uniform;
myMediaElement.Volume = 0;
myMediaElement1.UnloadedBehavior = MediaState.Manual;
myMediaElement1.Stop();
myMediaElement1.Source = null;
myMediaElement1.Close();
System.Threading.Thread.Sleep(900);
myMediaElement1.Source = new Uri(images[images.Count > index + 1 ? index + 1 : 0], UriKind.RelativeOrAbsolute);
myMediaElement1.Stretch = Stretch.Uniform;
myMediaElement1.MediaOpened += myMediaElement1_MediaOpened;
myMediaElement1.MediaEnded += myMediaElement1_MediaEnded;
myMediaElement.BeginAnimation(Image.OpacityProperty, fadeOUT, HandoffBehavior.SnapshotAndReplace);
myMediaElement1.BeginAnimation(Image.OpacityProperty, fadeIN, HandoffBehavior.SnapshotAndReplace);
}
}
You have to reduce memory consumption of application as much as possible until exception is gone.
You can decrease the memory consumption by just unregister the event when the child window closes.
For example:
myMediaElement1.MediaEnded -= myMediaElement1_MediaEnded;
You can assign null to all MediaElement Properties once Begin Animation is completed and then start BeginAnimation of MediaElement1
myMediaElement.Source=null;
myMediaElement.UnloadedBehavior = null; (if possible)
.
.
This Link further elaborate Memory Leaks

Dart Scoring Game Using C# Visual Studio 2010 - Alternating player score after evey 3rd score

Just bought a dartboard and thought to create a little scoring application, so far i have set up the player names and individual buttons to start the game. three games - 301, 501 and 1001, these are the targets scores to get to zero by throwing three darts each in turn by two players.
I have included many buttons for the score of each dart from 20 down to 1, each for single, double, triple, bull, outer bull and a no score button. Once the games starts the first three button presses should be attributed to player one then the following three buttons pressed to allocate the corresponding score to player 2. The games ends with the winning player achieving the target score (or whittles it down to zero).
I could have a player selection button to do this but was after some tips on a way of coding the alternate pattern of scoring to be automatic.
Any help greatly appreciated. Thank you
namespace dbstats
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
btnPlay.Visible = false;
btnReset.Visible = false;
btnUndo.Visible = false;
gbDartBoard.Visible = false;
gbScoreBoard.Visible = false;
lbP1Select.Visible = false;;
lbP2Select.Visible = false;
cmbP1.Visible = false;
cmbP2.Visible = false;
string[] lines = File.ReadAllLines(#"playerStats.csv");
foreach (var line in lines)
{
string[] names = line.Split(',');
if (names[0] != "NAME")
{
cmbP1.Items.Add(names[0]);
cmbP2.Items.Add(names[0]);
}
}
}
private void tsm301_Click(object sender, EventArgs e)
{
lbP1Select.Visible = true;
cmbP1.Visible = true;
lbP2Select.Visible = true;
cmbP2.Visible = true;
btnPlay.Visible = true;
tbPlayer1.Text = "301";
tbPlayer2.Text = "301";
gamesToolStripMenuItem.Visible = false;
manageToolStripMenuItem.Visible = false;
}
private void btnPlay_Click(object sender, EventArgs e)
{
if (cmbP1.SelectedItem == cmbP2.SelectedItem || cmbP1.SelectedItem == null || cmbP2.SelectedItem == null)
{
MessageBox.Show("Make Sure:" + "\n\n"
+ "The Players are NOT the same." + "\n"
+ "&" + "\n"
+ "At least one selection is NOT left blank", "Choose Again!");
}
else
{
lbP1Select.Visible = false;
cmbP1.Visible = false;
lbP2Select.Visible = false;
cmbP2.Visible = false;
btnPlay.Visible = false;
lbPlayer1.Text = cmbP1.SelectedItem.ToString();
lbPlayer2.Text = cmbP2.SelectedItem.ToString();
btnReset.Visible = true;
btnUndo.Visible = true;
gbDartBoard.Visible = true;
gbScoreBoard.Visible = true;
lbPlayer1.Visible = true; ;
lbPlayer2.Visible = true;
}
}
}
Keep a turnsEntered member variable to whatever class is managing turns/scoring.
// Call after every time a new score is entered. Start at 0.
turnsEntered++;
if (turnsEntered % 3 == 0)
{
SwitchPlayer(); // However you keep track of current player - switch here
}
After the 3rd score is entered it will switch players...after 3 more it will switch again, etc...
You can later check turnsEntered to determine when the game is over. Set it back to zero when you reinitialize for a new game.

Dynamic GroupBoxes always align to top

I have this application where several groupboxes are in a vertical line. They can be enabled and disabled individually. I want to always align the groupboxes to the top, no matter if the ones normally being in between to is enabled or not.
Say we have three groupboxes (1, 2, 3). The middle one (2) gets disabled. Normally the two other boxes (1,3) locations wont be affected by this. But I want the last groupbox (3) to then take the place that the middle groupbox (2) would normally be in.
Any ideas how to accomplish this the most simple way?
This is what im doing at the moment which isnt good enough for scaling
if (isForgeIncluded == "True" | isForgeIncluded == "true")
{
forgeBox.Visible = true;
if (headerPic == "False" | headerPic == "false")
{
PictureBox1.Visible = false;
forgeBox.Location = new Point(6, 5);
pathBox.Location = new Point(6, 112);
}
else
{
}
}
else
{
forgeBox.Visible = false;
if (headerPic == "False" | headerPic == "false")
{
PictureBox1.Visible = false;
pathBox.Location = new Point(6, 5);
}
else
{
pathBox.Location = new Point(6, 168);
}
}
set groupbox2 location(x,y) to groupbox3 location(x,y) while your groupbox2 disables - might be from a specific event.
I managed to figure out a solution to this, which is "more dynamic" but not 100% dynamic, as i will have to go add new GroupBoxes to an array manually. However, this does work, and takes care of the picturebox in the beginning
int nextBoxLocation = 0;
int boxHeightAdd = 0;
int spacing = 6;
if (headerPic == "True" | headerPic == "true")
{
PictureBox1.Visible = true;
nextBoxLocation = PictureBox1.Height + spacing;
}
if (isForgeIncluded == "True" | isForgeIncluded == "true")
{
forgeBox.Visible = true;
}
GroupBox[] boxes = {forgeBox , pathBox , typeBox};
foreach(GroupBox box in boxes)
{
if (box.Visible == true)
{
box.Location = new Point(6, nextBoxLocation);
boxHeightAdd = box.Height;
}
if (box.Name == "pathBox" && (minecraftPathVar == Environment.ExpandEnvironmentVariables("C:\\Users\\" + Environment.UserName + "\\AppData\\Roaming\\.minecraft")))
{
defRadioPath.Visible = false;
minecraftRadioPath.Visible = false;
box.Height = 75;
boxHeightAdd = 75;
}
nextBoxLocation += boxHeightAdd + spacing;
boxHeightAdd = 0;
}

Can´t check if array values are true from another class in main class

I am programming a basic writing game in C#. The game will randomly show picture in a picturebox. The pictures is stored in an array in a different class than the main class.
The code to the class looks like this :
public class bildelisteDyr
{
public static Bitmap bildeListe (int index)
{
Bitmap[] bildeListe = new Bitmap[21];
bildeListe[0] = Properties.Resources.ål;
bildeListe[1] = Properties.Resources.ant;
bildeListe[2] = Properties.Resources.bird;
bildeListe[3] = Properties.Resources.bear;
bildeListe[4] = Properties.Resources.butterfly;
bildeListe[5] = Properties.Resources.cat;
bildeListe[6] = Properties.Resources.chicken;
bildeListe[7] = Properties.Resources.dog;
bildeListe[8] = Properties.Resources.elephant;
bildeListe[9] = Properties.Resources.fish;
bildeListe[10] = Properties.Resources.goat;
bildeListe[11] = Properties.Resources.horse;
bildeListe[12] = Properties.Resources.ladybug;
bildeListe[13] = Properties.Resources.lion;
bildeListe[14] = Properties.Resources.moose;
bildeListe[15] = Properties.Resources.polarbear;
bildeListe[16] = Properties.Resources.reke;
bildeListe[17] = Properties.Resources.sheep;
bildeListe[18] = Properties.Resources.snake;
bildeListe[19] = Properties.Resources.spider;
bildeListe[20] = Properties.Resources.turtle;
return bildeListe[index];
}
}
When calling on the values in the array to show picture randomly in the picturebox it all works well. This is done like this :
pictureBox1.Image = bildelisteDyr.bildeListe(r.Next(0, 20));
But I have three times where I need the code to check the value of the picturebox to do a something. I have one play sound button, one button that give a label a text and one button to check given answer from a textbox. None of them seems to work. Here are some code :
Give text to label :
if (pictureBox1.Image == bildelisteDyr.bildeListe(0))
{
svarPåOppgave.Text = "ÅL";
}
else if (pictureBox1.Image == bildelisteDyr.bildeListe(1))
{
svarPåOppgave.Text = "MAUR";
}
// etc.
Play sound button :
if (pictureBox1.Image == bildelisteDyr.bildeListe(0))
{
SoundPlayer player = new SoundPlayer();
player = new SoundPlayer("lyd/dyr/ål.wav");
player.PlaySync();
}
else if (pictureBox1.Image == bildelisteDyr.bildeListe(1))
{
SoundPlayer player = new SoundPlayer();
player = new SoundPlayer("lyd/dyr/enmaur.wav");
player.PlaySync();
}
// etc.
Checking if correct answer is given :
if (pictureBox1.Image == bildelisteDyr.bildeListe(0))
{
if (textBox1.Text.Trim().ToLower() == "ål")
{
riktigLyd.Play();
poengInt += 1;
textBox1.Text = "";
pictureBox1.Image = bildelisteDyr.bildeListe(tilfeldigBildet);
tekstTilLabel();
svarPåOppgave.Visible = false;
}
else
{
feilLyd.Play();
poengInt -= 1;
textBox1.Text = "";
}
String poengString = poengInt.ToString();
label1.Text = poengString;
textBox1.Select();
}
else if (pictureBox1.Image == bildelisteDyr.bildeListe(1))
{
if (textBox1.Text.Trim().ToLower() == "maur")
{
riktigLyd.Play();
poengInt += 1;
textBox1.Text = "";
pictureBox1.Image = bildelisteDyr.bildeListe(tilfeldigBildet);
tekstTilLabel();
svarPåOppgave.Visible = false;
}
else
{
feilLyd.Play();
poengInt -= 1;
textBox1.Text = "";
}
String poengString = poengInt.ToString();
label1.Text = poengString;
} // etc.
I would guess there was something wrong with the if statements like
if (textBox1.Text.Trim().ToLower() == "ål")
But I can´t seem to understand what?
To sum it up, when I debug the program I get the random picture from the other class. But when I press the buttons on the program nothing happens. No sound, no text to label and no checking of answer.
There's some unusual architectural choices here, but the specific problem you're facing is that you're re-creating the Bitmaps every time, and comparisons are performed by reference, not by value.
Change your bildelisteDyr class as follows:
public class bildelisteDyr
{
static Bitmap[] bildeListeInternal;
static bildelisteDyr() {
bildeListeInternal = new Bitmap[21];
bildeListeInternal[0] = Properties.Resources.ål;
//...
bildeListeInternal[20] = Properties.Resources.turtle;
}
public static Bitmap bildeListe (int index) {
return bildeListeInternal[index];
}
}
Some more resources on the conceptual problem:
== Operator
Value vs Reference Types

Categories