Click here to Skip to main content
Click here to Skip to main content

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

, 29 May 2010 CPOL
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++.

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)

Share

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.

Comments and Discussions

 
GeneralMy vote of 1 PinmemberPanic2k311-May-10 6:35 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.141030.1 | Last Updated 30 May 2010
Article Copyright 2010 by Software_Developer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid