Click here to Skip to main content
Licence CPOL
First Posted 10 May 2010
Views 5,445
Downloads 42
Bookmarked 4 times

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

By | 29 May 2010 | Article
Step by step on how to create a Windows Forms joke jukebox app in C++.

Introduction

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.

Listbox

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)
{

    label8->Text="";
    label1->Text="Jokefile= "+fileList[listBox1->SelectedIndex] ;
    ProcessFile( fileList[listBox1->SelectedIndex] );
    jokeFileCount=listBox1->SelectedIndex;
    listBox2->SetSelected(0,true);
    updateInfoDisplay();
    this->Text ="Joke Jukebox by TopCoder  [" + 
                   fileList[listBox1->SelectedIndex]+"]";
}

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)
{
    label8->Text=JokeList[listBox2->SelectedIndex];
    jokeLineCount=listBox2->SelectedIndex;
    updateInfoDisplay();
}

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)
    {
        jokeLineCount=(int)iJokeList[doneLines];
        listBox2->SetSelected(jokeLineCount,true);
        label8->Text=JokeList[jokeLineCount];
        label10->Text="Unread Lines "+(LineCount-doneLines) +"  ";
        label11->Text="Unread Files "+(FileCount-doneFiles);

        doneLines++;
        if (doneLines>=LineCount)
        {
            doneLines=0;
            updateInfoDisplay();
            jokeFileCount=(int)iFileList[doneFiles];
            ProcessFile( fileList[jokeFileCount] );
            listBox1->SetSelected(jokeFileCount,true);
            label1->Text="Jokefile= "+fileList[jokeFileCount] ;
            listBox2->SetSelected(0,true);
            label11->Text="Unread Files "+(FileCount-doneFiles);

            GenerateRandomLines();

            doneFiles++;
            if (doneFiles>=FileCount)
            {
                initRandomMode();
            }
        }
    }
}

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

Thanks for reading.

License

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

About the Author

TopCoder23



Sweden Sweden

Member

About me:
I attended programming School and I have a degree in three 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've gotten paid to teach coding. I am roughly 20 years old and i have been a teacher's assistant in programming ,
i have held a lecture in Visual basic 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 about a dozen small simple applications and games.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 1 PinmemberPanic2k36:35 11 May '10  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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 | Mobile
Web03 | 2.5.120517.1 | Last Updated 30 May 2010
Article Copyright 2010 by TopCoder23
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid