I have written a C# plugin for AutoCAD where the user can enter some information and afterwards the appropriate drawing should be openend.
The actual workflow should be:
I start my plugin in AutoCAD
The user can input some information in a windows form which is on top of the current active drawing
When the user hits the enter button a new drawing should be opend
The form where the user has entered some information should be closed (which works fine)
A new window form should be opend to enter some other information (not the same window as the first one) BUT IN THE NEW DRAWING
The problem is that I can correctly close the first window and correclty open the new drawing. Like this:
DocumentCollection documentCollection = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager;
if (File.Exists(absoultePathOfDrawing))
{
Document newDrawing = documentCollection.Open(absoultePathOfDrawing, false);
Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument = newDrawing; // this sets the new drawing as the active one ==> is on top
}
Dispose(); // closes the first form
DialogResult = DialogResult.OK; // tells my applciation that the first window was successfully closed
The form closes correctly and afterwards I try to open the new form with:
if (result == DialogResult.OK)
{
MessageBox.Show("Test");
}
But now the new drawing is on top and behind is the old drawing. When I switch back to the old drawing, the new MessageBox will be shown but actually this should be shown in the new drawing, because I set the active document to the new drawing.
What am I doing wrong?
I have found the solution.
I had to load my plugin with the following option:
[CommandMethod("PluginName", Autodesk.AutoCAD.Runtime.CommandFlags.Session)]
Without this my plugin is only valid in one document (the one where I started my plugin)
Related
I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.
For now in order to launch second window I use:
var editorWindow = new EditorWindow();
editorWindow.Activate();
The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.
First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so
var editorWindow= new EditorWindow();
editorWindow.Activate();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted Attempted to read or write protected memory eror.
I tried to do it in the 2nd Window .cs file like this:
this.InitializeComponent();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted the same error
So how can I do this properly?
If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:
var editorWindow= new EditorWindow();
editorWindow.Activate();
this.Close();
If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.
I have an application written in C# using Microsoft.Office.Interop.PowerPoint. This application opens a PowerPoint presentation file to the user. The user interacts with the file. After applying some modifications, the user submits the file through the application submit button.
The Problem:
When user modifies the content of the presentation, for example changes font color of the text to red, by using a dialog and keeps the dialog open without clicking on "Apply" or "OK" button and thereby submits the file by clicking on application submit button, those dialog done changes aren't reflected in the submitted file and hence such changes can't be tracked of.
I somehow want to alert a user to close any open dialog before clicking on submit button.
I do this easily on Word and excel files by checking Exception on saving such files because Word and Excel throws exception on using save method if there is any dialog open, like the Following:
try{
document.Save();
}
catch (Exception e){
//Alert user here here
}
but this doesn't work for PowerPoint files. I tried the following:
PowerPointApplication application = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations presentations = application.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation =
presentations
.Open(file, WithWindow: Microsoft.Office.Core.MsoTriState.msoTrue,ReadOnly:Microsoft.Office.Core.MsoTriState.msoFalse,Untitled:Microsoft.Office.Core.MsoTriState.msoFalse);
The following is triggered on Submit button click event:
try
{
presentation.Save();
}
catch (Exception e)
{
//alert user here
}
Is there a way to track if there are any open dialog boxes?
Typically if any dialog window is displayed to a user the code is blocked because the dialog window uses the main thread for running. Do you use multiple threads in the code?
Anyway, you can use Windows API functions for detecting the active window, see GetActiveWindow which retrieves the window handle to the active window attached to the calling thread's message queue. To get the handle to the foreground window, you can use GetForegroundWindow. To get the window handle to the active window in the message queue for another thread, use GetGUIThreadInfo.
After retrieving the window handle you can use the GetWindowText function which copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied.
new to c# wandered how i can use a form to open a new form whilst also closing that initial form. I have a form that a user needs to fill out and click login on to move forward which at this point if the information is correct should open the next form and close the current one. All i seem to get is both forms closing. i can do it without closing the initial form but then it is obviously still open in the background. The form i'm trying to open is form encryption and the one 'im trying to close is form1 any help is much appreciated.
here's my code:
if (maskedTxtLogin != null)
{
FormEncryption Encryption = new FormEncryption();
Encryption.Show();
this.Close();
}
else
{
MessageBox.Show("Please input your initials");
}
I'm developing a WPF application that's meant to live in the tool tray, so it doesn't involve any windows. Right-clicking the tool tray icon brings up a menu with a Configure Report Path... option, and I'd like to display a folder browser dialog to the user when this is clicked:
What I'm finding is that when the option is selected, a dialog opens and immediately closes unless I assign some window to Application.Current.MainWindow and show it before opening the dialog. This is the code I'm using:
public CounterIconViewModel(IMessenger messenger)
{
void ConfigureReportPath()
{
// Application window must be created and displayed.
Application.Current.MainWindow = new Window();
Application.Current.MainWindow.Show();
var browseDialog = new VistaFolderBrowserDialog { ShowNewFolderButton = false };
if (browseDialog.ShowDialog() != true)
{
return;
}
// (Separate issue) Command doesn't execute unless I comment out the line below.
//messenger.Send(browseDialog.SelectedPath, "ReportPath");
}
ConfigureReportPathCommand = new RelayCommand(ConfigureReportPath);
ExitApplicationCommand = new RelayCommand(Application.Current.Shutdown);
}
In this case I'm using VistaFolderBrowserDialog from Ookii.Dialogs.Wpf, but I've tried the same thing with another WPF browser dialog and notice identical behaviour.
Is there a reason why a browser dialog seems to require a window to be displayed to remain open, and any workarounds?
Update
I've found that if I initialize and pass an instance of Window to browseDialog.ShowDialog, the dialog remains open without me having to assign the main application window and display it:
if (browseDialog.ShowDialog(new Window()) != true)
I don't understand why this works. I'll post this as an answer if no others appear so that at least people in a similar situation are aware of this workaround.
Update 2
The other dialog I tested it with was CommonOpenFileDialog from Microsoft.WindowsApiCodePack-Shell:
var browseDialog = new CommonOpenFileDialog { IsFolderPicker = true };
browseDialog.ShowDialog();
My tool tray icon displays a rich tool-tip (a custom UserControl) if I hover over it, and with this browser dialog I found that:
If I hover over the icon to make the tool-tip display, then the browser dialog works fine when I try to open it on the first and every subsequent attempt.
If I try to open the browser dialog before displaying the tool-tip display, the browser dialog opens and closes immediately on the first try, but then remains open on every subsequent attempt.
This dialog also accepts a Window instance in ShowDialog but it makes no difference if I pass one or not.
My workaround (initializing and passing a blank window to the Ookli dialog browser) seems to work fine regardless of whether I first bring up the tool-tip, so I'm sticking with that for the time being.
Based on this post, I managed to open a new tab, but when I try to navigate in the new tab, the navigation occurs in the old tab.
I saw that I should use this:
driver.switchTo().window(windowName);
but what is windowName?
You have to use window handle function here. You asked for a solution in c#. I used java with selenium webdriver. They both would use similar concepts.
Here is a sample working code in java:
String parentHandle = driver.getWindowHandle(); // get the current window handle
System.out.println(parentHandle); //Prints the parent window handle
String anchorURL = anchor.getAttribute("href"); //Assuming u are clicking on a link which opens a new browser window
anchor.click(); //Clicking on this window
for (String winHandle : driver.getWindowHandles()) { //Gets the new window handle
System.out.println(winHandle);
driver.switchTo().window(winHandle); // switch focus of WebDriver to the next found window handle (that's your newly opened window)
}
//Now your driver works on the current new handle
//Do some work here.....
//Time to go back to parent window
driver.close(); // close newly opened window when done with it
driver.switchTo().window(parentHandle); // switch back to the original window
Hope this helps!