how to select an element while Window form is running

how to select an element while Window form is running

Anonymous
Not applicable
1,601 Views
3 Replies
Message 1 of 4

how to select an element while Window form is running

Anonymous
Not applicable

In the C# window form, there is a button.  

 

What i want is that if I press the button, I can select an element in Revit model, and then return to the window form.

 

I tried  "winForm.hide()", but it still does not allow me to select the element even if the window form is hidden.  

 

Could anyone suggest how to implement this function?  or sample code?

0 Likes
1,602 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable

What I do is place my ShowDialog() call in a while loop and then before my form closes for any reason, I set a public enum property within the form object that indicates whether we are really quiting or not. If not, the enum value provides instructions on which action should be performed.

 

I then call form.Close() and check the enum property using a switch statement. If we are really quiting, break out of the loop and continue as normal, if not, branch off and perform whatever action is required then update the form's state with the required info before looping back to the ShowDialog() call and do it all again.

 

Here's a generic code snippet for demonstration

 

public DialogResult ShowMyPseudoModelessForm()
{
	MyForm form = new MyForm();
	DialogResult res = DialogResult.None;
	Boolean reallyExit = false;

	while(!reallyExit)
	{
		form.PopulateFormData();
		res = form.ShowDialog(); // set the RunCommand property from within the form's code before calling the Close() method

		switch(form.RunCommand) // RunCommand is a property of type: public enum CommandToRun { None, CommandA, CommandB }
		{
			case(MyForm.CommandToRun.CommandA):
				form.RunCommand = MyForm.CommandToRun.None;
				DoCommandA();
				break;

			case (MyForm.CommandToRun.CommandB):
				form.RunCommand = MyForm.CommandToRun.None;
				DoCommandB();
				break;

			case (MyForm.CommandToRun.None):
				reallyExit = true;
				break;

			default:
				reallyExit = true;
				break;
		}				
	}

	return res;
}

 

0 Likes
Message 3 of 4

Anonymous
Not applicable

Thank you for your reply.  In your code 

MyForm form = new MyForm();

if you press "CommandA" button, you call close(), will it dispose the window? if so, the class will not exist, and in the next loop, above code has problem.   Should I add some flag in dispose() function to prevent it running if I press "CommandA" button.  My window form has lots of panels, so I have to save all the data if the "form" object is disposed..

0 Likes
Message 4 of 4

Anonymous
Not applicable

If you display the form using ShowDialog() , the Close() method does not cause the form to be disposed.

 

Refer to the Remarks at MSDN entry for more info: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.close(v=vs.110).aspx

 

The two conditions when a form is not disposed on Close is when (1) it is part of a multiple-document interface (MDI) application, and the form is not visible; and (2) you have displayed the form using ShowDialog. In these cases, you will need to call Dispose manually to mark all of the form's controls for garbage collection.

 

0 Likes