Merhaba arkadaşlar bu makalemizde C++ ile random sayı oluşturacağız. Bu örneğimizde textbox ta 7 random sayının gösterimini sağlayacağız. Formumuza 1 er adet TextBox ve Button ekleyelim. Buttonumuzun click kısmına aşağıdaki kodu yazacağız.
Şekil 1
Şekil 2
MyForm.h
#pragma once
#include <stdlib.h>
#include <time.h>
namespace cpprandomnumbersgenerator {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Summary for MyForm
/// </summary>
public ref class MyForm : public System::Windows::Forms::Form
{
public:
MyForm(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~MyForm()
{
if (components)
{
delete components;
}
}
protected:
private:
/// <summary>
/// Required designer variable.
/// </summary>
System::ComponentModel::Container ^components;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ button1;
PictureBox^ pbox;
#pragma region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
void InitializeComponent(void)
{
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// textBox1
//
this->textBox1->Font = (gcnew System::Drawing::Font(L"Tahoma", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->textBox1->Location = System::Drawing::Point(229, 29);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(391, 46);
this->textBox1->TabIndex = 0;
//
// button1
//
this->button1->Font = (gcnew System::Drawing::Font(L"Tahoma", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(162)));
this->button1->Location = System::Drawing::Point(292, 96);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(255, 42);
this->button1->TabIndex = 1;
this->button1->Text = L"Olustur";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &MyForm::button1_Click);
//
// MyForm
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(632, 175);
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Name = L"MyForm";
this->Text = L"MyForm";
this->Load += gcnew System::EventHandler(this, &MyForm::MyForm_Load);
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
private: System::Void MyForm_Load(System::Object^ sender, System::EventArgs^ e) {
pbox = gcnew PictureBox;
pbox->Location = Point(10, 10);
pbox->Size = System::Drawing::Size(150, 150);
pbox->Image = Image::FromFile(L"D:\\numbers.jpg");
Controls->Add(pbox);
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
int dizi[7] = { 0 };
srand((int) time(0));
// random dizi degerlerini olusturuyoruz.
for (int i = 0; i < 7; i++)
dizi[i] = (rand() % 99) + 1;
// tboxta 7 random numarasini gosterimini sagliyoruz.
this->textBox1->Text = Convert::ToString(dizi[0]);
this->textBox1->Text += "-";
this->textBox1->Text += Convert::ToString(dizi[1]);
this->textBox1->Text += "-";
this->textBox1->Text += Convert::ToString(dizi[2]);
this->textBox1->Text += "-";
this->textBox1->Text += Convert::ToString(dizi[3]);
this->textBox1->Text += "-";
this->textBox1->Text += Convert::ToString(dizi[4]);
this->textBox1->Text += "-";
this->textBox1->Text += Convert::ToString(dizi[5]);
this->textBox1->Text += "-";
this->textBox1->Text += Convert::ToString(dizi[6]);
}
};
}
MyForm.cpp
#include "MyForm.h"
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
cpprandomnumbersgenerator::MyForm form;
Application::Run(% form);
}
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek üzere. Bahadır ŞAHİN
0 comments:
Yorum Gönder