Click here to Skip to main content
13,549,226 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


4 bookmarked
Posted 10 May 2010
Licenced CPOL

How to create a Windows Forms Joke Jukebox in C++

, 29 May 2010
Rate this:
Please Sign up or sign in to vote.
Step by step on how to create a Windows Forms joke jukebox app in C++.


Designing apps in Microsoft Visual C++ 2005 Express Edition is pretty straightforward. Point this, click that, presto! place it on form. But seriously, click on the desired component (control) located at the left toolbar of Visual C++ 2005 Express Edition, and draw it at an appropriate location on the form.

Components on the form align to rectangular grids, giving your apps a symmetric look. This simple app demonstrates the ease of creating fun applications in Windows Forms.


Fast forwarding a bit, we've created a Windows form, placed listboxes, labels, common dialogs on the form. To have all these controls respond to mouse clicks, we have to put some code into it. So we double-click on any given control and place some code in that control's event method.

The listBox1_SelectedIndexChanged Event

The listBox1_SelectedIndexChanged event is called upon when the left button is pressed down while above listBox. Once the index has changed, this method is called upon and it represents the user changing from one text file containing jokes to another.

private: System::Void listBox1_SelectedIndexChanged(
         System::Object^  sender, System::EventArgs^  e)

    label1->Text="Jokefile= "+fileList[listBox1->SelectedIndex] ;
    ProcessFile( fileList[listBox1->SelectedIndex] );
    this->Text ="Joke Jukebox by TopCoder  [" + 

The Form1::GenerateRandomFiles() Method

The Form1::GenerateRandomFiles() method is called upon to load jokes from a text file to a managed string array.

void Form1::GenerateRandomFiles()
    Random^ random = gcnew Random( Environment::TickCount );

    for (int j=0;j<FileCount;j++)
        iFileList[j] =random->Next() % (FileCount) ;
        for(int k=0;k<j;k++)
            if (iFileList[j]==iFileList[k]) j--;

The listBox2_SelectedIndexChanged_1 Event

The listBox2_SelectedIndexChanged_1 event is called upon when the left button is pressed down while above listBox. Once the index has changed, this method is called upon and it represents the user changing from one line of joke to another.

private: System::Void listBox2_SelectedIndexChanged_1(
         System::Object^  sender, System::EventArgs^  e)

The Form1::displayRandomJokeLines() Method

The Form1::displayRandomJokeLines() method is called upon to display one line of joke from a string array on a label.

void Form1::displayRandomJokeLines()
    String^ sDebug;

    if (RanDom==1)
        label10->Text="Unread Lines "+(LineCount-doneLines) +"  ";
        label11->Text="Unread Files "+(FileCount-doneFiles);

        if (doneLines>=LineCount)
            ProcessFile( fileList[jokeFileCount] );
            label1->Text="Jokefile= "+fileList[jokeFileCount] ;
            label11->Text="Unread Files "+(FileCount-doneFiles);


            if (doneFiles>=FileCount)

And that is how easy it is to create fun applications in Windows Forms.

Thanks for reading.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

About me:
I attended programming college and I have a degree in three most famous and successful programming languages. C/C++, Visual Basic and Java. So i know i can code. And there is a diploma hanging on my wall to prove it.
I am a professional, I am paid tons of cash to teach or do software development. I am roughly 30 years old .

I hold lectures in programming. I have also coached students in C++, Java and Visual basic.

In my spare time i do enjoy developing computer games, and i am developing a rather simple flight simulator game
in the c++ programming language using the openGL graphics libray.

I've written hundreds of thousands of code syntax lines for small simple applications and games.

You may also be interested in...


Comments and Discussions

GeneralMy vote of 1 Pin
Panic2k311-May-10 6:35
memberPanic2k311-May-10 6:35 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02-2016 | 2.8.180515.1 | Last Updated 30 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid