[译]如何在C ++中创建Windows Forms Joke Jukebox
By robot-v1.0
本文链接 https://www.kyfws.com/applications/how-to-create-a-windows-forms-joke-jukebox-in-c-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 3 分钟阅读 - 1375 个词 阅读量 0[译]如何在C ++中创建Windows Forms Joke Jukebox
原文地址:https://www.codeproject.com/Articles/79936/How-to-create-a-Windows-Forms-Joke-Jukebox-in-C
原文作者:Software_Developer
译文由本站 robot-v1.0 翻译
前言
Step by step on how to create a Windows Forms joke jukebox app in C++.
逐步介绍如何在C ++中创建Windows Forms笑话点唱机应用程序.
介绍(Introduction)
在Microsoft Visual C ++ 2005 Express Edition中设计应用程序非常简单.指向这个,点击那个,保存!将其放在表单上.但是请认真地单击Visual C ++ 2005 Express Edition左侧工具栏中的所需组件(控件),然后将其绘制在表单上的适当位置.(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.)
表单上的组件与矩形网格对齐,为您的应用程序提供对称外观.这个简单的应用程序演示了在Windows窗体中创建有趣的应用程序的简便性.(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)
快进一点,我们创建了一个Windows窗体,在该窗体上放置了列表框,标签和常用对话框.为了使所有这些控件都能响应鼠标单击,我们必须在其中添加一些代码.因此,我们双击任何给定的控件,然后在该控件的event方法中放置一些代码.(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.)
listBox1_SelectedIndexChanged事件(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]+"]";
}
Form1 :: GenerateRandomFiles()方法(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--;
}
}
listBox2_SelectedIndexChanged_1事件(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();
}
Form1 :: displayRandomJokeLines()方法(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();
}
}
}
}
这就是在Windows窗体中创建有趣的应用程序很容易的过程.(And that is how easy it is to create fun applications in Windows Forms.)
谢谢阅读.(Thanks for reading.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
C++ .NET Dev 新闻 翻译