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();
Related
I'm trying to retrofit an old C# application that was made using the old Adobe Acrobat API. One of the features of this application was to change the icon of a button and update the caption of the button as well. The caption was overlayed on top of the icon.
Currently, I'm using iText 7 and C# to program the application. I have most of the functionality ready, but I can't figure out how to add a caption and icon to a button. I was able to figure out how to add a caption and an icon seperately, but when I try to combine the code snippets, the icon seems to cover the caption.
PdfButtonFormField button = (PdfButtonFormField)pdfForm.GetField(fieldName);
button.SetImage(tempFileLOGO);
var buttonObj = button.GetPdfObject();
buttonObj.GetAsDictionary(PdfName.MK).Put(PdfName.TP, new PdfNumber((int)PushButtonLayouts.LABEL_OVER));
pdfForm.ReplaceField(fieldName, button);
var fields = pdfForm.GetFormFields();
foreach (var f in fields)
{
if (f.Key.StartsWith("sign_"))
{
f.Value.SetValue(fieldValue);
}
}
This code causes the icon to be placed on the button, but I don't see the caption text (variable name "fieldValue")
Any help would be appreciated!
Thanks!
As far as I can see from iText code, if a button has an image its text is always ignored while the button appearance is generated.
So there is no standard way to do it.
I can suggest you to modify an image first, adding the needed text over it as it is done in this example https://itextpdf.com/en/resources/books/best-itext-7-questions-stackoverflow/how-add-text-image
And then set a new one using the setImage method
I have a simple StatusStrip with one ToolStripStatusLabel in it. Text in label can be quite long, so I have prefered to display it cutted.
I have set ToolStripStatusLabel properties: Spring=true and TextAlign=MiddleLeft. I didn't want to set StatusStrip's property LayoutStyle = ToolStripLayoutStyle.Flow, because with ToolStripLayoutStyle.Flow the Text will be overridden with triangle(for resizing).
The Text property is set directly after InitializeComonents() and is displayed as expected - cutted.
If I do minimize(to taskbar) and then restore the window, the text will not be displayed at all. If I make window wider I can see the text, and if I bring the window to the initial size I still can see the text, cutted as expected.
I have and will post my solution, but I would ask you, whether you have any elegant one?
The solution I have found is quite simple. Just handle the window's restore event(there is no such event, but there is a workaround:
Is there an event raised in C# when a window is restored?) and reset the text in ToolStripStatusLabel:
var txt = tslabel.Text;
tslabel.Text = " ";
tslabel.Text = txt;
I am trying to create a popup but when it is open it is still possible to use the tab key to switch the focus to an element in the background (e.g. to a button and use space to press is). The only way I found until now is to check on every lostFocus event (which also fires for every element contained in the Border element) and check if the focus is now in a element inside the Border. If not I manually set the focus.
Is there a nicer way to keep the focus within the Border (or a Grid,...)
I'm working on a Windows 8 App.
Do you mean that using a Modal Dialog with Form.ShowDialog(Owner) still allows you to focus the parent components with Tab?
Can you give a sample of your code call?
Form2 form = new Form2(); //Make an instantiation of your Form
form.ShowDialog(); //ShowDialog()!!! NOT form.Show()!!! Or anything else :/
A few ideas:
Set Enabled to False on the background visual tree, though that might change the way things look if you still want to show them partly
Set IsHitTestVisible to False to disable pointer input
Use RenderTargetBitmap.Render() if targeting Windows 8.1 to render the content of the background to an image and simply replace all that visual tree with an image of it
I have a form that contains WebBrowser control.
I need to change the cursor to WebBrowser.
I try
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.TopLevelControl.Cursor = Cursors.WaitCursor;
The cursor changes form only, but not for WebBrowser.
How can I change the cursor in WebBrowser control?
Add a reference to your solution to "mshtml.dll". After you load your Document, try this:
IHTMLDocument2 doc = (webDocument1.Document.DomDocument) as IHTMLDocument2;
IHTMLStyleSheet style = doc.createStyleSheet("", 0);
style.cssText = #"body { cursor: wait; }";
Please bear in mind that the result depends also on the way you load the web page (load a local/embedded file, set the DocumentStream, etc.).
Reason for Failure:
you are setting the Cursor for Form Instead of WebBrowser Control as below:
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
thats why it is setting the cursor to Form instead of WebBrowser Control.
you can set the Cursor for any Control as below:
controlName.Cursor=System.Windows.Forms.Cursors.WaitCursor;
but WebBrowser control does not support the Cursor property.
hence you can not set this property for WebBrowser Control.even if you set it, it does not give compile errors but throws following Runtime Error.
WebBrowser Control does not Support the Cursor Property.
Try this:
Icon ico = new Icon(#"C:\temp\someIcon.ico");
this.Cursor = new Cursor(ico.Handle);
The static class System.Windows.Forms.Cursors contains all system cursors.
To switch back to the default system cursor, use this:
this.Cursor = System.Windows.Forms.Cursors.Default;
If you set the cursor of a Form with a WebBrowser control in it, the Form will show the wait cursor, but the browser will not, because the browser sets the cursor on it's own corresponding to the HTML content. For example if you move the mouse over a hyperlink, Internet Explorer changes the cursor to the hand cursor. Also JavaScript and CSS can modify the cursor. So there is no way to set a WaitCursor while Internet Explorer is controling the cursor.
But I found a trick to do this with one line of code!
If you do a lengthy processing and want to display the wait cursor meanwhile, you can use this code to turn it on and off:
public void SetWaitCursor(bool b_Wait)
{
Application.UseWaitCursor = b_Wait;
// The Browser control must be disabled otherwise it does not show the wait cursor.
webBrowser.Enabled = !b_Wait;
Application.DoEvents();
}
The trick is disabling the browser control and it will show the Wait Cursor because a disabled Internet Explorer does not control the cursor anymore.
So your final code will look like this:
SetWaitCursor(true);
doLenghtyProcessing();
SetWaitCursor(false);
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();