Created
May 4, 2009 10:59
-
-
Save Jirapong/106420 to your computer and use it in GitHub Desktop.
dynamic add control on-the-fly
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'winforms' | |
form = create_form :text => "Hello", :top_most => true | |
btn = create :text => "Click Me" | |
txt = create :text => "(Your name)" | |
layout form, [btn, txt], :top => 100, :spacing => 20 | |
btn.click do |sener, args| | |
MessageBox.show("Hello #{txt.text}") | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | |
Form = System::Windows::Forms::Form | |
Button = System::Windows::Forms::Button | |
TextBox = System::Windows::Forms::TextBox | |
Application = System::Windows::Forms::Application | |
MessageBox = System::Windows::Forms::MessageBox | |
def create(control_class, params) | |
control = control_class.new | |
params.each_pair { |k,v| control.send("#{k}=".to_sym, v) } | |
control | |
end | |
def create_form(params) | |
f = create(Form, params) | |
t = Thread.new(f) { |form| Application.run form } | |
$t = t | |
f | |
end | |
def invoke(form, &b) | |
$t.invoke(b, form) | |
end | |
def add_control(form, control) | |
$t.invoke(form) { |*| form.controls.add control } | |
end | |
def default(value, default_value) | |
value = value.nil?? default_value : value | |
end | |
def layout(form, controls, params) | |
current = default(params[:top], 100) | |
spacing = default(params[:spacing], 50) | |
controls.each do |control| | |
control.top = current | |
current += control.top + spacing | |
add_control(form, control) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment