[Closed] Create buttons at routime
Hi
Here is my first rewrited DotNet code from C# in to Maxscript
Maybe someone can find it usefull…
C# Syntax
C# Syntax (Toggle Plain Text) //r0ckbaer
using System;
using SystemDrawing;
using SystemCollections;
using SystemComponentModel;
using SystemWindowsForms;
using SystemData;
using SystemTextRegularExpressions;
namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1
/// </summary>
public class Form1 : SystemWindowsFormsForm
{
private SystemWindowsFormsTextBox textBox1;
private SystemWindowsFormsTextBox textBox2;
private SystemWindowsFormsButton button1;
/// <summary>
/// Required designer variable
/// </summary>
private SystemComponentModelContainer components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/// <summary>
/// Clean up any resources being used
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
componentsDispose();
}
}
baseDispose( disposing );
}
#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>
private void InitializeComponent()
{
thistextBox1 = new SystemWindowsFormsTextBox();
thistextBox2 = new SystemWindowsFormsTextBox();
thisbutton1 = new SystemWindowsFormsButton();
thisSuspendLayout();
//
// textBox1
//
thistextBox1Location = new SystemDrawingPoint(16, 16);
thistextBox1Name = "textBox1";
thistextBox1Size = new SystemDrawingSize(48, 20);
thistextBox1TabIndex = 0;
thistextBox1Text = "textBox1";
//
// textBox2
//
thistextBox2Location = new SystemDrawingPoint(72, 16);
thistextBox2Name = "textBox2";
thistextBox2Size = new SystemDrawingSize(56, 20);
thistextBox2TabIndex = 1;
thistextBox2Text = "textBox2";
//
// button1
//
thisbutton1Location = new SystemDrawingPoint(152, 16);
thisbutton1Name = "button1";
thisbutton1TabIndex = 2;
thisbutton1Text = "button1";
thisbutton1Click += new SystemEventHandler(thisbutton1_Click);
//
// Form1
//
thisAutoScaleBaseSize = new SystemDrawingSize(5, 13);
thisClientSize = new SystemDrawingSize(240, 46);
thisControlsAdd(thisbutton1);
thisControlsAdd(thistextBox2);
thisControlsAdd(thistextBox1);
thisName = "Form1";
thisText = "Form1";
thisResumeLayout(false);
}
#endregion
/// <summary>
/// The main entry point for the application
/// </summary>
[STAThread]
static void Main()
{
ApplicationRun(new Form1());
}
private void button1_Click(object sender, SystemEventArgs e)
{
// This is the generate button routine
int count = 0;
SystemCollectionsArrayList al = new ArrayList();
foreach (Control c in thisControls)
{
if (cGetType() == typeof(Button) && cName != "button1")
{
alAdd(c);
}
}
for (int i = 0; i < alCount; i++)
thisControlsRemove((Control)al[i]);
// Resize to start size
thisSize = new Size(248, 80);
// First one is columns
int columns = ConvertToInt32(textBox1Text);
int rows = ConvertToInt32(textBox2Text);
int locX = 16, locY = 56;
// We assume space between is 20
for (int i = 0; i < columns * rows; i++)
{
Button b = new Button();
bName = "b" + iToString();
bSize = new Size(10, 10);
bLocation = new Point(locX, locY);
if (((i + 1) % columns) == 0)
{
locX = 16;
locY += bSizeHeight + 20;
}
else
{
locX += bSizeWidth + 20;
}
thisControlsAdd(b);
bClick +=new EventHandler(b_Click);
if ((bLocationX + bWidth) > thisClientSizeWidth)
{
thisWidth += ((bLocationX + bWidth) - thisClientSizeWidth) + 10;
}
if ((bLocationY + bHeight) > thisClientSizeHeight)
{
thisHeight += ((bLocationY + bHeight) - thisClientSizeHeight) + 10;
}
}
}
private void b_Click(object sender, EventArgs e)
{
string s = ((Control)sender)Name;
s = new Regex("\\d+")Match(s)Value;
int num = ConvertToInt32(s);
int columns = ConvertToInt32(textBox1Text);
int rows = ConvertToInt32(textBox2Text);
s = stringFormat("Column: {0}, Row: {1}", (num % columns) + 1, (num / columns) + 1);
MessageBoxShow(this, s);
}
}
}
MaxScript
(
try form.close() catch()
form = dotnetObject "Form"
components = (dotnetObject "System.ComponentModel.Container").Components
--Clean up any resources being used
if components.count != 0 do components.Dispose()
--region Windows Form Designer generated code
fn button1Click s a =
(
--This is the generate button routine
al = dotnetObject "System.Collections.ArrayList"
--get all buttons except first one
for i = 0 to form.controls.count-1 do
(
itm = form.controls.item[i]
if (itm.GetType()).ToString() == "System.Windows.Forms.Button" and itm.name != "button1" do
(
al.add( itm )
)
)
--remove all buttons except first one
for i = 0 to al.count-1 do form.controls.Remove al.Item[ i ]
--Resize to start size
form.Size = dotNetObject "System.Drawing.Size" 248 80
--First one is columns
columns = if textBox1.Text as integer != undefined then textBox1.Text as integer else 0
rows = if textBox2.Text as integer != undefined then textBox1.Text as integer else 0
locX = 16
locY = 56
--We assume space between is 20
for i = 0 to columns * rows do
(
b = dotNetObject "Button"
b.Name = "b" + i as String
b.Size = dotNetObject "System.Drawing.Size" 10 10
b.Location = dotnetObject "System.Drawing.Point" locX locY
if mod (i+1) columns == 0
then
(
locX = 16
locY += b.Height + 20
)
else
(
locX += b.Width + 20
)
dotNet.addEventHandler b "Click" button1Click
form.Controls.Add(b)
if ((b.Location.X + b.Width) > form.Width) do
(
form.Width += ((b.Location.X + b.Width) - form.Width) + 10
)
if ((b.Location.Y + b.Height) > form.Height) do
(
form.Height += ((b.Location.Y + b.Height) - form.Height) + 10
)
)
)
fn InitializeComponent =
(
textBox1 = dotnetObject "TextBox"
textBox2 = dotnetObject "TextBox"
button1 = dotnetObject "Button"
form.SuspendLayout()
-- textBox1
textBox1.Location = dotnetObject "System.Drawing.Point" 16 16
textBox1.Name = "textBox1"
textBox1.Size = dotnetObject "System.Drawing.Size" 48 20
textBox1.TabIndex = 0
textBox1.Text = "4"
-- textBox2
textBox2.Location = dotnetObject "System.Drawing.Point" 72 16
textBox2.Name = "textBox2"
textBox2.Size = dotnetObject "System.Drawing.Size" 56 20
textBox2.TabIndex = 1
textBox2.Text = "8"
-- button1
button1.Location = dotnetObject "System.Drawing.Point" 152 16
button1.Name = "button1"
button1.TabIndex = 2
button1.Text = "button1"
dotNet.addEventHandler button1 "Click" button1Click
-- Form1
form.AutoScaleBaseSize = dotnetObject "System.Drawing.Size" 5 13
form.ClientSize = dotnetObject "System.Drawing.Size" 240 46
form.Controls.Add(button1)
form.Controls.Add(textBox2)
form.Controls.Add(textBox1)
form.Name = "Form1"
form.Text = "Form1"
form.ResumeLayout(false)
)
--mc2system.show components
InitializeComponent()
form.show()
)
one problem is there , keyboard is stil focused in to max interface.
(I can’t write in to textBox)
temporally way
fn onOpenForm = (enableAccelerators = false)
fn onCloseForm = (enableAccelerators = true)
dotNet.addEventHandler form “Shown” onOpenForm
dotNet.addEventHandler form “Closed” onCloseForm
I think this will be ok
fn onGotFocus = (enableAccelerators = false)
fn onLostFocus = (enableAccelerators = true)
dotNet.addEventHandler textBox1 “GotFocus” onGotFocus
dotNet.addEventHandler textBox1 “LostFocus” onLostFocus
dotNet.addEventHandler textBox2 “GotFocus” onGotFocus
dotNet.addEventHandler textBox2 “LostFocus” onLostFocus
I will be glad to see if anybody knows better way
hi i ran the maxscript code you have written and when i press button1 i got
Unknown property: "text" in undefined
so i changed two lines and it worked fine
--First one is columns
columns = if form.controls.item[1].Text as integer != undefined then form.controls.item[1].Text as integer else 0
rows = if form.controls.item[1].Text as integer != undefined then form.controls.item[1].Text as integer else 0
and you could use the max dotnetform to get the textbox to work without the two function onGetFocus and onLostFoucs.
form = dotNetObject "MaxCustomControls.MaxForm"
-and use showModeless()
form.ShowModeless()
Hi Akram
You get:
Unknown property: "text" in undefined
is because I cereated it in 3DsMax9 and maybe in higer max versions access is changed.
I cant use :
form = dotNetObject "MaxCustomControls.MaxForm".
Version 9 dont have yet file MaxCustomControls.dll
thanks anyway man :)
here is the next version:
Global mc2ButtonForm
try mc2ButtonForm.close() catch()
-- Create buttons at routime
(
--Using
local netCursors = (dotNetClass "Cursors")
local netFlatStyle = (dotnetClass "System.Windows.Forms.FlatStyle")
local netContainer = (dotnetObject "System.ComponentModel.Container")
fn netDrColor clr = ((dotNetClass "Drawing.Color").fromArgb clr.r clr.g clr.b)
fn netDrPoint pos = (dotnetObject "Drawing.Point" pos.x pos.y)
fn netDrSize size = (dotNetObject "Drawing.Size" size.x size.y)
fn netDecimal val = (dotnetObject "System.Decimal" val)
mc2ButtonForm = dotnetObject "Form"
local form = mc2ButtonForm
local sp1 = dotnetObject "NumericUpDown"
local sp2 = dotnetObject "NumericUpDown"
--Clean up any resources being used
local components = netContainer.Components
if components.count != 0 do components.Dispose()
--form.Container
--This is the generate button routine
fn rClickMenu =
(
rcMenu mainMenu
(
separator file_menu_1
menuItem new_1 "Close Form..."
separator file_menu_2
on new_1 picked do (form.close())
)
PopupMenu mainMenu
)
fn button1Click s a =
(
local al = dotnetObject "System.Collections.ArrayList"
--get all buttons
for i = 0 to form.controls.count-1 do
(
local itm = form.controls.item[i]
if (itm.GetType()).ToString() == "System.Windows.Forms.Button" do
(
al.add( itm )
)
)
--remove all buttons
for i = 0 to al.count-1 do form.controls.Remove al.Item[ i ]
--Resize to start size
form.Size = netDrSize [248, 80]
--First one is columns
local columns = sp1.value
local rows = sp2.value
local padding = 10
local btn_size = 24
local locX = padding
local locY = 56
--We assume space between is 20
for i = 0 to columns * rows do
(
local b = dotNetObject "Button"
b.Name = "b" + i as String
b.Size = netDrSize [btn_size, btn_size]
b.Location = netDrPoint [locX, locY]
b.Cursor = netCursors.Default
b.FlatStyle = netFlatStyle.System
if mod (i+1) columns == 0
then
(
locX = padding
locY += b.Height + padding
)
else
(
locX += b.Width + padding
)
dotNet.addEventHandler b "Click" button1Click
form.Controls.Add(b)
if ((b.Location.X + b.Width) > form.Width) do
(
form.Width += ((b.Location.X + b.Width) - form.Width) + btn_size
)
if ((b.Location.Y + b.Height) > form.Height) do
(
form.Height += ((b.Location.Y + b.Height) - form.Height) + btn_size
)
)
)
fn onGotFocus = (enableAccelerators = false)
fn onLostFocus = (enableAccelerators = true)
fn InitializeComponent =
(
form.SuspendLayout()
--spinner1
sp1.Size = netDrSize [42, 20]
sp1.Location = netDrPoint [16, 16]
sp1.Name = "sp1"
sp1.TabIndex = 0
sp1.Minimum = netDecimal 1
sp1.Maximum = netDecimal 40
sp1.ReadOnly = true
--mc2System.show sp1
dotNet.addEventHandler sp1 "ValueChanged" button1Click
dotNet.addEventHandler sp1 "GotFocus" onGotFocus
dotNet.addEventHandler sp1 "LostFocus" onLostFocus
--spinner2
sp2.Size = netDrSize [42, 20]
sp2.Location = netDrPoint [60, 16]
sp2.Name = "sp2"
sp2.TabIndex = 0
sp2.Minimum = netDecimal 1
sp2.Maximum = netDecimal 40
sp2.ReadOnly = true
dotNet.addEventHandler sp2 "ValueChanged" button1Click
dotNet.addEventHandler sp2 "GotFocus" onGotFocus
dotNet.addEventHandler sp2 "LostFocus" onLostFocus
-- Form1
form.AutoScaleBaseSize = netDrSize [5, 13]
form.ClientSize = netDrSize [240, 46]
--form.width = 200
--form.height = 200
form.backColor = netDrColor (color 0 255 0)
form.opacity = 0.8
form.FormBorderStyle = form.FormBorderStyle.FixedDialog
form.ShowInTaskbar = false --hide from windows task bar
form.ControlBox = false --hide main bar
form.StartPosition = form.StartPosition.Manual
form.Location = netDrPoint [ 200, 100 ]
form.Name = "Form1"
dotNet.addEventHandler form "mouseUp" rClickMenu
form.Controls.Add(sp2)
form.Controls.Add(sp1)
form.ResumeLayout(false)
--mc2system.show form
)
InitializeComponent()
form.show()
form.BringToFront()
form.focus()
)
I aded two spinners there. Is posible set one of them verticaly ?
Spinner in Dotnet is a Trackbar and at least in c# you only need to set the orientation to vertical .
In maxscrip You Must call the Orientation Class Before you’re able to use that
form = dotnetobject"System.Windows.Forms.form"
spi = dotnetobject"System.Windows.Forms.Trackbar"
Ori = dotnetclass"System.Windows.Forms.Orientation"
spi.Orientation = Ori.vertical
form.controls.add(spi)
form.show()
Hi Cyfer
It will be not a good idea to controll buttons columns and rows with trackbars.
But for training was wery good! Thanks for this
(
try (form.close()) catch()
--DotNet Components
fn netDrSize size = (dotNetObject "Drawing.Size" size.x size.y)
fn netDrPoint pos = (dotnetObject "Drawing.Point" pos.x pos.y)
fn netDrColor clr = ((dotNetClass "Drawing.Color").fromArgb clr.r clr.g clr.b)
local netCursors = (dotNetClass "Cursors")
--Local Variables
local trackbar_width = 45
local form_bounds = 8
local rows_columns = [6,2]
local btn_size = 48
local form_size = ([btn_size, btn_size] * rows_columns)+trackbar_width + form_bounds
--Interface Elements
form = dotnetobject"System.Windows.Forms.form"
spiH = dotnetobject"System.Windows.Forms.Trackbar"
spiV = dotnetobject"System.Windows.Forms.Trackbar"
tlp = dotNetObject "TableLayoutPanel"
btn = dotNetObject "button"
--Interface Setup
form.ControlBox = false --hide main bar
form.size = netDrSize form_size
form.StartPosition = form.StartPosition.Manual
form.Location = netDrPoint [ 200, 100 ]
tlp.size = netDrSize (form_size - trackbar_width - form_bounds)
tlp.backColor = netDrColor( green )
spiH.TickStyle = spiH.TickStyle.BottomRight
spiH.SetBounds 0 tlp.height 100 4--tlp.width 4 --pos size
spiH.SetRange 0 10
--spiH.size = netDrSize [tlp.width, 4]
--spiH.Location = netDrPoint [0, tlp.height]
--spiH.TickFrequency = 1
--spiH.value = 0
spiV.Orientation = spiV.Orientation.vertical
spiV.TickStyle = spiV.TickStyle.TopLeft
spiV.SetBounds tlp.width -4 4 100--tlp.height --pos size
spiV.SetRange 0 10
--spiV.size = netDrSize [4, tlp.height]
--spiV.Location = netDrPoint [tlp.width, 0]
--spiV.TickFrequency = 1
--spiV.value = 0
btn.size = netDrSize [trackbar_width, trackbar_width]
btn.location = netDrPoint [tlp.width,tlp.height]
btn.text = "Close"
--Functions
fn onBtnMouseUp = form.close()
--Event Handlers
dotNet.addEventHandler btn "MouseUp" onBtnMouseUp
form.controls.add(tlp)
form.controls.add(spiH)
form.controls.add(spiV)
form.controls.add(btn)
form.show()
)
Finaly is finished first version.
here is a picture:
here si code:
Global mc2ButtonForm
(
try (mc2ButtonForm.close()) catch()
--DotNet Components
fn netDrSize size = (dotNetObject "Drawing.Size" size.x size.y)
fn netDrPoint pos = (dotnetObject "Drawing.Point" pos.x pos.y)
fn netDrColor clr = ((dotNetClass "Drawing.Color").fromArgb clr.r clr.g clr.b)
fn netDrRect pos size = (dotnetObject "Drawing.Rectangle" pos.x pos.y size.x size.y)
local netCursors = (dotNetClass "Cursors")
--Local Variables
local spinner_width = 20
local form_bounds = 10
local columns = 6
local rows = 2
local intr_btn_size = 22
local user_btn_size = 48
local form_size = ([user_btn_size, user_btn_size] * [columns, rows])+spinner_width + form_bounds
--Interface Elements
mc2ButtonForm = dotnetObject "Form"
local form = mc2ButtonForm
local tlp = dotNetObject "TableLayoutPanel"
local btnExit = dotNetObject "button"
local btnHL = dotNetObject "button"
local btnHR = dotNetObject "button"
local btnVU = dotNetObject "button"
local btnVD = dotNetObject "button"
local lblH = dotNetObject "label"
local lblV = dotNetObject "label"
--Interface Setup
form.ControlBox = false --hide main bar
form.size = netDrSize form_size
form.StartPosition = form.StartPosition.Manual
form.Location = netDrPoint [ 200, 100 ]
form.BackColor = netDrColor yellow
tlp.size = netDrSize (form_size - spinner_width - form_bounds)
tlp.backColor = netDrColor( green )
btnHL.Bounds = netDrRect [0 , tlp.height] [intr_btn_size, intr_btn_size]
btnHL.FlatStyle = btnHR.FlatStyle.flat
btnHL.text = "<"
btnHL.tag = "HL"
btnHL.BackColor = netDrColor (color 124 196 22)
btnHR.Bounds = netDrRect [tlp.width - intr_btn_size, tlp.height] [intr_btn_size, intr_btn_size]
btnHR.FlatStyle = btnHR.FlatStyle.flat
btnHR.text = ">"
btnHR.tag = "HR"
btnHR.BackColor = netDrColor (color 124 196 22)
btnVU.Bounds = netDrRect [tlp.width , 0] [intr_btn_size, intr_btn_size]
btnVU.FlatStyle = btnHR.FlatStyle.flat
btnVU.text = "^"
btnVU.tag = "VU"
btnVU.BackColor = netDrColor (color 124 196 22)
btnVD.Bounds = netDrRect [tlp.width , tlp.height - intr_btn_size] [intr_btn_size, intr_btn_size]
btnVD.FlatStyle = btnHR.FlatStyle.flat
btnVD.text = "v"
btnVD.tag = "VD"
btnVD.BackColor = netDrColor (color 124 196 22)
lblH.Bounds = netDrRect [intr_btn_size, tlp.height] [tlp.width - 2*intr_btn_size, intr_btn_size]
lblH.TextAlign = lblH.TextAlign.MiddleCenter
lblH.BackColor = netDrColor (color 84 132 15)
lblH.text = "Columns:" + ((columns as integer) as string)
lblV.Bounds = netDrRect [tlp.width, intr_btn_size] [intr_btn_size, tlp.height - 2*intr_btn_size]
lblV.TextAlign = lblH.TextAlign.MiddleCenter
lblV.BackColor = netDrColor (color 84 132 15)
lblV.text = "Rows:" + ((rows as integer) as string)
btnExit.size = netDrSize [intr_btn_size, intr_btn_size]
btnExit.location = netDrPoint [tlp.width,tlp.height]
btnExit.BackColor = netDrColor (green)--color 138 15 15)
btnExit.text = "X"
--Functions
fn closeForm = (form.close())
fn onBtnMouseUp s a =
(
format "name:% tag:%
" s.name s.tag
)
fn addButton int =
(
local btn = (dotNetObject "Button")
btn.size = netDrSize [user_btn_size, user_btn_size]
btn.flatstyle = btn.flatstyle.flat
btn.margin = dotnetobject "padding" 0
btn.text = (int as integer) as string
btn.name = "button_"+btn.text
btn.tag = 123
dotNet.addEventHandler btn "MouseUp" onBtnMouseUp
btn
)
fn generateButtons =
(
tlp.controls.clear()
local cnt = 0
for x = 0 to rows - 1 do
for y = 0 to columns - 1 do
tlp.Controls.Add (addButton (cnt +=1)) y x
)
fn resizeInterface s a =
(
case s.tag of
(
"HL":(if columns > 1 do columns -= 1)
"HR":(if columns < 50 do columns += 1)
"VU":(if rows > 1 do rows -= 1)
"VD":(if rows < 50 do rows += 1)
)
lblH.text = "Columns:" + ((columns as integer) as string)
lblV.text = "Rows:" + ((rows as integer) as string)
generateButtons()
local s = ([user_btn_size, user_btn_size] * [columns, rows])+spinner_width + form_bounds
local v = s.x
local h = s.y
form.size = netDrSize s
tlp.size = netDrSize (s - spinner_width - form_bounds)
btnHL.Bounds = netDrRect [0 , tlp.height] [intr_btn_size, intr_btn_size]
btnHR.Bounds = netDrRect [tlp.width - intr_btn_size, tlp.height] [intr_btn_size, intr_btn_size]
btnVU.Bounds = netDrRect [tlp.width , 0] [intr_btn_size, intr_btn_size]
btnVD.Bounds = netDrRect [tlp.width , tlp.height - intr_btn_size] [intr_btn_size, intr_btn_size]
lblH.Bounds = netDrRect [intr_btn_size, tlp.height] [tlp.width - 2*intr_btn_size, intr_btn_size]
lblV.Bounds = netDrRect [tlp.width, intr_btn_size] [intr_btn_size, tlp.height - 2*intr_btn_size]
btnExit.location = netDrPoint [tlp.width,tlp.height]
)
--Event Handlers
dotNet.addEventHandler btnExit "MouseUp" closeForm
dotNet.addEventHandler btnHL "MouseUp" resizeInterface
dotNet.addEventHandler btnHR "MouseUp" resizeInterface
dotNet.addEventHandler btnVU "MouseUp" resizeInterface
dotNet.addEventHandler btnVD "MouseUp" resizeInterface
form.controls.add(tlp)
form.controls.add(btnHL)
form.controls.add(btnHR)
form.controls.add(btnVU)
form.controls.add(btnVD)
form.controls.add(lblH)
form.controls.add(lblV)
form.controls.add(btnExit)
generateButtons()
form.show()
)
can anybody make it better or faster?
chers
you can do exactly same things using DataGridView control with custom paint…
to change number of rows and columns of the grid on fly is not a big deal.
LOL , i mixed spinners and sliders and it seems you were asking about scrollbars
i wonder, when you write a form derived from the System.windows.form and not
from MaxCustomControls.Maxform … how do you handle the keyboard entry?
the min/maximize main max window …etc ?
Hi Denis
you say DataGridView… okay I will try
but you know? inside is not simple grid , but buttons.
and arrows is not scrollbars , but manipulators for increment buttons count
vertical and horizontal …