Notifications
Clear all

[Closed] dotNet UI

Hi Guys, porting to dot net from maxscript and I have written a form in dot net and will have max functions to work with the controls. I need to add an image to my form, but am having a little difficulty getting the code right. Below is a simplified version of the form and I am trying to add an image to it but it is obviously incorrect and I was hoping that I could get some input from people. I am missing some syntax and I have found the resources that I have found are helpful, its just my learning of it that needs adjustment. There are references to bmp files, but I am sure you will see round this at this point. :bowdown: Thanks very much

CODE:

iconDir = @“U:/DLimages/RawFiles/ScriptButtonImages/”

myBitmapClass = dotNetClass “System.Drawing.Bitmap”
rectClass=dotNetClass “system.drawing.rectangle”
bmpLens = dotnetobject bitmapClass (iconDir + “ui icons_handa_lens_90x45.bmp”)

– Create Form
form = dotNetObject “System.Windows.Forms.Form”
form.Size = dotNetObject “System.Drawing.Size” 330 670
texBrush = dotnetobject “System.Drawing.TextureBrush” form
form.Text = “An image form”
form.TopMost = true
TheFormBorderStyle = dotNetClass “System.Windows.Forms.FormBorderStyle”
TheFormBorderStyle = TheFormBorderStyle.FixedDialog
form.ShowInTaskbar = false
form.MinimizeBox = true
form.MaximizeBox = false
form.helpbutton = true

form.controls.add bmpLens

form.showDialog()

Should read:

bmpLens = mybitmapClass.fromFile (iconDir + "ui icons_handa_lens_90x45.bmp")

I also added a button, and removed your control, and applied the image to the button.

myButton = dotNetObject "Windows.Forms.Button"

myButton.size = dotnetObject "System.drawing.size" 30 30
myButton.location = dotNetObject "System.Drawing.Point" 50 50
myButton.image = bmpLens
...
...
form.controls.add myButton
2 Replies
 lo1
(@lo1)
Joined: 11 months ago

Posts: 0

The first form was also fine, Bitmap has a constructor that has a filename.

(@alexanderh)
Joined: 11 months ago

Posts: 0

In Max 2011 I got this error:

-- Error occurred in anonymous codeblock; filename: C:\Users\~Redacted~\; position: 195; line: 5
-- Unable to convert: undefined to type: String

My change eliminated this.

Hi Guys, thanks for the info, I will add to my development.

During the time I was waiting, I did a slight hack which has some result, but I have not got it working properly as the image is tiled and not in the right position. I have seen there are tile options, but I would rather do it properly. I have the code below, so please feel free to tear it apart and tell me where it is wrong. You will see that I used a groupbox and added a background image. :wip:

Adding an image to a button is not what I am looking to do in this case, but the code for doing it is very welcome and I will store that little gem for later.

Great fun being had. Even if I am going slightly mad :banghead:

Thanks again.

CODE:

iconDir = @“U:/DLimages/RawFiles/ScriptButtonImages/”

myBitmapClass = dotNetClass “System.Drawing.Bitmap”
rectClass=dotNetClass “system.drawing.rectangle”
bmpLens = dotnetobject myBitmapClass.fromFile (iconDir + “ui icons_handa_lens_90x45.bmp”)

grpBx_ImgBox = dotNetObject “System.Windows.Forms.GroupBox”
grpBx_ImgBox.width = 100
grpBx_ImgBox.height = 60
grpBx_ImgBox.BackgroundImage = bmpLens
grpBx_ImgBox.Location = dotNetObject “System.Drawing.Point” 5 5

– Create Form
form = dotNetObject “System.Windows.Forms.Form”
form.Size = dotNetObject “System.Drawing.Size” 330 670
–texBrush = dotnetobject “System.Drawing.TextureBrush” form
form.Text = “An image form”
form.TopMost = true
TheFormBorderStyle = dotNetClass “System.Windows.Forms.FormBorderStyle”
TheFormBorderStyle = TheFormBorderStyle.FixedDialog
form.ShowInTaskbar = false
form.MinimizeBox = true
form.MaximizeBox = false
form.helpbutton = true

form.controls.add grpBx_ImgBox

form.showDialog()

1 Reply
(@alexanderh)
Joined: 11 months ago

Posts: 0

Add:


grpBx_ImgBox.BackgroundImageLayout = grpBx_ImgBox.BackgroundImageLayout.none

You can also use “Center”, “Strectch” and “Zoom”

Are you just trying to display an image for viewing, or does it need to be on a control?
If so, try this:

imgFrame = dotNetObject "pictureBox" --"system.windows.forms.pictureBox"
imgFrame.bounds = dotNetObject "System.Drawing.Rectangle" 90 45 100 100
imgFrame.image = bmpLens
form.controls.add imgFrame

Thanks for the replies, they are very helpful. At the moment, the image is just there as a visual prompt and does not need to be part of a control (yet!) so can adapt what has been posted. I have most of the ui sorted now, so can put up a version that may be of use to others. As with all new languages I have learned, I have laid out the code in a very long way and should have used more streamlined methods, but that will be the next iteration where I use a factory for all the repeated controls instead of a long list in the script. Build big and whittle down as my Dad says. Nyuk

Also, the code below is the sum of the input from myself and those who have been generous enough to help me out, so I think its only fair to pay it forward. You will see that there are images, one in a group box and one inside the form (without border). I hope that others will find it useful.

Thanks again

EDITED POST: Below is the simple code for a UI with an image in a group box. It may not be the best solution, but it works for me right now. In the example below, you will need to put in your own location to the directory that the images are in and a file name with extension in the line beginning with bmpLens.

CODE:

–example of location as required in line below “C:/DLimages/RawFiles/ScriptButtonImages/”
iconDir = @“location of image directory”

myBitmapClass = dotNetClass “System.Drawing.Bitmap”
–variable to create a dotnet object of the image to be used
bmpLens = dotnetobject myBitmapClass (iconDir + “ui icons_handa_lens_90x45.bmp”)

grpBx_ImgBox = dotNetObject “System.Windows.Forms.GroupBox”
grpBx_ImgBox.width = 100
grpBx_ImgBox.height = 60
grpBx_ImgBox.BackgroundImage = bmpLens
grpBx_ImgBox.BackgroundImageLayout = grpBx_ImgBox.BackgroundImageLayout.center
grpBx_ImgBox.Location = dotNetObject “System.Drawing.Point” 5 5

imgFrame = dotNetObject “pictureBox” –“system.windows.forms.pictureBox”
imgFrame.bounds = dotNetObject “System.Drawing.Rectangle” 5 100 90 45
imgFrame.image = bmpLens

– Create Form
form = dotNetObject “System.Windows.Forms.Form”
form.Size = dotNetObject “System.Drawing.Size” 330 670
–texBrush = dotnetobject “System.Drawing.TextureBrush” form
form.Text = “An image form”
form.TopMost = true
TheFormBorderStyle = dotNetClass “System.Windows.Forms.FormBorderStyle”
TheFormBorderStyle = TheFormBorderStyle.FixedDialog
form.ShowInTaskbar = false
form.MinimizeBox = true
form.MaximizeBox = false
form.helpbutton = true

form.controls.add grpBx_ImgBox
form.controls.add imgFrame

form.showDialog()

Ok, so I have now got the UI laid out, the spinners work with mouse drags and the ui images are sitting well.
I am having a little trouble with a combo box. I have a set of items which the user can choose from, but I need to understand how the item chosen can get added to the list box I have once the user clicks on their choice. In the maxscript version I wrote previously I did this:

fn comLensFn len1 len2 =(return (len1 as integer) – (len2 as integer))

–ADD DROPDOWN SELECTION TO LIST
on lens_dd selected i do
(
local lensLstDd = lensList.items
local lensDDwn = lens_dd.selection
if lensDDwn == 1 do(print “Not a valid value”)
if lensDDwn !=1 do (appendIfUnique lensList.items (lens_dd.selected as string); lens_dd.selection = 1)
qsort lensLstDd comLensFn
lensList.items = lensLstDd

	)

which added the item if it was unique and reset the drop down to its first index item. I need to do this via the dot net way and have been looking at the Bobo list but am getting a little confused. I saw that there is a method for the dropdown:

on SelectedIndexChanged e do()

so think that this may be the way forward. Any pointers welcome. :lightbulb

Will post a small version as I develop.

Thanks guys

UPDATE:

Ok, have managed to get the item from the drop down into the list, which was easier than expected.

created a function:

fn addLensFromDDwn =
(
–add selected dropdown list item to list box

lstBx_Lens.items.Add drpDwn_LensType.SelectedItem

)

and an event handler

dotNet.addEventHandler bttn_addLens “Click” addLensFromDDwn

Now I need to sort out how to add item if its unique and sort by numeric value (smallest at top) Did it in maxScript, so should be ok, but any pointers welcome

Woo hoooo

1 Reply
(@alexanderh)
Joined: 11 months ago

Posts: 0

Copy the data to a Max array.
Sort the array, and re-write the data fields.

I figured out how to add an item if it is unique by using the .Contains property:

lstBx_Obj.Items.Contains(ObjDDwnItem)

which added the item if it was unique.

I also used the .Sorted property of the list box:

lstBx_Obj.Sorted = true

Whilst they both work, the .Sorted property only sorts by character count. So 5 would go in before 10. 10 would go in before 20, but 110 would go in before 35, which is not what is required.

Previously in a full maxscript version (no dot net), I did copy the items to an array and then made the items equal to the array after I had sorted it but the dot net listbox.items will not allow for this. Currently looking at creating a list view instead of a listbox as it has more options for the sorting. Will post back results.

It is just a matter of a quick loop.

Sudo:
for item in listbox, copy item to array, delete item.
–Listbox should be empty
sort array –as required
for item in array create item in list box

You can also add an array of values by tying list.items.addrange myArray

ok, I’ve done some playing for you.

startItems = #(1,105,999,10,20,35,110)	--Starting Data
		list1.items.addRange startItems	--Add the data to the list box
		
		tempArr = #()	--Temp array to store the collection of data in the list box
		for i in 0 to list1.items.count-1 do appendIfUnique tempArr (list1.items.item i)	--Collect all the values in the list box
		list1.items.clear()	--Wipes the list box
		sort tempArr	--Default sort method in MS
		list1.items.addRange tempArr	--Add the new array of data

The listbox should finish with all the fields in order as you’d expect.
Note though that I did remove any duplicate data in the copy to tempArr

Thanks for the reply. I think I may have written the last post incorrectly. What I have is a dot net form (no max controls) and the object I want to populate with unique items in numerical order is a listview object.

I have managed to get it to work, the code is below:

TEST UI CODE BELOW:

–dot net list view with drop down that updates if unique and in numerical order
(
– Create Form (this is sort of like the rollout creation in maxscript) and set properties
listViewForm = dotNetObject “System.Windows.Forms.Form”
listViewForm.Size = dotNetObject “System.Drawing.Size” 200 350
listViewForm.Text = “listview updater”
listViewForm.TopMost = true
lvFormBorderStyle = dotNetClass “System.Windows.Forms.FormBorderStyle”
listViewForm.FormBorderStyle = lvFormBorderStyle.FixedDialog
listViewForm.ShowInTaskbar = false
listViewForm.MinimizeBox = true
listViewForm.MaximizeBox = false
listViewForm.helpbutton = true

--Drop down with 
	drpDwn_A = dotNetObject "System.Windows.Forms.ComboBox"
	drpDwn_A.location = dotNetObject "System.Drawing.Point" 5 5				
	drpDwn_A.Width = 85
	drpDwn_A.items.addRange (#("Choose", "1000", "1200", "1400", "1600", "1800", "2000", "2200", "2400", "2600", "2800", "3000"))
	drpDwn_A.SelectedIndex = 0
		
	theListView = dotNetObject "System.Windows.Forms.ListView"
	theListView.Size = dotNetObject "System.Drawing.Size" 180 200
	theListView.Location = dotNetObject "System.Drawing.Point" 5 40
	theListView.view=(dotNetClass "system.windows.forms.view").details
	theListView.columns.add "Values" 180
	theListView.GridLines = true
	
	listViewItems = #()
	--//FUNCTION TO COMPARE AND SORT VALUES 
	fn compareFn len1 len2 =(return (len1 as integer) - (len2 as integer)	)			
		
	fn addToListViewFromDDwn =
	(
		local LstDdArr = theListView.items
		local DDwnItem = drpDwn_A.SelectedIndex			
		local strArray = #()
		
		for i = 1 to listViewItems.count do
		(
			append strArray listViewItems[i] --lstDdArr.item[i].text
		)
		
		if drpDwn_A.SelectedIndex == 0  then (print "Not a valid choice") 
		
		else
		(		
			tempArr = #()	--Temp array to store the collection of data in the list box
			index = finditem strArray drpDwn_A.items.item[DDwnItem] 

			if index == 0 then(	appendifunique listViewItems drpDwn_A.items.item[DDwnItem])
					
			drpDwn_A.SelectedIndex == 0			
			
			for i = 0 to (theListView.items.Count - 1) do	(	theListView.items.Remove theListView.items.item[0]	)
			
			qsort listViewItems compareFn
			
			for each in listViewItems do
			(
				theListView.items.add each
			)				
			theListView.Update()
		)
	)		
	
--Add Controls	
	listViewForm.Controls.Add drpDwn_A			
	listViewForm.Controls.Add theListView	
--Event Handlers
	--event to add drop down choice from lens drop down
	dotNet.addEventhandler drpDwn_A "SelectedIndexChanged" addToListViewFromDDwn
	
 listViewForm.show()

)

have managed to get a combo box that updates a listview with unique items and in numerical order. I also have a spinner which allows for values to also be added to the listview. Somewhere a.long the way, dot net is suppressing the trailing zeros, which is not what I want. If the value is 25.78, it will include all digits, just not the zero. Any ideas?

Thanks

1 Reply
(@alexanderh)
Joined: 11 months ago

Posts: 0

Spinner or Numeric UpDown?
Numeric UpDown has a decimal places property.
ex: num1.DecimalPlaces = 3

I am using a dot net numeric up down and I already have the decimal places set to 2. If the number ends in 12.36, then the full number is added to the listview, but if it is 24.60 in the updown, then the trailing zero is removed and the value shows up as 24.6 in the listview, which is not what is required. I need the full value, even if it ends in zero.

Cheers and thanks for the response.

Page 16 / 18