コードスペランカー

ゲーム開発日誌など

.net Frameworkのお勉強02

今度はボタンをつける
testForm.rb

require "mscorlib"
require "System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
require "System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

class MainForm < System::Windows::Forms::Form
	def initialize()
		self.InitializeComponent()
	end

	def InitializeComponent()
		self.Name = "MainForm"
		self.Text = "test02"
		self.Size = System::Drawing::Size.new(292,200)

		button01 = System::Windows::Forms::Button.new()
		button01.Location = System::Drawing::Point.new(109, 50)
		button01.Name = "button01"
		button01.Text = "Botton"
		self.Controls.Add(button01)
		button01.Click do |sender, e|
			self.button01_Click
		end
	end

	def button01_Click()
		puts"押した!\n"
	end
end

前回よりちょっとソースが長くなった。
差分の説明

		button01 = System::Windows::Forms::Button.new()
		button01.Location = System::Drawing::Point.new(109, 50)
		button01.Name = "button01"
		button01.Text = "Botton"
		self.Controls.Add(button01)

この辺がボタンの設置。
Locationはボタンの設置場所
Nameはボタンの名前
Textはボタンに表示するラベル
Controls.Add(button01)でフォームにボタンを貼り付けている

		button01.Click do |sender, e|
			self.button01_Click
		end

この辺がボタンのをクリックしたときのイベント設定