How to randomize groupbox

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 1242 Views - Last Post: 05 February 2013 - 09:48 AM Rate Topic: ***-- 1 Votes

#1 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

How to randomize groupbox

Posted 05 February 2013 - 02:01 AM

So far I made 5 different group box with different image using the toolbox, group boxes has similar location and size.
Can anyone help me how to view it randomly when I run my program? c#. Thankyou guys
Is This A Good Question/Topic? 0
  • +

Replies To: How to randomize groupbox

#2 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: How to randomize groupbox

Posted 05 February 2013 - 02:48 AM

I honestly don't fully understand what you mean. Can you show what you've tried and maybe give more detail on what you want to happen?
Was This Post Helpful? 0
  • +
  • -

#3 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: How to randomize groupbox

Posted 05 February 2013 - 03:45 AM

I'm making a maze game in c#, I draw mazes inside the group box, I have 5 different mazes that means I also have 5 groupbox, what I want to happen is to randomize the mazes.
Was This Post Helpful? 0
  • +
  • -

#4 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: How to randomize groupbox

Posted 05 February 2013 - 04:10 AM

I want that groupboxes shows randomly
Was This Post Helpful? 0
  • +
  • -

#5 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: How to randomize groupbox

Posted 05 February 2013 - 04:17 AM

OK... here's one way of doing it.
1) Have all the groupboxes in a collection (probably List<GroupBox>).
2) When the form loads, create an instance of the Random class.
3) Then, use the Random object's Next method, getting an int between 0 and 4 (because of zero-based indexing).
4) Then, show the groupbox in the List at the index that was randomised.
Was This Post Helpful? 1
  • +
  • -

#6 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: How to randomize groupbox

Posted 05 February 2013 - 04:50 AM

how can I add my groupboxes in the list? or should I say listBox?
what's wrong with this?
private void Game1_Load(object sender, EventArgs e)
        {
            Random r = new Random();
            GroupBox groupBox1a = new GroupBox[0];
            GroupBox groupBox1b = new GroupBox[1];
           
            for(int r =0; r <=1; i++)
                {
                    List[r].groupBox = GroupBox[r.Next(0,1)];
                }
        }



Was This Post Helpful? 0
  • +
  • -

#7 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: How to randomize groupbox

Posted 05 February 2013 - 04:58 AM

Well, you're making 2 new GroupBox variables in lines 4 and 5, and you seem to be attemtping to convert each into an array. You're also trying to access and indexed point of an uninstantiated variable named List using a Random object as the indexer and not an int.

Try like this:
Random r = new Random();
List<GroupBox> groupBoxList = new List<GroupBox>;
GroupBox groupBox = new GroupBox();
//Do whatever you need to set the groupbox type here.
groupBoxList.Add(groupBox);
//Repeat the last step for each groupbox you want to add.

GroupBox randomisedGroupBox = groupBoxList[r.next(groupBoxList.Length - 1)];


This post has been edited by MrShoes: 05 February 2013 - 04:59 AM

Was This Post Helpful? 0
  • +
  • -

#8 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: How to randomize groupbox

Posted 05 February 2013 - 04:58 AM

I'm stuck here
List<GroupBox> groupBoxList = new List<GroupBox>;
        private void Game1_Load(object sender, EventArgs e)
        {
            Random r = new Random();

        }


Was This Post Helpful? 0
  • +
  • -

#9 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: How to randomize groupbox

Posted 05 February 2013 - 05:25 AM

OK. So you said you had 5 groupboxes. How are you creating those?
Was This Post Helpful? 0
  • +
  • -

#10 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: How to randomize groupbox

Posted 05 February 2013 - 05:31 AM

creating it using the toolbox.

Oh. what I did wasn't right. I don't want to create new groupBoxes, I want to use the groupbox that I made or draw. I'm not really good in c#. :(
Was This Post Helpful? 0
  • +
  • -

#11 MrShoes  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 312
  • View blog
  • Posts: 488
  • Joined: 13-June 12

Re: How to randomize groupbox

Posted 05 February 2013 - 05:47 AM

Right, so each of those group boxes has a name, right? So say they're called groupBox1, groupBox 2 etc. Then we do this:

private void Game1_Load(object sender, EventArgs e)
{
// Creates a List of the GroupBoxes.
List<GroupBox> groupBoxList = new List<GroupBox>();
// Adds your group boxes to the list.
groupBoxList.Add(groupBox1);
groupBoxList.Add(groupBox2);
groupBoxList.Add(groupBox3);
groupBoxList.Add(groupBox4);
groupBoxList.Add(groupBox5);

// Hide them all first.
foreach(GroupBox box in groupBoxList)
{
box.Hide();
}

// Select a random one from the List.
Random r = new Random();
GroupBox visibleBox = groupBoxList[r.Next(groupBoxList.Count - 1)];

// Show your random box.
visibleBox.Show();
}


Was This Post Helpful? 2
  • +
  • -

#12 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,262
  • Joined: 05-May 12

Re: How to randomize groupbox

Posted 05 February 2013 - 05:52 AM

tlhIn`toq will probably jump into this thread soon. Listen to his advice, and he writes what I'm about to say in a much better way. The major problem is that you started "coding" by doing drag & drop programming without actually doing a lot of planning in term of how the code is going to work.

So the ugly brute force approach based on what you currently have is to generate a random number, and based on the random number, mark one of the groupboxes as visible and all others as hidden. In pseudo code:
groupbox1.Visible = false;
groupbox2.Visible = false;
    :
groupbox5.Visible = false;

switch(randomNumber)
{
case 0: groupbox1.Visible = true; break;
case 1: groupbox2.Visible = true; break;
    :
case 4: groupbox5.Visible = true; break;
}



Yuck!

I highly recommend going the route suggested by MrShoes. Also stop and take time to plan out your program instead of designing at the keyboard.

This post has been edited by Skydiver: 05 February 2013 - 05:54 AM

Was This Post Helpful? 1
  • +
  • -

#13 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: How to randomize groupbox

Posted 05 February 2013 - 06:44 AM

thankyou. It really helps :)
Was This Post Helpful? 0
  • +
  • -

#14 Kirstine  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 37
  • Joined: 04-February 13

Re: How to randomize groupbox

Posted 05 February 2013 - 06:55 AM

']
Yes because coding through drag and drop is easier to make design.But coding directly without using drag an drop is interesting. Can you help me how to make a groupbox with labels inside, not using drag and drop? :)
Was This Post Helpful? 0
  • +
  • -

#15 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5566
  • View blog
  • Posts: 11,903
  • Joined: 02-June 10

Re: How to randomize groupbox

Posted 05 February 2013 - 08:13 AM

View PostKirstine, on 05 February 2013 - 04:45 AM, said:

I'm making a maze game in c#, I draw mazes inside the group box, I have 5 different mazes that means I also have 5 groupbox, what I want to happen is to randomize the mazes.



STOP. You really need to stop and redesign what you're doing. What you are describing is just silly, terrible program design. Do you think that other programs with 200 mazes have 200 copies of controls?

Oh - wait - I just saw that Skydiver has already started telling you this. I don't mean to pile on and make you feel like we are beating you up.

There are LOTS of tutorials out there for "Maze Generator". If you do a google search with the terms "C# Maze GEnerator" you should get a let of how-to articles. Read them. Study them. Dissect the code and learn from it.

But mostly, I would recommend you stop trying to design any programs from scratch until you have spent a LOT more time actually learning C#. You wouldn't try to be a building architect until you understand the different engineering concepts. Its the same with software. You need to understand the materials and tools you have, their limitations and so on before you can architect a program.


My standard beginner resources post - Updated JAN 2013


Plan your study route:
There are three routes people seem to take when learning programming.
  • Just start trying to create programs
  • Start taking apart other programs and try to figure out the language by reverse engineering
  • Follow a guided learning course (school or self-teaching books)


For the life of me I can't figure out why people try 1 & 2. I strongly suggest taking the guided learning approach. Those book authors go in a certain order for a reason: They know what they're doing and they know the best order to learn the materials.

  • First learn the language by working 2-5 "Learn C# in 30 days" type books cover to cover.
  • Do a dozen on-line tutorial projects where you build what you're told to build, the way you are told to build it WITH AN EXPLANATION OF WHY so you can learn.
  • Learn to plan before you type.
  • THEN you start designing software with a purpose.



I don't learn from reading books: I learn by doing.
Spoiler


Newbie/Rookie said:

I have little/no programming experience but I need to write a program by Friday that does XYZ.
Spoiler



Resources, references and suggestions for new programmers.
Spoiler

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2