I have been searching up and down the web and unfortunately never came across an issue quite like mine, so here goes:
My C# WPF application won't show me no OpenFileDialogs or SafeFileDialogs.
private void btnBrowseNet_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.CheckPathExists = true;
ofd.Multiselect = false;
ofd.Title = "Open Network Configuration Batch file...";
ofd.ValidateNames = true;
ofd.Filter = "Comma Seperated Value Files|*.csv";
if (ofd.ShowDialog() == true)
{
//...
}
}
This exact code does in one occasion exactly what it is supposed to do and hardly five minutes later I can click the button all I want, nothing happens but the mouse pointer turning into a little busy-indicator and then nothing. I can step through the method or do something like this
bool? shown = ofd.ShowDialog();
But no matter what, the dialog won't show. Of course, shown will be false in that case. I wasted one and a half hours searching yesterday and right when I quit I tried it again and all of a sudden it worked. Sometimes it works, sometimes it doesn't. But it seems to be project specific because I can paste the same code into a new project and it works like it is supposed to do. Also, that's the only thing about the project that seems fishy. Everything else works as intended.
Has anyone on here ever experienced something similar and thus an idea of what on earth I could do? Any help weould be highly appreciated.
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
I am experiencing a similar problem, and as Garrett suggested, it is an STA issue. I have been struggling with STA issues a lot in the past few months, as I need to launch screens from a console window (Testing purposes) - this means that the calling thread is not STA, but can be simulated in something like the following:
[STAThread]
private void Execute() {
try {
Thread t = new Thread(() => {
OpenFileDialog dlg = new OpenFileDialog();
// The following would not return the dialog if the current
// thread is not STA
var result = dlg.ShowDialog();
});
t.SetApartmentState(ApartmentState.STA);
t.Start();
} catch (Exception ex) {
// Handle the exception
ex.LogException();
}
}
Unforunately, it did not work for me to just mark the method as STAThread, I had to launch the operation in a thread marked as STA.
sometime [staThread]
not working, you can try this:
public void showOpenFileDialog()
{
OpenFileDialog im = new OpenFileDialog();
if (im.ShowDialog() == DialogResult.OK)
{
textBox1.Text = im.FileName;
}
}
private void select_button_Click(object sender, EventArgs e)
{
Thread newThread = new Thread(new ThreadStart(showOpenFileDialog));
newThread.SetApartmentState(ApartmentState.STA);
newThread.Start();
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With