Notifications
Clear all

[Closed] maxscript/dotnet: rightclick menu in listview

Hello everybody,
I write on a script with that I get a listview, and this list all my scripts what I have in a folder. It looks like this, at the moment:


 (
 clearlistener()
 global start_dir = "\\\\path\	o\\scripts\\"
 
 fn get_all_files start_dir = (
 	global cur_files = getFiles (start_dir + "*.ms")
 	)
 
 rollout ListScripts "List Scripts" width:250 height:210 (
 		
 	local ProgramName = "Search Material and Textures"
 	
 	groupBox grpList "Collection From All Scripts:" pos:[5,5] width:240 height:200
 		dotNetControl mlbxList "system.windows.forms.listView" pos:[15,25] width:220 height:170
 	
 	-----------------------------------------------
 	--resize statment
 	-----------------------------------------------
 	on ListScripts resized newSize do (
 		grpList.width=newSize[1]-10
 		grpList.height=newSize[2]-10
 			mlbxList.width=newSize[1]-30
 			mlbxList.height=newSize[2]-40
 		
 			try (mlbxList.columns.item[0].width = newSize[1]-34) catch ()
 		)
 	
 	on ListScripts open do (
 		mlbxList.HeaderStyle = none
 		mlbxList.columns.add "Scripts" 216
 		mlbxList.view=(dotNetClass "system.windows.forms.view").details
 		mlbxList.FullRowSelect=true
 		mlbxList.GridLines=false
 		mlbxList.MultiSelect=false
 		cb = ((colorman.getColor #background)*255+20) as color
 		mlbxList.BackColor = (dotNetClass "System.Drawing.Color").fromARGB cb.r cb.g cb.b
 		cf = ((colorman.getColor #text)*255+30) as color
 		mlbxList.ForeColor = (dotNetClass "System.Drawing.Color").fromARGB cf.r cf.g cf.b
 		
 		get_all_files start_dir
 		
 		all_files = #()
 		
 		for i = 1 to cur_files.count do (
 			li = dotNetObject "System.Windows.Forms.ListViewItem" (getFilenameFile cur_files[i] as string)
 			li.UseItemStyleForSubItems=true
 			colAdd=cb.r+(if (mod i 2)==0 then -20 else 20)
 			li.BackColor=li.backcolor.fromARGB colAdd colAdd colAdd
 			
 			append all_files li 
 			)
 		
 		mlbxList.items.addRange all_files
 		mlbxList.Update()
 			
 		)
 
 		 on mlbxList MouseDoubleClick arg do (
 			hit=(mlbxList.HitTest (dotNetObject "System.Drawing.Point" arg.x arg.y))
 			all_files=hit.item.Index  
 			index = mlbxList.items.item[all_files].index + 1
 			fileIn cur_files[index]
 			)
 		
 	)
 
 try ( destroyDialog ListScripts )
 	catch ( MessageBox "Dialog not found!" )
 
 createDialog ListScripts style:#(#style_titlebar, #style_border, #style_sysmenu, #style_minimizebox, #style_resizing)
 	
 )
 

Now I want to write a rightclick menu, and here for example with the commands: edit, run, open in explorer.

Is this possible in maxscript with dotnet?

10 Replies

i use the following construct for right click context menus

struct lv_context_menu 
 (
 	fn SelectObj sender arg =
 	(	
		lv = sender.Parent.SourceControl; -- gets you the access to the dotnet control
 		-- your stuff here
 	),	
 	fn null = (),
 	fn FlipMap sender arg =
 	(	
 		-- your stuff here
 	),	
 	fn ClearAllFlips sender arg =
 	(	
 		-- your stuff here
 	),
 	names = #("&Select Objects","-", "&Flip Map", "&Clear All Flips"),
 	eventHandlers = #(SelectObj, null, FlipMap, ClearAllFlips),	
 	events = #("Click", "Click", "Click", "Click"),
 	
 -- build the menu from all the bits		
 	
 	fn GetMenu =
 	(	
 -- create the base menu			
 		
 		cm = (dotNetObject "System.Windows.Forms.ContextMenu");
 		for i = 1 to names.count do
 		(	
 -- add menu item 			
 			mi = cm.MenuItems.Add names[i];
 			
 -- add event handler routines			
 			
 			dotNet.addEventHandler  mi events[i] eventHandlers[i];
 			
 -- stop gc() deleting the event handlers				
 			
 			dotNet.setLifetimeControl mi #dotnet;
 		)	
 -- set to the listview
 		cm;
 	)
 )

then in the list initializer

cm = lv_context_menu()
 lv.ContextMenu = cm.getmenu();

where lv is your listview control

Thank you Klunk for you fast answer. This works great! I only have one question more:
In the first function you have this command:

fn SelectObj sender arg = (	
   				lv = sender.Parent.SourceControl; -- gets you the access to the dotnet control
   			),	

How I get from this now the index from the selected Item?

Edit: ok I think I have it, so I get the index: lv.SelectedItems.Item[0].index

Thanks again!

Edit2: when someone need it, here is the complete script:

https://github.com/jb-alvarado/ListScriptsInFolder/

Take a look this tool

thanks gazybara,
this looks also very interesting, and I belief that it has more good dotnet commands!

1 Reply
(@gazybara)
Joined: 11 months ago

Posts: 0

Read the manual (help pdf) and see the code. For now this tool works fine but of course code can be optimized more.

Can I ask here a other question? How I can build now with that example what I get a submenu in the right click menu?

1 Reply
(@gazybara)
Joined: 11 months ago

Posts: 0

On this forum you can find meny examples about .net contextmenu. Denis explained everything in this example

Thanks gazybara,
I will make a deeper look in that. I know in this forum is many threads, but is not so easy to found the right code what I need. Especially when my code what I had already looks different.

1 Reply
(@gazybara)
Joined: 11 months ago

Posts: 0

We all have to face with our demons

Ok I think slowly I come in :). But now I also have a other question: How can I catch a mouse click outside from a dotnet listview item?

I have this code:


 on mlbxList MouseClick arg do (
 			hit=(mlbxList.HitTest ( dotNetObject "System.Drawing.Point" arg.x arg.y ))
 			all_files=hit.item.Index
 			index = mlbxList.items.item[all_files].index + 1
 			ext = "no"
 			if ( getFilenameType cur_files[index] == ".mse" ) then (
 				ext = "mse"
 				) else if ( getFilenameType cur_files[index] == ".ms" ) then (
 					ext = "ms"
 					)
 
 			cm = lv_context_menu()
 			mlbxList.ContextMenu = cm.getmenu( ext )
 			)
 

And I want that my “ext” variable set to “no”, when I click in the listview, but not on a item.

Edit:

I put it now in a mouseUp event, and that works

on mlbxList mouseUp sender args do (
			dragFlag = false
			
			hit=(mlbxList.HitTest ( dotNetObject "System.Drawing.Point" args.x args.y ))
			
			if hit.item != undefined do (
				all_files=hit.item.Index
				index = mlbxList.items.item[all_files].index + 1
				
				if ( getFilenameType cur_files[index] == ".mse" ) then (
					ext = "mse"
					) else if ( getFilenameType cur_files[index] == ".ms" ) then (
						ext = "ms"
						)
				)

			cm = lv_context_menu()
			mlbxList.ContextMenu = cm.getmenu( ext )
			)