Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / WinForms

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

1.00/5 (1 vote)
30 May 2010CPOL1 min read 23.3K   133  
Step by step on how to create a Windows Forms joke jukebox app in C++.

Image 1

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.

C++
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.

C++
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.

C++
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.

C++
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)