Notifications
Clear all

[Closed] Dynamic button width using DotNet?

I’m still working on learning dotNet. It seems to have a lot of useful applications in MaxScript, but there is one thing bothering me. When you create a button using MaxScript, by default it fits the text written in that button. What do I need to do for the same effect using dotNet?

3 Replies
1 Reply
(@denist)
Joined: 11 months ago

Posts: 0

 try(destroydialog resizableRol) catch()
 rollout resizableRol "Resizable"
 (
 	local minwidth = 120
 	local maxwidth = 300
 	dotnetcontrol pb "CheckBox" text:"" width:120 height:24 pos:[2,2]
 	dotnetcontrol tb "TextBox" text:"type button text ..." width:120 pos:[2,30]
 
 	on resizableRol open do
 	(
 		pb.Appearance = pb.Appearance.Button
 		pb.TextAlign = pb.TextAlign.MiddleCenter
 		pb.FlatStyle = pb.FlatStyle.Flat
 		pb.FlatAppearance.CheckedBackcolor = pb.Backcolor.Orange
 	)
 	on tb TextChanged a do pb.text = tb.text
 	on pb TextChanged a do
 	(
 		tb.text = pb.text
 		w = ((dotnetclass "TextRenderer").MeasureText pb.text pb.font).width + 10
 		w = amax minwidth (amin w maxwidth)
 		if w != pb.width do resizableRol.width = (pb.width = tb.width = w) + 4
 	)
 	local commandmode
 	on pb CheckStateChanged a do if pb.Checked then
 	(
 		commandmode = toolmode.commandmode
 		if (obj = pickObject()) != undefined do pb.text = obj.name
 		pb.Checked = off	
 	)
 	else toolmode.commandmode = commandmode
 )
 createdialog resizableRol width:124 height:56
 

Hi,

You can use the Graphics.MeasureString method. But if you need pixel perfect precision, then it will get more involved.

Light

Have you tried setting the .AutoSize property to true? That should do the trick.

Martijn