I'm having an issue with my .NET application only printing the second page of my HTML file, and completely ignoring the first page (no other page is printed, and the back of it is blank).
When I pull up my printer's queue window, it does show it go from "Spooling" to "Printing" and lists both pages, so I'm at a loss as to why it's not printing the first page.
(My printer IS set to duplex printing, and if I literally just print the HTML document from my browser, it works as expected)
Here's what I'm doing:
private void Form1_Load(object sender, EventArgs e)
{
// Create a FileSystemWatcher to monitor all files on drive C.
FileSystemWatcher fsw = new FileSystemWatcher("C:\\COAForms");
// Watch for changes in LastAccess and LastWrite times, and
// the renaming of files or directories.
fsw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
// Register a handler that gets called when a
// file is created, changed, or deleted.
//fsw.Changed += new FileSystemEventHandler(OnChanged);
fsw.Created += new FileSystemEventHandler(OnChanged);
fsw.Error += new ErrorEventHandler(fsw_Error);
//fsw.Deleted += new FileSystemEventHandler(OnChanged);
fsw.EnableRaisingEvents = true;
fsw.SynchronizingObject = this;
PrinterSettings settings = new PrinterSettings();
label2.Text = settings.PrinterName;
Thread.CurrentThread.SetApartmentState(ApartmentState.STA);
}
void fsw_Error(object sender, ErrorEventArgs e)
{
MessageBox.Show(e.ToString());
}
private void OnChanged(object source, FileSystemEventArgs e)
{
notifyIcon1.BalloonTipText = "Printing document " + e.Name + "...";
notifyIcon1.BalloonTipTitle = "Printing Application";
notifyIcon1.BalloonTipIcon = ToolTipIcon.Info;
notifyIcon1.ShowBalloonTip(500);
PrintCOAPage(e.Name);
}
private void PrintCOAPage(string name)
{
try
{
// Create a WebBrowser instance.
WebBrowser webBrowserForPrinting = new WebBrowser();
// Add an event handler that prints the document after it loads.
webBrowserForPrinting.DocumentCompleted +=
new WebBrowserDocumentCompletedEventHandler(PrintDocument);
// Set the Url property to load the document.
webBrowserForPrinting.Url = new Uri(#"C:\COAForms\" + name);
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void PrintDocument(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
try
{
PrinterSettings ps = new PrinterSettings();
ps.Duplex = Duplex.Vertical;
// Print the document now that it is fully loaded.
((WebBrowser)sender).Print();
// Dispose the WebBrowser now that the task is complete.
((WebBrowser)sender).Dispose();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
{
this.Show();
this.Activate();
if (this.WindowState == FormWindowState.Minimized)
{
this.WindowState = FormWindowState.Normal;
}
}
private void Form1_Resize(object sender, EventArgs e)
{
if (FormWindowState.Minimized == WindowState)
{
Hide();
}
}
I only just recently added the PrinterSettings to the code and it changed nothing.
I would greatly appreciate any help you guys can provide on this! Thank you!
Wow, didn't think the CSS would impact it, but for whatever reason the CSS I was using (involving z-index) made the first page NOT display when I print previewed under the WebBrowser control, but worked just fine in actual IE8.. after changing the CSS around a bit it now works as intended.
Related
VS 10, WinFormApp
I have a list of url's. I am trying to navigate to the URL's one by one. After completing the load of the document of a page, then the integrated webbrowser will navigate to the next URL.
Here is some code:
private void btnHit_Click(object sender, EventArgs e)
{
webBrowser1.Navigate(lstUrls[counter].url);
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (e.Url != webBrowser1.Url)
{
lblStatus.Text = "Page is loading....";
lblStatus.ForeColor = Color.Red;
return;
}
else
{
funcMethod();
}
}
public void funcMethod()
{
lblStatus.Text = "Page is loaded";
lblStatus.ForeColor = Color.Green;
try
{
webBrowser1.Document.Focus();
webBrowser1.Navigate(lstUrls[++counter].url);
}
catch { }
}
The problem is, when I debug for each element line by line, it works for all(100+) items in the List of URL's. but when I just press the btnHit, then sometimes it loads 2/3/4/5/.. number of urls. and stop loading further. In that time, If I hit the btnHit again, the process starts and same thing happens as before.
What am I missing?
I have a situation which occurs in WebBrowser with C#.
I am trying to do downloader through a site However , when i click first time, it doesn't work but if I click second times it works.
How can i solve this problem .
Sincerely.
Codes :
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
webBrowser1.Document.GetElementById("youtube-url").SetAttribute("value", textBox1.Text);
webBrowser1.Document.GetElementById("submit").InvokeMember("click");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
label3.Text = "Video Alındı , indirme işleminin hazır olması bekleniyor";
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection col = webBrowser1.Document.GetElementsByTagName("a");
String link = "";
foreach (HtmlElement el in col)
{
if (el.InnerText == "Download")
{
link = el.GetAttribute("href");
Download(link);
label3.Text = "Video indiriliyor";
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.Navigate("http://www.youtube-mp3.org/tr");
}
void Download(String link)
{
WebClient downloader = new WebClient();
downloader.DownloadFileAsync(new Uri(link),#"D:\a.mp3");
downloader.DownloadProgressChanged += new DownloadProgressChangedEventHandler(downloader_DownloadProgressChanged);
}
void downloader_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label3.Text = "Video İndiriliyor : " + progressBar1.Value + "%";
if (progressBar1.Value == 100)
label3.Text = "Video İndirildi";
}
You're blocking yourself from investigating what the problem is. It's never a good idea to disable script errors for WebBrowser control (as you do with ScriptErrorsSuppressed = true), unless you handle them internally in your host app. Do the following:
enable script errors (ScriptErrorsSuppressed = false);
enable script debugging for all applications;
implement WebBrowser Feature Control (FEATURE_BROWSER_EMULATION), so the web page is getting the same (or close) experience and HTML features as with full-featured IE browser.
Then, hopefully, you can find out what's going wrong when you're simulating a button click, and debug it.
I've got this code and I'm using it to show a button which allows the user to choose an image from his library and use it as a background for my app.
So I create a PhotoChooserTask, set it to show the camera and bind it to a method that has to be executed when the task is completed.
The button will start the task by showing the PhotoChooserTask.
The action to do on complete is quite easy, I've just got to set a boolean value and update an image source.
PhotoChooserTask pct_edit = new PhotoChooserTask();
pct_edit.ShowCamera = true;
pct_edit.Completed += pct_edit_Completed;
Button changeImageButton = new Button { Content = "Change Image" };
changeImageButton.Tap += (s, e) =>
{
pct_edit.Show();
};
void pct_edit_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
bi.SetSource(e.ChosenPhoto);
IsRebuildNeeded = true;
}
}
The problem is that it won't show the PhotoChooserTask but it will give me an exception, taking me to
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
if (Debugger.IsAttached)
{
Debugger.Break();
}
}
in App.xaml.cs.
This looks weird as I've got another PhotoChooserTask in the same class and this one works fine.
What's wrong with it?
VisualStudio won't even tell me what's the exception and so there's no way to figure it out!
EDIT:
I just found out that the exception is thrown when I call
pct_edit.Show();
in the button's tap event.
You should be defining your chooser as a field in your class. It's a requirement that you have page scope for the PhotoChooser. You then subscribe to it in your constructor. This is stated on the MSDN here
class SomeClass
{
readonly PhotoChooserTask pct_edit = new PhotoChooserTask();
SomeClass()
{
pct_edit.ShowCamera = true;
pct_edit .Completed += new EventHandler<PhotoResult>(pct_edit_Completed);
}
}
You can use try to check what is the problem
changeImageButton.Tap += (s, e) =>
{
try
{
PhotoChooserTask pct_edit = new PhotoChooserTask();
pct_edit.ShowCamera = true;
pct_edit.Completed += (s,e) =>
{
if (e.TaskResult == TaskResult.OK)
{
var bi = new BitmapImage() // maybe you didn't initialize bi?
bi.SetSource(e.ChosenPhoto);
IsRebuildNeeded = true;
}
}
pct_edit.Show();
}
catch (Exception ex)
{
Message.Show(ex.Message);
}
};
Put brakepoint on Message, then you can check everything inside ex.
So, I'm trying to make a file changed notifier, and I need to make it so the text in a textbox updates whenever the contents of the file are changed. This is what I have so far:
string path = "C:/Users/Max/Dropbox/Public/IM.txt";
StringBuilder b = new StringBuilder();
private void Window_Loaded(object sender, EventArgs e)
{
TB.Text = File.ReadAllText(path);
b.Append(TB.Text);
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = path.Remove(path.Length - 6, 6);
watcher.NotifyFilter = NotifyFilters.LastWrite;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
TB.SelectionStart = TB.Text.Length;
TB.ScrollToCaret();
}
private void OnChanged(object source, FileSystemEventArgs e)
{
TB.Text = File.ReadAllText(path);
}
This seems to raise the event correctly, but as soon as it touches the code in the OnChanged event, the program exits, no errors or anything, just closes. I have tried to stop it from closing, I have even tried putting e.Cancel under the formclosing event, but nothing seems to work. Any ideas? I can provide more info if needed.
Have you tried wrapping the code in try catch
private void OnChanged(object source, FileSystemEventArgs e)
{
try
{
TB.Text = File.ReadAllText(path);
}catch(Exception e)
{
//Show exception in messagebox or log to file.
}
}
Try this in your Changed method
if (TB.InvokeRequired)
{
TB.Invoke(new MethodInvoker(delegate { TB.Text = File.ReadAllText(path); }));
}
I try to use the WebBrowserControll with C#. I use the following Code.
The WebBrowser Controll open the Web Page and if there is a href= blank it is also oppend in an new WebBrowser Controll. But If I want to open an new JavaScript with window.open it doenst open an new WebBrowser Controll. What I have to do that a href= blank and a Java Script is opend in my WebBrowser Controll.
Does anyone have a Answer??
Thanks for your help.
public MainWindow()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.FileDownload += new EventHandler(webBrowser1_FileDownload);
webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);
webBrowser1.DocumentCompleted +=new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.PreviewKeyDown += new PreviewKeyDownEventHandler(webBrowser1_PreviewKeyDown);
this.FormClosing += new FormClosingEventHandler(webBrowser1_FormClosing);
}
public void setURL(String aURL)
{
webBrowser1.Url = new Uri(aURL);
}
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
{
// open href= blank in new WebBrowser Controll
MainWindow newWindow = new MainWindow();
newWindow.setURL(webBrowser1.StatusText);
newWindow.Show();
e.Cancel = false;
}
private void webBrowser1_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
Console.WriteLine(e.KeyCode.ToString() + " " + e.Modifiers.ToString());
if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
{
MessageBox.Show("ctrl-v pressed");
}
}
private void webBrowser1_FormClosing(object sender, FormClosingEventArgs e)
{
if (MessageBox.Show(this, "Really close the window?", "Caption", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
e.Cancel = false;
}
else
{
e.Cancel = true;
}
}
private void beendenToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
I Do not think it is possible... The browser should send a create argument (1), when clicking on a window.open, all though it does work when closing a window (destroy (2))
If you are up for a hack you could search the document on load and replace the window.open in the DocumentText property, with tags instead, it should work with a bit of creativity.
Good luck