Closing window form? - c#

I am working on C# window forms, in my application i am trying to execute procedures using c# window forms in page load event. I am trying for, while executing the procedure the form should display, after that form should close automatically. My code is like this
public partial class Form1 : Form
{
string servername = "";
string database = ""; string password = ""; string Filepath = ""; string uid = "";
string reslt = "";
public Form1()
{
InitializeComponent();
GetExelData();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Close();
}
}
But with my code window form is not displaying, it is closed when the methods are executed. can you help me.

Try this:-
private BackgroundWorker bw;
private void Form1_Load(object sender, EventArgs e)
{
bw = new BackgroundWorker();
bw.WorkerReportsProgress = true;
bw.DoWork += BwOnDoWork;
bw.WorkerSupportsCancellation = true;
bw.RunWorkerAsync();
}
private void BwOnDoWork(object sender, DoWorkEventArgs e)
{
GetExelData();
bw.CancelAsync();
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(delegate { this.Close(); }));
}
}

Move the GetExcelData() to the Form1_Load handler. (The Shown handler might be better)
This way the method will be called when the form is showing.
To close the form after the method has finished:
use an event or callback (if available)
or call the GetExcelData on a separate thread and close the form when the thread finishes

You could try moving the GetExcelData call to the load event:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GetExelData();
this.Close();
}
Form load only occurs when the form is fully constructed. Note however that, even with my improvement, it doesn't make sense what you're trying to do exactly.
This "delay" in closing is only because the method is executed on the GUI thread (bad practice). Are you trying to display some kind of loading indicator? There are better ways for that.

Use Shown event handler.
This event will be executed after form was displayed first time(just what you need).
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
GetExelData();
}
private void Form1_Shown(object sender, EventArgs e)
{
this.Close();
}

Related

close waiting dialog in c# when event occured

In socket programming that we wait for data receive, i show a dialog that with show method(i do not show it with show dialog method because event is not occured.)
Psudo code:
buttonClick()
{
socket.Send(array byte)
waitingForm.Show();
}
recieveCompletedEventHandler()
{
WaitingForm.Close();
}
It is ok, but user can access the form controls and because the waiting form is not modal.i just want a sample code that when i send bytes and all data received and event occur and the waiting form close. And of course the waiting form be modal when data does not receive completely.
Thanks #Ofir Winegarten for answer
i create a sample and put the code here for others
public partial class Form1 : Form
{
delegate void funDel();
private waiting waitForm;
private event funDel funEvent;
public Form1()
{
InitializeComponent();
funEvent += Form1_funEvent;
}
private void Form1_funEvent()
{
waitForm.Close();
}
private void buttonStart_Click(object sender, EventArgs e)
{
Task.Run(() =>
{
if (InvokeRequired) // Line #1
{
this.Invoke(new MethodInvoker(() => button1.Text = "Proccessing"));
}
else
{
button1.Text = "Proccessing";
}
});
waitForm = new waiting();
timer1.Start();
waitForm.ShowDialog();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
funEvent();
}
}

How to open a form only once?

I have an app that show a form call System Parameters and i want the form to only pop one time so that the user cant open the same window million times. I tried
private void SystemParametersClick(object sender, EventArgs e)
{
Xpan sp = new Xpan();
sp.CurrentItem = this.GetCaller(sender);
if (sp.Visible==false)
{
sp.Show();
}
}
It doesnt work because it is not the same instance. :(
How do i make it only pop once?
Why do you instantiate the form within the method? Simply instantiate it within the parent class and only call the Show() method within the click event.
public class MainForm : Form
{
private Xpan _Xpan;
public MainForm()
{
InitializeComponent();
_Xpan = new Xpan();
}
private void SystemParametersClick(object sender, EventArgs e)
{
_Xpan.Show();
}
}
Maybe this simple approach would suffice?
private bool has_been_shown = false;
private void SystemParametersClick(object sender, EventArgs e)
{
if(!has_been_shown)
{
has_been_shown = true;
Xpan sp = new Xpan();
}
}
First disable closing for Xpan form. You can do it by defining OnFormClosing event handler.
private void Xpan_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
Hide();
}
Then define your Xpan form as a class member of the parent form, e.g.:
private readonly Xpan _sp = new Xpan();
And finally defile your Click handler this way:
private void SystemParametersClick(object sender, EventArgs e)
{
if (!_sp.Visible)
{
_sp.Show();
}
else
{
_sp.Activate();
}
}
That's it.

Events occur only after construction?

I try to understand why i can't invite event in the last line of constructor?
Does the event "formload" occur after construction ends?
public Form1()
{
InitializeComponent();
button1.Text = "a";
button1.PerformClick();
}
private void button1_Click(object sender, EventArgs e)
{
button1.Text = "b";
}
This behavior is because if controls are built on the windows messages, which only works if the form instance exists and has a handle, which is not while it is still built.
The form must be open to be sure that the controls are working properly.
And it's not best practise to raise event on your constructor
Tested. Not dependent on the control created.
public Form1()
{
InitializeComponent();
button1.Text = "A";
button1_Click_helper();
}
private void button1_Click(object sender, EventArgs e)
{
button1_Click_helper();
}
private void button1_Click_helper()
{
button1.Text = "B";
}

Switch among windows forms

I have a bunch of windows forms. Each form has "Back" and "Next" buttons for switching forms. For example, clicking "Back" on Form3 then we go to Form2. Then clicking "Next" button on Form2 then Form3 is shown.
Now my question is that if we click "Next" from the very beginning, it works smoothly. However if I click "Back" on Form3 then Form2 is displayed, then click "Next" on Form3 go to Form3. The code doesn't goto Form3_Load event.
What is wrong in my code?
public partial class Form3 : Form
{
Form2 FormPrev;
Form4 FormNext;
List<DataRow> drlist = new List<DataRow>();
DataTable dt = new DataTable();
public Form3(Form2 _FormPrev)
{
InitializeComponent();
this.FormPrev = _FormPrev;
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnNext_Click(object sender, EventArgs e)
{
ShowNext();
}
private void btnBack_Click(object sender, EventArgs e)
{
ShowPrev();
}
private void ShowNext()
{
if (FormNext == null)
FormNext = new Form4(this);
FormNext.Show();
this.Hide();
}
private void ShowPrev()
{
FormPrev.Show();
this.Hide();
}
private void Form3_Load(object sender, EventArgs e)
{
// blah blah.
}
Thanks.
A form's Load event is only fired when the form is invoked for the first time. If you subsequently hide the form and reshow it then this is not reloading the form so the form's Load event is not fired.
If you want to use an event to handle when the form is re-displayed then you should look at the following more suitable events:
Activated
Shown
VisibleChanged
Form Load event only gets fired before the form is shown for the first time.
You should use a different event, like maybe Form Activated or GotFocus.
That's a normal behaviour. Load is for load form, not for show. In your case you try to show hided form. If you want to use
form.Show()
than use not a
form.Hide()
but
form.Close()
UPD:
Code should be:
public partial class Form3 : Form
{
List<DataRow> drlist = new List<DataRow>();
DataTable dt = new DataTable();
public Form3()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnNext_Click(object sender, EventArgs e)
{
ShowNext();
}
private void btnBack_Click(object sender, EventArgs e)
{
ShowPrev();
}
private void ShowNext()
{
Form4 formNext = new Form4();
formNext.Show();
this.Close();
}
private void ShowPrev()
{
Form2 formPrev = new Form2();
formPrev.Show();
this.Close();
}
private void Form3_Load(object sender, EventArgs e)
{
// blah blah.
}
}
But there is a problem with such colutions - you shouldn't close your main form.

how can i make a threadsafe progress bar in C#? what am i doing wrong?

Hey everyone, can someone let me know what they see wrong with this code ?
it throws "Cross-thread operation not valid" exception, on
_DialogueThread.Start();
but if i remove "owner" from
_progressDialogue = new Progresser{Owner = _owner, StartPosition = FormStartPosition.CenterParent};
the exception wont be thrown but the progressDialouge will be shown then hidden right away .
now i understand why this the error is thrown if i set the progressDialouge.Owner to a parent form that was created on a different thread. but why dose the form disappears when i dont ? what am i doing wrong ?
thanks
class Sampleer : BackgroundWorker
{
private Progresser _progressDialogue;
private Thread _DialogueThread;
private Form _owner;
private bool _SampleSuccess;
public Sampleer(Form owner)
{
_owner = owner;
_progressDialogue = new Progresser{Owner = _owner, StartPosition = FormStartPosition.CenterParent};
_progressDialogue.Closed += ProgressDialogueClosed;
WorkerReportsProgress = true;
WorkerSupportsCancellation = true;
DoWork += Sampleer_DoWork;
RunWorkerCompleted += Sampleer_RunWorkerCompleted;
ProgressChanged += Sampleer_ProgressChanged;
}
private void Sampleer_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//UPDATE STATUS CODE IS HERE
}
void ProgressDialogueClosed(object sender, EventArgs e)
{
CancelAsync();
Dispose();
}
void Sampleer_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//FINISH PROCESS
}
void Sampleer_DoWork(object sender, DoWorkEventArgs e)
{
_DialogueThread = new Thread(_progressDialogue.Show);
_DialogueThread.Start();
//DO LONG PROCESS HERE
}
}
In your action (button click), i would create the progress dialog, and then fire off the background worker. The background worker then reports back to the dialog in the ProgressChanged event.
public partial class MainWindow : Window {
private void btnDoSomething_Click(object sender0, RoutedEventArgs e0) {
_progressDialogue = new Progresser{Owner = _owner, StartPosition = FormStartPosition.CenterParent};
_progressDialogue.Closed += ProgressDialogueClosed;
_progressDialogue.Show();
BackgroundWorker worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.DoWork += delegate(object sender, DoWorkEventArgs e) {
DoSomething();
e.Result = result;
};
worker.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e) {
progressDialogue.Update()
};
worker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e) {
progressDialogue.Close()
};
worker.RunWorkerAsync(new CustomArgs() {
SomeValue = txtValue.Text,
});
}
}
There are a few mistakes in your approach. Let me point them out one by one.
You inherit BackgroundWorker. That is fine. But you create another thread inside (_DialogueThread). There is no need. DoWork() method runs in a separate thread.
You create/use/manipulate a UI element in another thread. Now, always remember. A Thread never creates a UI element. Its the other way around. A UI element creates a Thread. Progresser in your case should be creating a new Thread or using BackgroundWorker to do any background work you require.
`
Yes decyclone is right, there are many mistakes in the code and in your approach to solution.
You are inherting BackgroundWorker type but subscribing to its own events?
Instead override the methods that are responsible for raising the event in your class.
ex: Instead of subscribing to DoWork, override OnDoWork method.
I've created a sample application, in which a Form (when performing a background task) shows another Form (BackgroundWorkUINotification) and starts the background task in BackgroundWorker thread. The BackgroundWorkUINotification notifies the main form when the Form's CancelButton is clicked.
The main Form when notified, closes the notifier and cancels the background task.
Code below: BackgroundWorkUINotification Form
public partial class BackgroundWorkUINotification : Form
{
public event EventHandler CancelClicked;
public BackgroundWorkUINotification()
{
InitializeComponent();
// call code to animate progress bar..
// probably in another BackgroundWorker that reports progress..
this.cancelButton.Click += HandleCancelButtonOnClick;
}
protected virtual void OnCancelClicked()
{
if (CancelClicked != null)
this.CancelClicked(this, EventArgs.Empty);
}
private void HandleCancelButtonOnClick(Object sender, EventArgs e)
{
this.OnCancelClicked();
}
}
Main Form
public partial class Form1 : Form
{
private BackgroundWorker backgroundWorker;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.backgroundWorker = new BackgroundWorker();
this.backgroundWorker.DoWork += HandleBackgroundWorkerOnDoWork;
this.backgroundWorker.RunWorkerCompleted += HandleBackgroundWorkerOnRunWorkerCompleted;
this.backgroundWorker.WorkerSupportsCancellation = true;
}
private void HandleDataRequest()
{
// show UI notification...
BackgroundWorkUINotification backgroundWorkUINotification = new BackgroundWorkUINotification();
backgroundWorkUINotification.CancelClicked += HandleBackgroundWorkUINotificationOnCancelClicked;
backgroundWorkUINotification.Show(this);
// start the background worker
this.backgroundWorker.RunWorkerAsync();
}
private void HandleBackgroundWorkUINotificationOnCancelClicked(Object sender, EventArgs e)
{
// UI notification Form, Cancelled
// close the form...
BackgroundWorkUINotification backgroundWorkUINotification = (BackgroundWorkUINotification)sender;
backgroundWorkUINotification.Close();
// stop the background worker thread...
if (backgroundWorker.IsBusy)
backgroundWorker.CancelAsync();
}
private void HandleBackgroundWorkerOnRunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
{
}
private void HandleBackgroundWorkerOnDoWork(Object sender, DoWorkEventArgs e)
{
// do some work here..
// also check for CancellationPending property on BackgroundWorker
}
}
Some days ago I had the same trouble.
This was helped me: "MSDN. How to: Make Thread-Safe Calls to Windows Forms Controls"
I used first aproach (checking InvokeRequired) because it is easiest way.
Hope it is helpful advise!

Categories

Resources