create dotnetcontrol buttons inside function

I want to create buttons depending on certain things!
So here is what I need:

fn myfunction=
for i=1 to IDcount do
     dotnetcontrol ID_btn[i] "System.Windows.Forms.Button" width:20 height:60 pos:[27,i*153]
     ID_btn[i].backcolor  = (dotnetcolor 70 70 70)
     ID_btn[i].flatstyle = dotnetobject "System.Windows.Forms.FlatStyle" Flat
     ID_btn[i].foreColor = (dotnetcolor 200 200 200)

How to make it work?


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.'s picture

Thx Barigazy!

I use this

append btnarray #(prevh,2, w, h, (ID as string), s.material) --w= 33
append btnarray #(prevh,1, w, h, "multimaterial", s.material)--w= 33
append btnarray #(prevh,3, w, h, (classOf s.material) as string, s.material)--w= 66
But how can I detect which button is selected?
--1, 2 and 3 - is the type of material!
pn.controls.add (defBtn 0 btnarray[i][2] w:btnarray[i][3] h:h txt:btnarray[i][5])	

How to find out which s.material is used for certain button? and how to find out is this button ID btn or material?

barigazy's picture

I will use "Tag" property of

I will use "Tag" property of dotnet object in this case button.
Simply put some information lat say materilal in button tag prop
btn1.tag =
then use findItem method for btnarray or

for i = 0 to (pn.controls.count - 1) where pn.Controls.Item[i].tag == do print i

barigazy's picture


But one more question!

If I add btns in dotnet panel

pn.controls.addRange ctrlsIn2st

then how to use them?

on --<<>>??? click do


script_btns.jpg 120.33 KB
barigazy's picture

I post already my write

I post already my write method.
You need to predefine locals fo button objects.
Then you nedd to define events for this buttons. Let's say you have:

btn = dotnetobject "Button" = "Button1"
-- now we need a fn
fn printSomething sender arg = print
--and only you need to do next
--If you use mxs rollout dialog (example "TestRoll") then in open events put this
on testroll open do
    dotnet.addeventhandler btn "Click" printSomething

on btn click arg do ... you can use only if button is dotNetControl but not dotNetObject. In mxs help you can find topics about dotNet definition

barigazy's picture

This is what I came up with

try(destroyDialog testR)catch()
rollout testR "Test" width:400 height:400
	dotnetcontrol pn "Panel" pos:[5,5] width:390 height:378
	fn defColor r g b = ((dotNetClass "System.Drawing.Color").FromArgb r g b)
	local COL1 = (defColor 80 80 80), COL2 = (defColor 150 150 150)--, COL3 = (defColor 65 125 88), COL4 = (defColor 125 150 80)
	local ctrlsIn2st=#()
	fn defBtn txt: w: h: cnt:0 =
		print h
		btn = dotnetobject "Button"
		btn.text = txt
		btn.backcolor = COL2
		btn.width = w ; btn.height = h
		btn.Location = dotNetObject "System.Drawing.Point" 5 (-15 + (cnt*btn.height))
	on testR open do 
		pn.backcolor =
		pn.BorderStyle = pn.BorderStyle.FixedSingle
		pn.width = 390 ; pn.height = 390 
		for i=1 to 5 do -- add mat id buttons
			append ctrlsIn2st (defBtn txt:(i as string) w:20 h:60 cnt:i)
		pn.controls.addRange ctrlsIn2st
createDialog testR

Now I can easily go on! Thank you, Barigazy! You HELPED me lot!!!'s picture


ok! Thx! I will try now

barigazy's picture

My suggestion is to use

My suggestion is to use "Form" instead of Rollout because you use dotnet objects.
This is the way how I write my tools, is not the perfect aproch but works for me.

AttachmentSize 19.87 KB

barigazy's picture


sp.panel1.width = 15 

?'s picture

just tell me

how to define the width of the containers and if I can use just one container then let me know how to fill pn with one container. Everything else I understood how to do!'s picture

Except this

I thought to draw all buttons like you showed me in this function:

	fn defBtn pnl txt: w:50 h:50 cnt:0 =
		btn = dotnetobject "Button"
		btn.text = txt
		btn.width = 50 ; btn.height = 20
		btn.Location = dotNetObject "System.Drawing.Point" 5 (-15 + (cnt*btn.height))

Here it is possible to put location and dimensions! In fact I need only one container where I could add buttons!
Is it possible to define just one container where I could add these buttons, or should I make a thin column on the left and one wider on the right where I could add all the buttons?

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.