C# bring Form to Front following File Dialog - c#

When I start up the program I have added code to open a file dialog box, but doing this results in the main form being sent behind Visual Studio(and other open programs) once a file has been selected.
I have tried using this.BringToFront() but this doesn't seem to work.
The program currently only has one form as well, how would I bring this to the front when the program starts?
public Form1()
{
InitializeComponent();
InitialiseDataGrid();
selectFile();
readData();
this.BringToFront();
}
selectFile() is a function that selects a file using a file dialog box,
readData() is a function that reads the data from the text file into a dataGridView.

You should past the owner window's instance while Opening The dialog window. example code:
var file = new OpenFileDialog();
file.ShowDialog(this);

You can use
this.TopMost = true;

You're juggling with different applications: VS and your program. The released version of the program probably won't run through VS anyway.
Bring it to the foreground:
this.Activate();
Use it with caution.

Related

How to close Window in WinUI3?

I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.
For now in order to launch second window I use:
var editorWindow = new EditorWindow();
editorWindow.Activate();
The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.
First time I tried to add Window.Close() after opening the 2nd window in the .cs file of 1st Window, so
var editorWindow= new EditorWindow();
editorWindow.Activate();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted Attempted to read or write protected memory eror.
I tried to do it in the 2nd Window .cs file like this:
this.InitializeComponent();
var oldWindow = new MainWindow();
oldWindow.Close();
Which resulted the same error
So how can I do this properly?
If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:
var editorWindow= new EditorWindow();
editorWindow.Activate();
this.Close();
If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here.

How to check if a dialog is open in PowerPoint?

I have an application written in C# using Microsoft.Office.Interop.PowerPoint. This application opens a PowerPoint presentation file to the user. The user interacts with the file. After applying some modifications, the user submits the file through the application submit button.
The Problem:
When user modifies the content of the presentation, for example changes font color of the text to red, by using a dialog and keeps the dialog open without clicking on "Apply" or "OK" button and thereby submits the file by clicking on application submit button, those dialog done changes aren't reflected in the submitted file and hence such changes can't be tracked of.
I somehow want to alert a user to close any open dialog before clicking on submit button.
I do this easily on Word and excel files by checking Exception on saving such files because Word and Excel throws exception on using save method if there is any dialog open, like the Following:
try{
document.Save();
}
catch (Exception e){
//Alert user here here
}
but this doesn't work for PowerPoint files. I tried the following:
PowerPointApplication application = new Microsoft.Office.Interop.PowerPoint.Application();
Microsoft.Office.Interop.PowerPoint.Presentations presentations = application.Presentations;
Microsoft.Office.Interop.PowerPoint.Presentation presentation =
presentations
.Open(file, WithWindow: Microsoft.Office.Core.MsoTriState.msoTrue,ReadOnly:Microsoft.Office.Core.MsoTriState.msoFalse,Untitled:Microsoft.Office.Core.MsoTriState.msoFalse);
The following is triggered on Submit button click event:
try
{
presentation.Save();
}
catch (Exception e)
{
//alert user here
}
Is there a way to track if there are any open dialog boxes?
Typically if any dialog window is displayed to a user the code is blocked because the dialog window uses the main thread for running. Do you use multiple threads in the code?
Anyway, you can use Windows API functions for detecting the active window, see GetActiveWindow which retrieves the window handle to the active window attached to the calling thread's message queue. To get the handle to the foreground window, you can use GetForegroundWindow. To get the window handle to the active window in the message queue for another thread, use GetGUIThreadInfo.
After retrieving the window handle you can use the GetWindowText function which copies the text of the specified window's title bar (if it has one) into a buffer. If the specified window is a control, the text of the control is copied.

How to Set Application Shutdown Mode in a C# Windows Forms Project?

I have just switched from VB.Net to C# and am writing a windows application.
In VB.net you could easily change the Shutdown Mode by selecting the properties of the
project and moving to a dropdown where you could choose between "When startup form closes"
and "When last form closes". Please help me to find the equivalent in C#.
By the way, I'm using VS 2010.
In c# the easiest trick to achieve this is to change the entry point of the application in the "program.cs" file.
This entry form should be hidden on startup,but will call the main form. Then call the Application.Exit(); function in the close procedure in any other form/class.
Sample pseudo code below
program.cs
//edit this line
Applcation.Run(startupForm);
StratupForm.cs
//startup method
StartupForm_load (object e)
{
this.Hide();
MainForm mainForm = new MainForm();
mainForm.show();
}
MainForm.cs
//application exit method
MainFormExit_close (object e)
{
Application.Exit();
}
You should probably implement a neater way to manage and keep track of open forms later in your program
It's not the first thing you might look for, but if you look at the docs for Application.Run(ApplicationContext), you'll find sample code to do exactly what you're asking for: exit the application when the last form is closed.

Problem with FolderBrowserDialog

If the dialog click Make new folder, just start editing the name just create a folder and click OK, OK dialogrezalt returns, but in the property SelectedPath he will name the folder New folder, then there is the name of the default
This happens because when we create, just edit and click OK, this property is not updated and the method ShowDialog () returns.
How fix this problem?
Thank you!
I had the same problem - if you created a new Folder with the FolderBrowseDialog, the .SelectedPath showed "xxx\NewFolder" not whatever new name the user had given.
The problem went away once I explicitly gave the command, prior to displaying the dialog,
MyFolderBrowser.ShowNewFolderButton = True
I failed to simulate the problem you are describing, I have tested it:
Create a new Form Form1 add button1 to it and in the button1.Click handler copy this code:
private void button1_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
dialog.ShowNewFolderButton = true;
if (dialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK)
{
string path = dialog.SelectedPath;
Console.WriteLine(path);//will not print new folder if the file renamed.
}
}
}
It worked as expected either by creating a new folder and press enter two times. or by creating a new folder and click ok.
Are you using a third party UI Controls, theams...
Edit: You stated:
Yes, if this sample run at windows application, it work correct. But
my application is Excel add-in. And FolderBrowserDialog work that I
write at started post
So you are using a third party "Excel add-in", When using a third party with FolderBrowserDialog or OpenFileDialog.. you may notice a strange behavior depending on the third party..
The solution for the problem you described is either by disabling ShowNewFolderButton or implement your own custom OpenFileDialog.

restart Function in visual C#

I have a windows application I need to create a button that unload/close/restart the currunt form while its running and reload/reopen/restart the same form.
How ??
I tried the hide show thing but it keeps the old form running in the background
Application.Restart();
I found it ....
Application.Restart() will restart your entire application.
Hide() will only do what it says, hide the form. If you just want a fresh version of your form to reappear, you can just create a new one, Show() it, and Close() your current form.
public void Restart()
{
var window = new MyForm();
window.Show();
this.Close();
}
You'll have 2 forms open for a very short time, so if you have any data connections that need to be closed, do so before reopening the form. To the end-user, it will happen so fast that they won't know 2 forms were open.

Categories

Resources