Welcome to the CSIG, a Special Interest Group of the ACGNJ. The subject for this month is a discussion about graphics in Visual Studio. The object of this application is to represent graphically a pair of Dice and "roll" them to create random numbers. This can then be used in any game requiring the throw of a pair of dice.
The program demonstrates taking a graphic drawing of a "dot" and placing it in multiple locations on the screen. It uses a new .NET tool or function called TableLayoutPanel Class. Here's what the Microsoft says about it:
The TableLayoutPanel control arranges its contents in a grid. Because the layout is performed both at design time and run time, it can change dynamically as the application environment changes. This gives the controls in the panel the ability to proportionally resize, so it can respond to changes such as the parent control resizing or text length changing due to localization.
Since the TableLayoutPanel is a Container, it's contents can be enumerated and then scanned in a loop.
The other major items used in this application are the Random Class for generating a random throw and the Timer Class for sequencing the display.
There is a button called "Roll" to start the action but since it is the only button the action may also be started by pressing the SPACE BAR or the ENTER KEY. The dice sequence through random values and finally stop with a final value after about 10 seconds.
The Common Language Infrastructure (CLI) is an open specification developed by Microsoft that describes the executable code and runtime environment that form the core of the Microsoft .NET Framework. The specification defines an environment that allows multiple high-level languages to be used on different computer platforms without being rewritten for specific architectures.
Microsoft .Net Framework 2.0 |
C++ 7.0 |
.Net 2.0 |
CLI |
Common Language Infrastructure |
Managed |
........
private: System::Void Form1_ResizeEnd(System::Object^ sender, System::EventArgs^ e) { int width = this->ClientSize.Width; int height = 6 * (width / 13); this->ClientSize = System::Drawing::Size(width, height); } private: System::Void Roll_Click(System::Object^ sender, System::EventArgs^ e) { Loop = 0; timer1->Enabled = true; Roll->Enabled = false; } private: System::Void timer1_Tick(System::Object^ sender, System::EventArgs^ e) { System::Collections::IEnumerator ^c = tableLayoutPanel1->Controls->GetEnumerator(); PictureBox ^pic = nullptr; if (++Loop == 60) { timer1->Enabled = false; Roll->Enabled = true; Roll->Focus(); } Random ^prand = gcnew Random(); int i=0; if (Loop < 10) // Delay startup by displaying 12 { HitValue1 = HitValue2 = 6; } else { HitValue1 = prand->Next(6) + 1; // a number from 1 to 6 HitValue2 = prand->Next(6) + 1; // a number from 1 to 6 } Type ^mtype = nullptr; while(c->MoveNext()) { mtype = c->Current->GetType(); if (mtype->FullName == "System.Windows.Forms.Button") continue; pic = (PictureBox ^)c->Current; // // Test to check indexes... // //pic->Visible = false; Refresh(); //pic->Visible = true; Refresh(); Results: 17 16 15 14 13 12 //pic->Visible = false; Refresh(); 11 10 9 8 7 6 //pic->Visible = true; Refresh(); 5 4 3 2 1 0 //pic->Visible = false; Refresh(); //++i; //continue; // end of test if (i==17 ||i==16 ||i==15 ||i==11 ||i==10 ||i==9 || i==5 ||i==4 ||i==3) switch (HitValue1) { case 1: if (i==10) pic->Visible = true; else pic->Visible = false; break; case 2: if (i==5 || i==15) pic->Visible = true; else pic->Visible = false; break; case 3: if (i==5 || i==10 || i==15) pic->Visible = true; else pic->Visible = false; break; case 4: if (i==17 || i==15 || i==5 || i==3) pic->Visible = true; else pic->Visible = false; break; case 5: if (i==17 || i==15 || i==5 || i==3 || i==10) pic->Visible = true; else pic->Visible = false; break; case 6: if (i==17 || i==16 || i==15 || i==5 || i==4 || i==3) pic->Visible = true; else pic->Visible = false; break; } ++i; } Refresh(); }