Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Topics

Recent Status Updates

View All Updates

Photo
- - - - -

Connecting a button to picturebox and putting up random images in the pictureboxes


  • Please log in to reply
4 replies to this topic

#1 rubbadrools

rubbadrools

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 19 January 2011 - 11:28 AM

First, I just want to say that I am a complete newb so don't be angry for my ignorance please :) Second, I am here because I want to learn and I am hoping that someone will have the patience to teach me :P Anyways, here goes!

I want to create a form with one button and one/or multiple pictureboxes that are connected to eachother. The way I would like this to go would be :

(for single picturebox)
Press the button -> random picture from a folder would appear in the picturebox

(for multiple pictureboxes)
Press the button -> random picture will occur in these pictureboxes but the pictures shown have no chance of repeating in another picturebox.

I suck at explaining but imagine if you will a deck of cards being dealt to let's say 4 players (then you would have 4 pictureboxes), and when that one card (lets say ace of spades?) is dealt to one player, it cannot be dealt to another player until the round ends.

TBH I don't even know if this is possbile to do in C# but im here to ask and to learn if it is and if it's VERY hard to do?

Thanks in advance ;)
  • 0

#2 bytecoder

bytecoder

    CC Lurker

  • Just Joined
  • Pip
  • 3 posts

Posted 22 January 2011 - 12:40 AM

Ok so here goes a fast explanation that I hope will get you started, use google to continue and keep asking here if needed.

1) Create a button and a picture box.
2) Double click the button and you'll be redirected to the "click" event of the button (now we're going to write what happens when the button is clicked)
3) The code to execute when the button is clicked is :
pictureBox1.Image = Image.FromFile(r.Next(5).ToString() + ".jpg");
4) As you can see we're using r.Next (gives us a random number, based on Random class) so we'll have to create this variable before using it :
Random r = new Random();
5) Full code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        Random r = new Random();

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pictureBox1.Image = Image.FromFile(r.Next(5).ToString() + ".jpg");
        }
    }
}

What does it do :
When pushing the button, you generate a random number from 0-5, then load the picture with this number as its name to the picture box. For example we generate "3" so we load 3.jpg to the picture box.
The picture must be at the same folder as your project (.exe) otherwise you have to write the FULL path to the folder where the pictures are, before the r.Next so it'll be like :
string path = @"C:\";
            pictureBox1.Image = Image.FromFile(path + r.Next(5).ToString() + ".jpg");

  • 1

#3 rubbadrools

rubbadrools

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 28 January 2011 - 06:21 AM

Hello and thank you for answering with such percision :)
I have however encountered my next issue!
This is my code atm!

/*
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
public partial class Form1 : Form
{
Random rand = new Random();
List<int> RemainNums;
int ARandomNumber;


public Form1()
{
InitializeComponent();
pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
RemainNums = new List<int>();
RemainNums.AddRange(new int[] { 0, 1, 2, 3, 4, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51 });
}



private void button1_Click(object sender, EventArgs e)
{

int index = rand.Next(0, RemainNums.Count - 1);
ARandomNumber = RemainNums[index];
RemainNums.RemoveAt(index);

string path = @"\\Fs\Homedir\sys09gma\Desktop\Kortlek\Kortlek\";
pictureBox1.Image = Image.FromFile(path + ARandomNumber + ".png");
pictureBox2.Image = Image.FromFile(path + ARandomNumber + ".png");
pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");
pictureBox4.Image = Image.FromFile(path + ARandomNumber + ".png");
Console.WriteLine(ARandomNumber);

}

private void pictureBox2_Click(object sender, EventArgs e)
{

}

private void pictureBox3_Click(object sender, EventArgs e)
{

}

private void pictureBox4_Click(object sender, EventArgs e)
{

}

private void pictureBox1_Click(object sender, EventArgs e)
{

}
}
}
*/

As you can see I have evovled the idea into a "highest cardgame" where 4 players (making this the 4 pictureboxes) each get a random card and highest card win (haven't come to the scoring part yet :P).
But as you can see it's 4 pictureboxes that unfortunatley show the same card in each picturebox. i've gotten some code off some other forum to remove the factor of showing the same card twice , but the problem now is that instead of showing 4 random cards in the picturebox I show the same card in all 4 pictureboxes. Im guessing I have to change the ->
/*

string path = @"\\Fs\Homedir\sys09gma\Desktop\Kortlek\Kortlek\";
pictureBox1.Image = Image.FromFile(path + ARandomNumber + ".png");
pictureBox2.Image = Image.FromFile(path + ARandomNumber + ".png");
pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");
pictureBox4.Image = Image.FromFile(path + ARandomNumber + ".png");
Console.WriteLine(ARandomNumber);
*/

part somehow but I can't really figure out how.
My next issue or rather question is if it is possible to create a code that delays each picturebox of showing... so it feels like the real deal. In case you didn't understand (cuz of my sucky explanation) :

Press the button --> shows random card in the picture --> 1.3sec --> next picturebox is filled with a random card --> 1.3sec --> next picturebox is filled with a random card --> 1.3sec --> Last card is shown in the last picturebox.
Furthermore .... Is it possible to have different delay timers? as 1.3sec on first and second then have 1.5 on the third and 2.0sec just to build some excitement on the last card??

Thanks in advance for the help!
  • 0

#4 CommittedC0der

CommittedC0der

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1065 posts
  • Programming Language:C#
  • Learning:C#, JavaScript, PL/SQL

Posted 29 January 2011 - 06:08 PM

First off please use code tags like this: [CODE*]Code here[/CODE*] just remove the '*' symbol. ;)

And I think this code should work. It may not be the best solution, but for now I gtg.
private void button1_Click(object sender, EventArgs e)
        {

            int index = rand.Next(0, RemainNums.Count - 1);
            ARandomNumber = RemainNums[index];
            RemainNums.RemoveAt(index);

            string path = @"\\Fs\Homedir\sys09gma\Desktop\Kortlek\Kortlek  \";
            pictureBox1.Image = Image.FromFile(path + ARandomNumber + ".png");
 index = rand.Next(0, RemainNums.Count - 1);
             ARandomNumber = RemainNums[index];
             RemainNums.RemoveAt(index);
            pictureBox2.Image = Image.FromFile(path + ARandomNumber + ".png");
 index = rand.Next(0, RemainNums.Count - 1);
             ARandomNumber = RemainNums[index];
             RemainNums.RemoveAt(index);
            pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");
 index = rand.Next(0, RemainNums.Count - 1);
             ARandomNumber = RemainNums[index];
             RemainNums.RemoveAt(index);
            pictureBox4.Image = Image.FromFile(path + ARandomNumber + ".png");
            Console.WriteLine(ARandomNumber);

        }
Also check out: Using a Timer in C# - Stack Overflow
I think the 3rd post from BFree may help you.
Good Luck ~ Committed.
  • 1
There IS a war going on for your mind.
Nothing of eternal significance happens besides prayer.

#5 rubbadrools

rubbadrools

    CC Newcomer

  • Member
  • PipPip
  • 15 posts

Posted 31 January 2011 - 05:05 AM

Hi and Thanks for the quick answer!

I tried the code u wrote and it worked like a charm! Even the link you gave me worked

Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        Random rand = new Random();
        List<int> RemainNums;
        int ARandomNumber;
        Timer timer = new Timer();
        private int counter = 0; 
        

        public Form1()
        {
            InitializeComponent(); 
            
            timer.Interval = 350;
            timer.Tick += new EventHandler(timer_Tick); 

            
            pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            pictureBox2.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            pictureBox3.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            pictureBox4.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
            PopulateCards();
        }
            
        public void timer_Tick(object sender, EventArgs e)    
        {
           counter++;
           if (counter == 1) 
            //or whatever amount of time you want it to be invisible            
        {
            pictureBox1.Visible = true;
            
        }
        if (counter == 2)
        {
            pictureBox2.Visible = true;
         
        }
        if (counter == 3)
        {
            pictureBox3.Visible = true;
        }
        if (counter == 4)
        {
            pictureBox4.Visible = true;
            timer.Stop();
            counter = 0;
            
        }
    } 


        private void PopulateCards()
            {
            
            RemainNums = new List<int>();
            RemainNums.AddRange(new int[] { 0, 1, 2, 3, 4, 5, 6, 7,
                8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21,
                22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51  });
        }

       

        private void button1_Click(object sender, EventArgs e)
        {
            timer.Start();
            pictureBox1.Visible = false; 
            pictureBox2.Visible = false; 
            pictureBox3.Visible = false; 
            pictureBox4.Visible = false; 

            int index = rand.Next(0, RemainNums.Count - 1);
            ARandomNumber = RemainNums[index];
            RemainNums.RemoveAt(index);

            string path = @"\\Fs\Homedir\sys09gma\Desktop\Kortlek\Kortlek\";
            pictureBox1.Image = Image.FromFile(path + ARandomNumber + ".png");
            index = rand.Next(0, RemainNums.Count - 1);
            ARandomNumber = RemainNums[index];
            RemainNums.RemoveAt(index);
            pictureBox2.Image = Image.FromFile(path + ARandomNumber + ".png");
            index = rand.Next(0, RemainNums.Count - 1);
            ARandomNumber = RemainNums[index];
            RemainNums.RemoveAt(index);
            pictureBox3.Image = Image.FromFile(path + ARandomNumber + ".png");
            index = rand.Next(0, RemainNums.Count - 1);
            ARandomNumber = RemainNums[index];
            RemainNums.RemoveAt(index);
            pictureBox4.Image = Image.FromFile(path + ARandomNumber + ".png");
            Console.WriteLine(ARandomNumber);
            if (RemainNums.Count <= 1){
                RemainNums.Clear();
                PopulateCards();
            }
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox4_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }
    }
}


This is how far i've gotten. Got the timer down and random card on the pictureboxes :) The next step I wanted to do was to connect a database to this game so that a Scoreboard will keep track of the points. And for this I figured that I need to apoint the cards each a value (figured the int numbs will do). But how do I connect a picturebox to a player from the database? Can't figure this step out :(
  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download