Phone: 905 409-1589
Email: info@penproductions.ca
RSS LinkedIn Twitter Twitter
DotNet / Button:
DotNet Button:
Comments:
You might ask why you would want to use a DotNet button over a Max script button. The simple answer might be, because you can. There are advantages to using the DotNet buttons because of how flexible they are. For instance a Max script button has two event handlers where the DotNet button has sixty nine different event handlers. I'm sure that 99% of the time we don't need anything other then a Max script button but in case you need just that little more control they come in handy. Also if you go further with DotNet you will find that they have their own dialogs as well. Only DotNet controls can be added to a DotNet dialog so you will need to have a good understanding of all the UI items that you will want to replicate.
Creating a Button:
We will get started with a simple rollout and the dotNetControl to create the button. I have set a height property to 50 just to help illustrate one of the display features. Once again we use dotNetControl and pass it the name space path to the forms control called button.

 

 

Code:
try(destroyDialog testR)catch()

rollout testR "Test"
(
	dotNetControl testButton "system.windows.forms.button" height:50
)
createDialog testR
								

Setting Properties:
We can use the button as it is or apply a few properties, one that you are almost always going to want to set is the .text property so the user knows what the button is for. This property is simple as it just takes a standard Max script string as the input.

FlatStyle is property that will allow you to select from one of four options to affect the look of a button. Use "showProperties theButton.flatStyle" and it will print out the options as shown below. .Standard is the default, .Flat will draw the button with just an out line around it, .popup will set it so that it is flat until the mouse moves over the button when it will display as 3D and .system will look the same as the default Max script buttons.

 

Listener:
  .Flat : , read-only, static
  .Popup : , read-only, static
  .Standard : , read-only, static
  .System : , read-only, static
								

To set the flat style there are two options. First you can create the flatStyle object using dotNetClass and then setting the desired style and passing that to the .flatStyle property. I have commented that method out for a simpler version where you use the .flatStyle property that it self returns a dotNetObject and do the same by setting the style that you would use to use. Either way works and from test I don't see one being any faster then the other but the second is less code.

 

TextAlign will allow the position of the text property to be set and has several properties. Note that it doesn't appear to work when the system option is used. You can set this property using the dotNetClass method or the same method that we used to set the flatStyle.

BackColor is a property that you will find in most dotNetControls and needs the dotNetObject system.drawing.color to be created and a color passed in. Here I'm using one of the preset colours, lightBlue but you can also use the fromARGB method and set your own RGB colors values.

 

Code:
	on testR open do
	(
		--Use
-- 		testButton.flatStyle=(dotNetClass "System.Windows.Forms.FlatStyle").flat
		--Or
		testButton.flatStyle=testButton.flatStyle.flat
		testButton.text="Test Button"
		testButton.TextAlign= (dotNetClass "System.Drawing.ContentAlignment").BottomRight
		testButton.backColor=testButton.backColor.lightBlue
	)
								

Event Handlers:
For the event handlers we are going to setup the button to change colours when it is selected or the mouse is over the button.

The first and second event handlers are using both the senderArg and the arg arguments for the mouseDown and mouseUp. SenderArg just holds a reference to the button itself, you could replace senderArg in the event handler expression use testButton and get the same result. The reason we're using the senderArg is it would be easier to cut and paste the event handlers for other buttons as we would only have to change testButton to the name of the new button.

In this case we are testing to see which mouse button has been pressed on the button. We use the arg argument to do this. If you use "showProperties arg" you can get the seven properties that can be accessed.

 

Listener:
  .Button : , read-only
  .Clicks : , read-only
  .Delta : , read-only
  .Location : , read-only
  .X : , read-only
  .Y : , read-only
  .Empty : , read-only, static
								

We will use the .button property that returns a "system.windows.forms.mouseButtons" object, in the listener use showProperties (dotnetclass "System.Windows.Forms.MouseButtons") to get the properties of the object. Depending on which button is pressed, the mouseButtons object will return on of the six properties. We can then use that to test what button has been pressed.

 

Listener:
  .Left : , read-only, static
  .Middle : , read-only, static
  .None : , read-only, static
  .Right : , read-only, static
  .XButton1 : , read-only, static
  .XButton2 : , read-only, static
								

Once the button has been tested we can use the backColor property and set the color as we did to initialize the button in the on open handler.

 

Code:
	on testButton mouseDown senderArg arg do
	(
		if arg.button==arg.button.left then
		(
			senderArg.backColor=senderArg.backColor.red
		)
	)
	on testButton mouseUp senderArg arg do
	(
		if arg.button==arg.button.left then
		(
			senderArg.backColor=senderArg.backColor.green
		)
	)
								

The next two event handlers will set the colour as the mouse moves into and out of the area of the button.

 

Code:
	on testButton mouseEnter senderArg arg do
	(
		senderArg.backColor=senderArg.backColor.green
	)
	on testButton mouseLeave senderArg arg do
	(
		senderArg.backColor=senderArg.backColor.lightBlue
	)
								

Another Option:

"just an extra bit while we are on the subject of buttons - when using the flat styling property, you don't have to use the mouseover and mousedown handlers to change button color states, there is a flatappearance property that can do this for you"

Using the code below means you don't have to use the event handlers at all to get the colours to change.

Code:
try(destroyDialog testR)catch()
 
 rollout testR "Test"
 (	
 	local Ccolor = dotnetclass "system.drawing.color"	
 	dotNetControl testButton "system.windows.forms.button" height:50
 				
 	on testR open do
 	(
 		testButton.flatStyle=testButton.flatStyle.flat
 		testButton.text="Test Button"
 		testButton.TextAlign= (dotNetClass "System.Drawing.ContentAlignment").BottomRight
 		testButton.backColor=testButton.backColor.lightBlue
 		
 		testButton.flatappearance.mouseoverbackcolor = Ccolor.green
 		testButton.flatappearance.mousedownbackcolor = Ccolor.red		
 		testButton.flatappearance.bordercolor = Ccolor.yellow		
 	)	
 )
createDialog test
                                
Font Styling:

We can set the font using the "System.Drawing.Font" object. Either add a function that can be called when the rollout opens or just add it to the on open event handler as we have above. This method can be applied any where that the font can be changed in a dotNetControl.

 

Code:
testButton.font = dotNetObject "System.Drawing.Font" "Times New Roman" 24
                                

In this example not only is the font type being changed but also a font style is being created and applied to the button. For this the "System.Drawing.FontStyle" class is needed to be able to create the styles that will be used. Because we are adding two different styles the dotNet.combineenums command is used to create a single style object that uses both styles.

 

Code:
dnFontStyle = dotNetClass "System.Drawing.FontStyle"
myFontStyle = dotnet.combineenums dnFontStyle.bold dnFontStyle.italic
testButton.font = dotNetObject "System.Drawing.Font" "Times New Roman" 24 myFontStyle