print html in c# with invisible browser but with visible print dialog - c#

I want to print a html file, though i do not want the browser to be visible, but i want the print dialog to be visible.
Thanks,
Horea

found the problem.
i have to wait for the event DocumentLoaded (or smt like that) to be fired, and then try the print.
thanks

So, just hide the WebBrowser control on your form:
myWebBrowser.Visible = false;
Of course, you can also set this property in the Designer if you never want the control to be visible.

Related

How do I show/activate the text cursor in open document? [C#, EnvDTE]

I'm trying to set the cursor in an open document, but it doesn't show. I can see that the line is "marked" and I can "navigate" the lines, but the cursor is not shown, and thus I'm not able to write anything. Also it seems like the document doesn't really fully load, since guidelines and the navigationmap is not shown either.
Which makes me believe that the focus isn't being set completely inside the document.
I have confirmed that the focus indeed is not set in the document in the window, since If I have the output window focused before, it's still focused after the window.Activate() method has been called.
I've used the common way of opening the document through ProjectItem.Open(Constants.vsViewKindCode), activating it, and using the TextSelection.GotoLine(1,false) method.
This correctly shows the document and sets the line correctly, but I have to manually click inside the document for the cursor to appear.
The code I have:
Window window = projItem.Open(Constants.vsViewKindCode);
window.Activate(); <----- this does not focus the window.
TextSelection textSelection = window.Document.Selection as TextSelection;
textSelection.GotoLine(1, false);
I want to not have to manually click inside the document for it to load completely and for me to be able to write in it.
Hope someone can help me.
Oh well, another issue I had to solve by myself... Two for two. I guess people don't know that much in here like I'd hoped. Oh well.
Anywho, I discovered that if you use DTE.ItemOperations.OpenFile(path); and nothing else,
the file will receive focus correctly, just like it should have when you use .Activate().

How to force a focus on a control in windows forms

I am trying to focus a "search" textbox control in my windows forms application. This textbox is inside a user control, which is inside a panel which is inside a windows form (if it is important).
I tried 3 methods which I could find:
// 1
this.ActiveControl = myTextBox;
// 2
myTextBox.Focus();
// 3
myTextBox.Select();
Neither of them seems to work. I mean for example when I try the first one, active control is really set to myTextBox, but when I try to write something on keyboard, textbox doesn't accept it and I have to first click inside the textbox to get focus. This is same with all of the methods.
Am I missing something?
Ok, finally found the answer:
As I said my textbox is inside user control which is inside panel which is inside a form.
When I need my user control I add it to panel. To get focus on my textbox I have to firstly focus my user control so something like this:
In my top Form:
panel.Controls.Add(myUserControl);
myUserControl.Focus();
and then in my user control:
myTextBox.Select();
Note that if I used: myTextBox.Focus() it wouldn't work (don't know why). Also if I used myUserControl.Select() instead of myUserControl.Focus() it wouldn't work either.
This seems to be the only combination which works.
You could do these logic steps to set your control to be the focus:
your_control.Select();
your_control.Focus();
Enjoy! :)

Add button to copy text in MessageBox

I have a program that contains a list of names, and a button that displays a random name in a MessageBox. Is there any way I can add a button "Copy" next to the OK to the MessageBox, that when clicked copies the name and then closes?
If the above isn't possible, is there a way to enable copying the text in a MessageBox?
Thank you.
edit: My users won't understand Ctrl+C, Highlight and Right Click > Copy is what I'm looking for (if a Copy button isn't possible)
If a user presses Ctrl-C while the MessageBox has focus, the message, the MessageBox caption and the MessageBoxButtons labels are copied to the clipboard.
I googled your title and found this..
Or if you really need a button that says copy you can create your own MessageBox with a new windows form and then do what you want with the buttons. Open it like this to keep the MessageBox feel :
var myMessageBox = new CustomMessageBox();
myMessageBox.ShowDialog();
It sounds like maybe you are looking for the Clipboard class.
Clipboard.SetText(variableWithValue);
There is also another answer here about manipulating the contents of a Message Box.
It also might be easier to simply make a modal dialog that emulates a MessageBox without actually using the MessageBox class.

Is it possible to force a WebBrowser to, instead of opening in a new window, replace the currently opened window?

So I have a link I'm trying to click on a WebBrowser control. The problem is it pops up in a new tab, making IE open. I can't manage the web pages after it opens in IE, so I need to force it to somehow stay within my program. It doesn't matter if another WebBrowser control needs to open or anything, just so long as it says in my program.
there are a few ways you can do this, one way is to loop through the DOM (Document Object Model) and find the link which opens in a new window, and change its "target=" attribute value to "target=_top" this way it will open in current top-level page of your current WB Control container.
Otherwise, there is a NewWindow2 event which you can intercept, write the following code inside that event (where WB1 is you WebBrowser Controls name):
Processed = True
WB1.Navigate2 URL
This will tell your WB Control that the request has been processed (tricking it into believing that it has been processed), and if you just did this, nothing would happen, so after the first line you write the second line which tells it to open the URL that it just tried to open in the new window, in the current window (WB1), so really you are just reissuing the request but for the same container/WB Control where the link was clicked.
I've written the code in VB, however I'm sure it's not a problem to understand and transfer to c#.
Let me know how you get along and if there is anything else i can do to help.

Watin script for handling Ajax popup

I'm using WatiN testing tool and i'm writing c#.net scripts. I've a scenario where i need to change the theme of my web page, so to do this i need to click on a image button which opens a ajax popup with the image and "Apply Theme" button which is below the image now i need to click on the button so how to do this please suggest some solution.
So first click your button that throws up the popup, and .WaitUntilExists() for the button inside the popup.
IE.Button("ShowPopup").click()
IE.Button("PopupButtonID").WaitUntilExists()
IE.Button("PopupButtonID").click()
This may not work in the case the button on the popup exists but is hidden from view. In that case you could try the .WaitUntil() and specify an attribute to look for.
IE.Button("ButtonID").WaitUntil("display","")
The Ajax pop-up itself shouldn't pose a problem if you handle the timing of the control loading asynchronously. If you are using the ajax control toolkit, you can solve it like this
int timeout = 20;
for (i=0; i < timeout; i++)
{
bool blocked = Convert.ToBoolean(ie.Eval("Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack();"));
if (blocked)
{
System.Threading.Thread.Sleep(200);
}
else
{
break;
}
}
With the control visible you then should be able to access it normally.
Watin 1.1.4 added support for WaitUntil on controls as well, but I haven't used it personally.
// Wait until some textfield is enabled
textfield.WaitUntil("disable", false.ToSting, 10);
I'm not using any ajax control toolkit and in the popup there is no text field as i've mentioned there is only a image and a button below it, which i need to click in order to apply that image as theme.
I wrote a post about how I do ajax synchronization, since I was having problems with WaitUntilExists: http://lebobitz.wordpress.com/2011/03/06/synchronizing-watin-and-ajax-with-jquery/

Categories

Resources