I want to browse for a folder in a LINQPad script so I tried using FolderBrowserDialog. It didn't work.
Here is a snippet showing what I'm doing?
string path = "";
var browser = new FolderBrowserDialog { ShowNewFolderButton = false };
if (browser.ShowDialog() == DialogResult.OK)
{
path = browser.SelectedPath;
}
This runs and hangs on the line with ShowDialog() with the yellow execution pointer against that line, but the folder browser dialog isn't visible.
I know that there is an overload for ShowDialog() that takes an IWin32Window owner argument and thought that might be the solution, but haven't yet figured out how to get an IWin32Window for the main LINQPad window. I hoped the Util class might provide a way but unless I'm missing it, it doesn't seem to.
Anyone have advice on getting around this problem?
Not solution, but an alternative, the FilePicker controle (part of new LinqPad Input Controls).
you can write:
new FilePicker().Dump().TextInput += (x, e) => ((FilePicker)x).Text.Dump();
or:
var picker = new FilePicker();
picker.TextInput += (x, e) => {
var fileName = picker.Text;
//action with the file...
};
full example:
void Main()
{
new FilePicker().Dump().TextInput += (x, e) => procces(((FilePicker)x).Text);
}
void procces(string file)
{
file.Dump("chosen file...");
//...
}
Setting the Run each query in its own process option to true is the cause of the problem. Setting that option back to the default false allows the code described above to run as expected.
However, making this change disables the built-in debugging. Furthermore the behaviour is still slightly problematic.
On first running the script the dialog is displayed and the script runs to completion after Ok or Cancel is selected. However, on running the script a second time it hangs as described in the question. After cancelling the execution and running it again the dialog displays but on the time after that it hangs again, and so on.
It was pointed out that setting the Always use fresh application domains option may resolve this and it does, allowing the dialog to display on every execution of the script.
I just came across this problem with LINQPad 5. I needed a folder picker similar to the file picker. Your solution worked without me having to modify LINQPad5 settings. The problem was the dialog was staying in the background. So here's how I got your snippet to work with that dialog in focus. Instead of using the FolderBrowseDialog.ShowDialog() I used the overload that passes in a windows form. I created a new form with description and window position then passed that to ShowDialog. That allowed me to set the description and window positioning.
string path = "";
using ( var browser = new System.Windows.Forms.FolderBrowserDialog { ShowNewFolderButton = false })
{
browser.Description = "Select Folder For Foo Processing";
var form = new System.Windows.Forms.Form(){TopMost = true, TopLevel = true};
var result = browser.ShowDialog(form);
if (result == System.Windows.Forms.DialogResult.OK)
{
path = browser.SelectedPath;
}
}
path.Dump();
I tried to initialize the form in ShowDialog with the settings, but had problems so I opted to declare it before show dialog. Hope this helps anyone with this problem.
Related
I have a button which opens an OpenFileDialog. When I compile the application, run it for the first time, press the button, select file(s) and then press the accept dialog button, it waits for about a minute before adding the selected file(s) into my list box.
If I close the application, restart it and do the same thing as above, everything works fast and normal. From then on it always works fast. It's only the very first time I run it after the compilation when it's too slow.
The code extract is below. What could be wrong with the dialog? Why does it run slowly for the first time? Thank you.
void ButtonAddClick(object sender, EventArgs e)
{
this.openFileDialog.FileName = String.Empty;
this.openFileDialog.InitialDirectory = this.openPath;
if (this.openFileDialog.ShowDialog() == DialogResult.OK)
{
foreach (string file in this.openFileDialog.FileNames)
{
if (!File.Exists(file))
{
this.ShowStatus("Error occured selecting file " + Path.GetFileName(file));
}
else if (!this.listBoxFiles.Items.Contains(file))
{
this.listBoxFiles.Items.Insert(0, file);
}
else{
this.ShowStatus("File " + Path.GetFileName(file) + " already selected");
}
}
}
if (this.listBoxFiles.Items.Count > 0)
{
this.openPath = Path.GetDirectoryName(this.listBoxFiles.Items[0].ToString());
this.listBoxFiles.Enabled = true;
this.buttonClear.Enabled = true;
this.buttonFolder.Enabled = true;
}
}
If you are facing this problem of slowness at first initialisation, my suggestion is listed below to rectify this.
For VS debugging, in Visual Studio IDE, just go to Tools>Options>Debugging. Find settings page named [Symbols]. Click it to load the page content. On the right side panel if “Microsoft Symbol Servers” checkbox is checked, just uncheck it and press “Ok” to save the settings. Now on running compiled exe, if the problem still persists then I suggest to perform some cleanup operation on your pc. Also make sure that all timely VS updates are incorporated in your VS. Let me know if this helps if not we can figure out something else.
Try calling Directory.GetFiles(folderPath); prior to showing the OpenFileDialog window. That might trigger the same caching that's occurring after you do OpenFileDialog the first time with your current method.
Right now, I am trying to develop a program using Mono and GTK# on a Debian (Raspbian) system.
The issue I'm facing is, that, completely randomly, the GUI (generated by the Stetic designer or its dynamic elements) isn't completely drawn, missing either a few characters from a Label-element or whole widgets, mostly those that were dynamically created. This is how it looks on a dialog window: http://imgur.com/oEZRg7c (text is cut off)
As soon as one window shows this issue, every other window has the same issues, sometimes missing whole widgets, even if those were created afterwards. The solution is usually to quit the program and reopen it, as it only randomly occurs.
This is how the constructor of most of my windows looks like (the part after Build() varies):
public partial class ErrorSolutionDialog : Gtk.Dialog
{
public ErrorSolutionDialog (string errorMessage, string solutionHint)
{
this.WidthRequest = this.Screen.Width;
this.HeightRequest = this.Screen.Height;
this.Maximize ();
this.Fullscreen ();
this.KeepAbove = true;
this.DestroyWithParent = false;
Build ();
this.ErrorMessage.Markup = "<b><span size='xx-large'>" + errorMessage + "</span></b>";
this.SolutionHint.Text = solutionHint;
}
}
I wouldn't say that the use of the Stetic designer inside Xamarin Studio/Monodevelop is bad, but as any piece of software it certainly has some issues.
Also, the use of any designer in any software environment will tie you to that development platform forever. Finally, the created source code will be hardly maintainable, apart from completely foreign for you.
That's why I always recommend to get rid of the designer. You can follow a Gtk# tutorial such as this one, and you'll find it is easy and rewarding. And you'll have whole and thorough control of your code.
The basics about Gtk# is creating a layout with VBoxes and HBoxes. For example, the following code creates a layout in which you'll have a TreeView and a TextView in a Dialog.
var swWin1 = new Gtk.ScrollWindow();
var swWin2 = new Gtk.ScrollWindow();
// TextView
this.txtView = new Gtk.TextView();
swWin1.AddWithViewport( this.txtView );
// TreeView
this.tvView = new Gtk.TreeView();
swWin2.AddWithViewport( this.tvView );
// Layout
var hBox = new HBox( false, 2 );
hBox.PackStart( swWin1, true, true, 5 );
hBox.PackStart( swWin2, true, true, 5 );
this.VBox.PackStart( hBox, true, true, 5 );
PackStart() is the method doing the magic in order to add a widget to a layout. The booleans tell Gtk to expand the widget. A ScrollWindow adds scrollbars to any widget.
Finally, my advice is for any action, use Gtk.Action, and call its methods CreateMenuItem() and CreateToolItem() in order to create menu entries and toobar buttons, instead of repeating the same code again and again.
Hope this helps.
I use watin, because I need to open some websites in the background for which the user needs to support Javascript. I don't know if WatiN is the best for this job, but at the moment it takes very long until Internet Explorer gets visible. I need to disable to popping up of Internet Explorer while using WatiN. User doesn't need to see the opening of sites. Is it possible while using WatiN to visit a website without showing it the user or should I use another alternative which supports JS on client side?
My code at the moment;
public static void visitURL()
{
IE iehandler = new IE("http://www.isjavascriptenabled.com");
if (iehandler.ContainsText("Yes"))
Console.WriteLine("js on");
else
Console.WriteLine("js off");
}
The WatIn.Core.IE class has a Visible property, you can initialize the object like that:
new WatiN.Core.IE() { Visible = true }
This way the IE will just blink on the screen when it's created, and then it will get hidden. You can later control the visibility of the IE with the ShowWindow method of WatiN.Core.IE class - I mean you can show it on the screen if you need, or you can hide again.
I use exactly that trick (of hiding IE) for writing UnitTests (using https://github.com/o2platform/FluentSharp_Fork.WatiN) that run in an hidden IE window
For example here is how I create a helper class (with an configurable hidden value)
public IE_TeamMentor(string webRoot, string path_XmlLibraries, Uri siteUri, bool startHidden)
{
this.ie = "Test_IE_TeamMentor".popupWindow(1000,700,startHidden).add_IE();
this.path_XmlLibraries = path_XmlLibraries;
this.webRoot = webRoot;
this.siteUri = siteUri;
}
which is then consumed by this test:
[Test] public void View_Markdown_Article__Edit__Save()
{
var article = tmProxy.editor_Assert() // assert the editor user (or the calls below will fail due to security demands)
.library_New_Article_New() // create new article
.assert_Not_Null();
var ieTeamMentor = this.new_IE_TeamMentor_Hidden();
var ie = ieTeamMentor.ie;
ieTeamMentor.login_Default_Admin_Account("/article/{0}".format(article.Metadata.Id)); // Login as admin and redirect to article page
var original_Content = ie.element("guidanceItem").innerText().assert_Not_Null(); // get reference to current content
ie.assert_Has_Link("Markdown Editor")
.link ("Markdown Editor").click(); // open markdown editor page
ie.wait_For_Element_InnerHtml("Content").assert_Not_Null()
.element ("Content").innerHtml()
.assert_Is(original_Content); // confirm content matches what was on the view page
var new_Content = "This is the new content of this article".add_5_RandomLetters(); // new 'test content'
ie.element("Content").to_Field().value(new_Content); // put new content in markdown editor
ie.button("Save").click(); // save
ie.wait_For_Element_InnerHtml("guidanceItem").assert_Not_Null()
.element ("guidanceItem").innerHtml()
.assert_Is("<P>{0}</P>".format(new_Content)); // confirm that 'test content' was saved ok (and was markdown transformed)
ieTeamMentor.close();
}
Here are a number of posts that might help you to understand how I use it:
https://github.com/TeamMentor/Dev/tree/master/Source_Code/TM_UnitTests/TeamMentor.UnitTests.QA/TeamMentor_QA_IE
http://blog.diniscruz.com/2014/07/how-to-debug-cassini-hosted-website-and.html
http://blog.diniscruz.com/2014/07/using-watin-and-embedded-cassini-to-run.html
http://blog.diniscruz.com/search/label/WatiN
Im having some trouble my program hanging when selecting a file in a file dialog. This is the code that is showing the file browser dialog:
private void isForgeIncluded_btn_Click(object sender, EventArgs e)
{
this.isForgeIncluded.Text = FolderFileDialog("file", isForgeIncluded.Text, "Forge installer file (*.jar)|*.jar");
}
public string FolderFileDialog(string type, string current, string fileTypes = "All files (*.*)|*.*|All files (*.*)|*.*", string StartFolder = "C:\\")
string ReturnString = current;
if (current != "")
{
StartFolder = Path.GetFullPath(current);
}
if (type == "file")
{
OpenFileDialog minecraftFile = new OpenFileDialog();
minecraftFile.Title = "Select file";
minecraftFile.InitialDirectory = StartFolder;
minecraftFile.RestoreDirectory = true;
minecraftFile.Filter = fileTypes;
if (minecraftFile.ShowDialog() == DialogResult.OK)
{
ReturnString = minecraftFile.FileName;
return ReturnString;
}
minecraftFile = null;
}
return ReturnString;
}
I've narrowed the problem down to between the "if" statement and the "Return string = minecraftFile.FileName;" .. When using the debugger the program takes up to a five second break between those two lines. After its break it comes back and returns the value as if nothing was wrong. But IntelliTrace sometimes comes up with a "FileNotFound Exception" even though the program never shows me any error messages, and returns the correct value to the textbox as it should.
The wierd part is that it is not always this happens. Its random and can happen even though i select the same file as last time. The files i select are local files too on the system drive.
What could be wrong? Does the code look as it should?
Two basic problems can cause trouble with OpenFileDialog. First is misbehaving shell extension installed on your machine, they'll get loaded into your process when you open the dialog. A basic check is that you don't see trouble with Notepad's File + Open command and that you see nothing special in the Visual Studio Output window while the dialog is active. Enabling unmanaged debugging gives you more info.
The second cause is having an issue with the thread state of your UI thread. If must be initialized as a single-threaded apartment to be able to show this dialog properly. Go back to your Main() method, normally in Program.cs, and ensure it has the [STAThread] attribute.
If that doesn't pan out then we need more info about the code that's misbehaving. You need to enable unmanaged debugging, Project + Properties, Debug tab. And enable the Microsoft symbol server, Tools + Options, Debugging, Symbols. And disable Just My Code debugging, Tools + Options, Debugging, General. Hit Debug + Break All right after you get it to misbehave and post the content of the Call Stack window.
This question is more of a follow-up to this one:
Hiding Internet Explorer when WatiN is run
Like the person who asked that original question, I also want to stop IE from being shown when my WatiN tests are running, but even when using this setting in a seemingly correct manner (code snippet below), it still ends up showing an empty IE window initially (although it does not show the test behavior/web page interaction).
Is it possible to stop the window from showing at all, or is this as good as it gets?
My helper method to create a new IE instance:
public static IE CreateNewBrowserInstance(string url = DefaultAppUrl)
{
Settings.Instance.MakeNewIeInstanceVisible = false;
Settings.Instance.AutoMoveMousePointerToTopLeft = false;
Settings.Instance.AutoStartDialogWatcher = false;
return new IE(url, true);
}
You can Hide window after initializing new IE instance
browser.ShowWindow(NativeMethods.WindowShowStyle.Hide);