Inconsistent behaviour axAcroPDF - c#

I have a pdf viewer which for the sake of the example, displays selected filename from a listbox.
It is a simple form with a listbox an axAcroPDF and textbox to confirm correct filepath.
The code is as follows and the files have been placed in a folder pdfs in the Debug folder:
using System;
using System.Windows.Forms;
namespace pdf_viewer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "pdfs\\" + listBox1.SelectedItem.ToString();
textBox1.Text = path;
InitializeAdobe(path);
}
private void InitializeAdobe(string filePath)
{
axAcroPDF1.LoadFile(filePath);
axAcroPDF1.src = filePath;
axAcroPDF1.setShowToolbar(false);
axAcroPDF1.setView("Fit");
axAcroPDF1.setLayoutMode("SinglePage");
axAcroPDF1.Show();
}
}
}
It all works correctly with a few issues:
the first time you cycle through the files it displays correctly in the window but if you go back to an entry, the second time it shows the toolbar at the right despite this being disabled in the code. The toolbar takes up the bulk of the window.
When you close the window, it takes and inordinately long time to close, indicating to me there is a lot of housekeeping going on. Any clues on why this would be happening.
In addition to this:
do I need both .LoadFile and .src statements in the code as both work in isolation but is one preferable to the other. Doesn't seem to change the above issues. This method was lifted from another Stack Overflow question.
Thanks
PS Since originally posting, I have tried to display in a webBrowser window but exactly the same thing happens with the toolbar panel displaying the second time you select an entry.
The code is as follows:
webBrowser1.Url = new Uri(path);

Revisited this problem after a very long time and found the answer here
Disable Adobe Reader toolbar from my ActiveX
It appears to work in an axAcroPDF and webbrowser window.
For the axAcropdf the code to display pdf without displaying the toolbar is (using question example):
this.axAcroPDF1.src = filePath + "#toolbar=0";
this.axAcroPDF1.setView("Fit");
this.axAcroPDF1.setLayoutMode("SinglePage");
this.axAcroPDF1.Show();
For webbrowser window
InitializeAdobe(path);
webBrowser1.Url = new Uri(path + "#toolbar=0");

Related

Why can't show FolderBrowserDialog twice in Window constructor?

Out of curiosity, I wonder why I can't show two different instances of FolderBrowserDialog one after the other in the constructor of a Window, but can do it in the Window's Loaded event.
The Example 1 just shows the first dialog (fbd1), and doesn't show the next one.
The Example 2 shows the two dialogs.
Example 1 :
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
using (var fbd1 = new FolderBrowserDialog()) {
fbd1.ShowDialog();
}
using (var fbd2 = new FolderBrowserDialog()) {
fbd2.ShowDialog();
}
}
}
Example 2 :
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e) {
using (var fbd1 = new FolderBrowserDialog()) {
fbd1.ShowDialog();
}
using (var fbd2 = new FolderBrowserDialog()) {
fbd2.ShowDialog();
}
}
}
By the way, I've also tested with WinForms, and this is almost the same.
It doesn't work in the Form's constructor and in the Form's Load event, but works in the Shown event.
The answer you like is not in fact the correct answer, it actually does activate the second dialog. Activation state and Z-order are distinct windows properties. You just can't see the dialog because you lost the foreground. One you can only ever keep when you have a window that can stay in the foreground.
A program gets ~6 seconds to steal the foreground with its own window after it starts up. That timeout is easy to see, Windows displays the Cursors.AppStarting cursor (small arrow with hourglass). That worked to get the 1st dialog into the foreground. What happens next is however doomed to go wrong. When the user closes the dialog then your app has no window left that can be moved into the foreground. Windows now goes hunting for another window to put in the foreground, inevitably one that's owned by another process. Pretty likely to be the VS main window when you debug for example. And the 6 seconds has expired. The 2nd dialog will show up and get activated but of course it is overlapped by that window.
Cold hard fact is that a dialog must always have an owner. FolderBrowserDialog is a bit too forgiving about that, providing you with a ShowDialog() overload without an owner argument. Very convenient, not always correct. It uses GetActiveWindow() under the hood to find an owner. If there isn't one then the desktop window becomes the owner, trouble ahead,
otherwise without throwing an exception.
As Reza Aghaei said in his 2nd comment :
When you close the first dialog, the second one appears, but since
your Form is not visible at the moment and is not visible in task-bar,
it doesn't activate the second dialog, while it's open behind other
windows. Just press Alt+Tab to see open windows and you will see the
second dialog too. But when your Form is visible (for example when run
code in Shown) you will not have this issue.
This is the answer to my curiosity.

Can not open any folderBrowserDialogs in my second form

I am using visual studio 2010 in C#. Basically, I have my first form with my main code, then I have a second form set up where the user is prompted to enter multiple paths. However, only on form1 do any folderBrowserDialogs open. On form2, regardless of what I try so far, the button simply clicks and nothing changes. I have not altered anything but variables really from the one I use on form1 and it works just fine.
Here's a bulk of my code, it contains one of the folderBrowserDialog's that will not work. This is all form form2:
string mexPath;
string ausPath;
string canPath;
string chilPath;
public CultureInfo targetCulture1 = new CultureInfo("es-CL"); //Chili
public CultureInfo targetCulture2 = new CultureInfo("de-AT"); //Austria
public CultureInfo targetCulture3 = new CultureInfo("es-MX"); //Mexico
public CultureInfo targetCulture4 = new CultureInfo("fr-CA"); //Canada
PassoloU.PassoloApp app = new PassoloU.PassoloApp();
//Respective Language Paths
private void spanPath_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
mexPath = folderBrowserDialog1.SelectedPath;
}
}
I have established 4 different folderBrowserDialog's in the design and I am not sure where to go from here.
EDIT: Thank you for the help. For some reason, adding "_1" to the end of each click events name allowed the dialog boxes to open. So now each reads "spanPath_Click_1", "germPath_Click_1", etc. I have no idea why this is an issue though, but it seemed to have solved my problem.
Open your Form2 in Designer mode, select the button, go to properties and ensure the click event is attached to the button as MichaC mentioned.
I just tried to put together an example with two forms, Form1 with a label, FolderBrowserDialog, and a button. Form1 displays the browser dialog, and then the selected folder path is displayed in the label. If you click a button it displays Form2 with similar controls. Both the FolderBrowserDialogs worked just fine. Let me know if you need me to post it.

menustrip tool in c sharp

im trying to work with the menustrip and i have the helpToolStripMenuItem_Click may someone help me the code on how to put the documentation i.e if i click help a new window show appear with documentation like this picture i capture from a vlc help button
any ideas this is my empty code
private void helpToolStripMenuItem_Click(object sender, EventArgs e)
{
}
Make form (for example with name frm_Help) with RichTextBox and Button
In Richtextbox add all text (read this Richtextbox)
In button event click add
{
Close();
}
Somewhere where you want to show this form add
{
frm_Help frm=new frm_Help;
frm.ShowDialog();
}
To get the same result as in the image you posted, you need to create a new form, put a WebBrowser control to it and load the HTML page with the documentation to this control in the form's initialization code:
public HelpForm()
{
InitializeComponent();
string text = System.IO.File.ReadAllText(#"Docs\readme.html");
webBrowser1.DocumentText = text;
}
Here, the code reads HTML from the readme.html file located in the Docs subfolder of the folder where the EXE file is located.
You should create new form, containing all the text you want in it and in helpToolStripMenuItem_Click you should create new instance of that form and show it (as a dialog maybe)
Create a new form or add AboutBox and add RichTextbox control.

WebBrowser control doesn't respond during Form_Load

I have a WinForms form with, amongst other things, a WebBrowser control. I use the browser to display a preview of the file the user creates.
When the user loads a document, I want it to automatically refresh the preview window to show the new document. This works 100%.
However, I just added a "Load most recent document" feature which, as you should be able to tell, loads the last document on program start up. Although it goes through the same code path as any other method of loading a document (Open button, File->Open, File->MRU, etc), the preview does not refresh on start up.
I've followed the execution in the debugger, and all the code is being executed. However, it appears that the WebBrowser simply isn't working. If I hit the refresh button (which goes through the same code path) afterwards, it works fine.
public partial class frmMain : Form {
int scrolltop = 0, scrollleft = 0;
delegate void VoidDelegate();
private void Form1_Load(object sender, EventArgs e) {
//irrelevant initialization code omitted
//this is normally 'about:blank', but it doesn't matter anyway
html.Navigate("http://google.com");
NewFile();
if (GlobalSettings.MRU.Files.Count > 0) {
LoadFile(GlobalSettings.MRU.Files[0]);
}
}
public void NewFile() {
//misc blanking omitted
html.DocumentText = "";
}
private void LoadFile(string file) {
//file loading code omitted
//Trying to call RefreshPreview after everything else is done.
this.Invoke(new VoidDelegate(RefreshPreview));
//RefreshPreview());
}
public void RefreshPreview() {
//preserve the position if possible
if (html.Document.Body != null) {
scrolltop = html.Document.Body.ScrollTop;
scrollleft = html.Document.Body.ScrollLeft;
}
//string code = HtmlProcessing.ProcessCode(txtCode.Text, GetImageList());
string code = "If you can see this, it worked.";
html.DocumentText = code;
}
}
If you paste this code into a form named frmMain with a WebBrowser control named html, and hook up the Form1_Load event (note to self, rename this ;), you should be able to reproduce this sample. Maybe add a button that calls RefreshPreview() too.
So, short version: During Form_Load, WebBrowser doesn't do anything. Afterwards, it works fine. I need it to do something during Form_Load, what am I doing wrong?
I would recommend moving your code to the Form.Shown event. The problem is likely due to the order and timing of Form events. Since Load occurs prior to display of the form, the WebBrowser's VisibleChanged event never occurs, and I believe it is completely inactive.

HTML to Image: The Confusion Begins

Currently I load an HTML string into a webBrowser control's contents and tell the user to print-screen it.
I'd like to somehow either grab the webBrowser contents as an image, or somehow render this HTML to an image file that can be saved. Are there any free libraries to do this?
I have been curious as to how this is accomplished myself. I have not tried to do this, but here is a link to a CodeProject article that seems to describe the process quite well:
HTML to Image in C#
I use a program called Webshot and I absolutely love it. It has a freeware version with upgrade available. It even has a callable command line interface which can be used in an automated fashion.
Link here.
Well, there's bound to be a better solution than this, but I'm pretty sure it's better than your solution (I haven't checked out the other answers), so...
I have a working C# program that saves a bitmap image of a control. Currently, the control is a textbox, but you could easily make it a web browser control. The entire program is below. The form has a button, button1, and a textbox, textBox1.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(textBox1.Width,textBox1.Height);
textBox1.DrawToBitmap(bm,new Rectangle(0,0,bm.Width,bm.Height));
bm.Save("image.bmp");
}
}
}

Categories