Visual Studio - Move cursor without losing focus - c#

I have some tool windows in my package and I would like to bring a specific point in the document into view when the user performs some actions in the tool windows.
I've tried the following code:
// Perform selection
TextSelection selection = activeDocument.Selection as TextSelection;
selection.MoveToAbsoluteOffset(offset, false);
// Show the currently selected line at the top of the editor if possible
TextPoint tp = (TextPoint)selection.TopPoint;
tp.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
It does what I want but unfortunately gives focus to the Visual Studio code editor, taking it away from my tool window. This is not good if the user is typing in my tool window and it suddenly moves focus to the editor.
Is there another way to do this without losing focus?

// Store active window before selecting
Window activeWindow = applicationObject.ActiveWindow;
// Perform selection
TextSelection selection = activeDocument.Selection as TextSelection;
selection.MoveToAbsoluteOffset(offset, false);
// Show the currently selected line at the top of the editor if possible
TextPoint tp = (TextPoint)selection.TopPoint;
tp.TryToShow(vsPaneShowHow.vsPaneShowTop, null);
// Restore focus to active window
activeWindow.Activate();

Related

Is this a way to show a modeless dialog from a dockable pane using WPF?

I'm building a Revit plugin. It consists of a dockable pane that (among other elements) has a button. I want to open a new, separate window when a user clicks this button.
At the moment, i create a new Window, but i don't know if that's the right way to go, because now i see two Revit icons on a task bar. I do not have experience as Revit user, i'm new to Revit development, so i'm not sure if this should be the case (two icons) and as silly as it sounds, i do not have admin rights to install random addins and get a feeling of expected user experience.
I create a Window using the following code:
ParametersMissingValueWindow parametersMissingValueWindow = new ParametersMissingValueWindow();
parametersMissingValueWindow.Show();
Based on the understanding of a dockable pane that i have, i think i do not want to create another dockable pane, but just a simple modeless dialog. I wasn't able to find any examples using WPF. Hence, any information whether this is the way to go or help on how to achieve this is highly appreciated.
The Show method takes an optional parent window argument. Specify the Revit main window as the parent window, and your modeless dialogue will be recognised as belonging to the running Revit process. It is accessible from the MainWindowHandle property.
var MyWindow = new MyWindow();
HwndSource hwndSource = HwndSource.FromHwnd(UIApplication.MainWindowHandle);
Window wnd = hwndSource.RootVisual as Window;
if (wnd != null)
{
MyWindow.Owner = wnd;
//MyWindow.ShowInTaskbar = false;
MyWindow.Show();
}
It's not necessary to assign a value to ShowInTaskbar property, but it actually achieves what i wanted to do from the beginning (have only one program open in taskbar), so i left it as part of the solution, but commentted out.
Big thanks to Jeremy Tammik for pointing out the parent property.
You can use WPF to setup a window to use in revit.
MyWPF menu = new menu();
System.Windows.Window wind = new System.Windows.Window();
wind.ShowDialog(); //--> the window shows up and make stuff for revit
if you need the menu to be a dockable one check this source.
Perhaps is not up to date and you will need to adapt the code to the new api.

Use WatiN in windows Form to Automate Clicking and Writing in Internet Explorer C#

Internet Explorer is open and it is showing a webpage. We know there a drop down and a textbox and a button are exist in this page.
I need to select an item from drop down, writing a text in textBox and clicking on that button.
How Can I do these using WatiN by pressing on a button in windows form, I currently add its related libraries and I have add WatiN.Core in using section, but it seems it is not working with windows forms.
This is a very small example but not sure what you mean by "not working" (does it throw an exception, are you able to start the program, when you click the button it fails, you need to be more precise on this).
using(var ie = new IE){
ie.GoTo("http://www.yourwebpage.com");
TextField entry = ie.TextField(Find.ById("myEntryBox"));
Button btn = ie.Button(Find.ById("submit"));
SelectList menu = ie.SelectList(Find.ById("selectList"));
entry.Value ="Hello world");
menu.SelectByValue("2");
btn.Click();
}
if you are having other problems, please, let us know.

How to save and restore the selection of a WebBrowser control?

I am using a Windows.Forms.WebBrowser control as a text editor. To alter the font size of some selected Text, I display a modal window, where the user can make chices concerning the font size and after closing that window, the previously selected text is decorated with the changes. Unfortunately as soon as the modal window opens, the selection in the main windows isn´t visible anymore and I can´t find a way to save and restore it. I can determine the selected Range using
IHTMLDocument2 htmlDocument = (IHTMLDocument2)webBrowser1.Document.DomDocument;
IHTMLSelectionObject currentSelection = htmlDocument.selection;
but since htmlDocument.selectionis readonly, I am not able to set it after the modal closes. All I Can do is call Select() on the main window, but then the caret jumps to the end of the text and nothing is selected.
Any ideas how to solve this?
(I know I could use a ComboBox to alter Font-size, but I need the custom window...for reasons.)
You can use bookmarks. Save selection as bookmark:
var bookmark = document.selection.createRange().getBookmark();
Restore:
var range = document.selection.createRange();
range.moveToBookmark(bookmark);
range.select();

C# & XAML Popup doesn't show in screenshot

I have many popups in a custom GUI application. These popups are window objects, not popup objects. The popups do not show up in a screenshot when using the Print Screen button on the keyboard. Instead, the disabled mainwindow below is all that shows in the screenshot. The popup never flickers or disappears, it just doesn't show in the screenshot.
WindowInstance.IsEnabled = true;
WindowInstance.Refresh();
DisplayPopUpWindow(WindowInstance);
The code in DisplayPopupWindow:
private void DisplayPopUpWindow(Window theWindow)
{
if (theWindow != null)
{
if (theWindow is MessagePopup)
{
// Only show one popup at a time (queue is handled elsewhere)
RemovePopUpsCommand.Execute(true, null);
}
ActiveWindow.IsEnabled = false; // main screen disabled
foreach (KeyValuePair<Window, int> aPopup in popUpWindows)
{
if ((aPopup.Key.IsVisible) && (aPopup.Key.Opacity == 1) && (aPopup.Key != theWindow))
{
// if window is a lower priority then disable it
if (aPopup.Value > displayPriority)
aPopup.Key.IsEnabled = false;
}
}
theWindow.Show();
theWindow.Opacity = 1;
}
}
Is there some property in the XAML that affects whether the window is visible for screenshots? This is a large issue as this also affects some remoting software we use in that popups do not display on the shared screen. Also affects our combobox implementation.
The "popups" are actually their own standalone windows. Some have instances created once during application startup and simply shown/hidden when needed, however, most are created on demand to be displayed. This problem affects both types.
The remoting software used is axeda access remote.
If I remember correctly I had the same problem and I think it was related to setting the popup windows parent to the main window that fixed it, I'd have to look at my code at home to confirm.
So make sure this is correctly set.
EDIT:
Try using this code when you create the Window object:
MainWindowVariable.Owner = Window.GetWindow(this)
You can read more here:
http://msdn.microsoft.com/en-us/library/system.windows.window.owner(v=vs.110).aspx

Word: SyncScrollingSideBySide and ScrollIntoView

One feature of our Word add-in shows two document windows side-by-side. The user can double-click a paragraph in the left-hand document to scroll an associated paragraph in the right-hand document into view. When we do this, we want to re-enable Synchronous Scrolling if it was enabled before the double-click. We're doing something like this:
private void LineUpParagraphs()
{
// Unlock the views so we can scroll them independently.
bool wasSyncEnabled = this.originalDocument.Document.Windows.SyncScrollingSideBySide;
this.originalDocument.Document.Windows.SyncScrollingSideBySide = false;
// Scroll corresponding original paragraph into view.
this.originalDocument.Document.Windows[1].ScrollIntoView(
this.CurrentOriginalParagraph.Range);
// Re-enable synchronous scrolling if it was enabled before.
if (wasSyncEnabled)
{
this.originalDocument.Document.Windows.SyncScrollingSideBySide = true;
}
}
After doing this, the desired range is in view in the original (right-hand for our app) document, but as soon as you scroll either window, the right-hand window jumps back to its original position.
Things we've tried that didn't work:
Set the SyncScrollingSideBySide property on all of the Application windows rather than just one of the two compare documents.
Toggle the property an additional time.
We've resorted to SendKeys to simulate a click on the Synchronous Scrolling button. (If you don't re-enable the sync programatically, then click the button yourself, the right-hand document doesn't jump back to its original position when you scroll). This isn't really an acceptable solution, though--it is inconsistent for example depending on whether our add-in's tab is active. Sometimes it works, sometimes it toggles the sync scrolling an additional time which will annoy the customer. Is there a better way?
Note: The issue occurs when the left-hand document is longer than the right-hand document (the one being scrolled).

Categories