I am using a OpenFileDialog to Open a File i want to process in my application, but the processing takes few seconds, and during that processing time the OpenFileDialog stays visible, and it's bothering!!! i want to hide my OpenFileDialog during the processing time !
Same problem goes to the SaveFileDialog
private void ofdImportation_FileOk(object sender, CancelEventArgs e)
{
Processing(); //Takes Few Seconds
//ofdImportation remains visible during that time...
//i want to hide it...
}
thank you everybody...
if (OpenFileDialog.ShowDialog() == DialogResult.Ok)
{
// Do stuff
}
The OK and Cancel events should be for UI specific behaviour irrespective of whatever the resulting file is for.
Separation of concerns
Clicking ok gives you a file, cancel gives you say a null, then you have a class with a process method that you pass the file name from the dialog to.
It shouldn't care where the file name came from.
Think about the hurdles you'd have to leap to unit test Processing()
Assuming you have a button to open the Dialog
Solution 1 (this way the window freezes so its not really a solution, i post it anyway):
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.Ok)
{
Thread.Sleep(10000);
}
}
Solution 2 (use of BackGroundWorker, a helpful tool for async jobs):
public partial class Form1 : Form
{
BackgroundWorker bgw;
String fileUrl;
public Form1()
{
InitializeComponent();
bgw = new BackgroundWorker();
bgw.DoWork += new DoWorkEventHandler(bgw_DoWork);
bgw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bgw_RunWorkerCompleted);
}
void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
button1.Text = fileUrl;
}
void bgw_DoWork(object sender, DoWorkEventArgs e)
{
Thread.Sleep(10000);
}
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
var dlr = ofd.ShowDialog();
if (dlr == DialogResult.Ok)
{
fileUrl = ofd.FileName;
bgw.RunWorkerAsync();
}
}
}
edit: 'Thread.Sleep(10000)' simulates your long running process ('Processing();')
Related
I have a small Menu strip item where I have a plethora of buttons which activate different forms.
The code for one button would be this:
Form B1 = new Form1();
private void Button1_Click(object sender, EventArgs e)
{
if (B1.Visible == false)
{
B1 = new Form1();
}
B1.Visible = true;
B1.Activate();
}
I also have a mouse enter- and leave event:
private void Button1_MouseEnter(object sender, EventArgs e)
{
Button1.Text = "Something prdy intriguing";
}
private void Button1_MouseLeave(object sender, EventArgs e)
{
Button1.Text = "Hi";
}
And a tooltip:
private void Tooltips()
{
ToolTip forB1 = new ToolTip();
forB1.SetToolTip(button1, "21.11.17");
}
Now imagine i need about 8 buttons for 8 different forms, that means i have to repeat all of these again and a gain, wasting time AND taking up a LOT of code space.
Is it possible to compress these in anyway?
This is very out of my world, im unsure where to start optimizing.
One option is move all this to one function:
public void AttachMenuStripButtonHandlers(
Button btn,
Form form,
string enterText,
string leaveText,
string tooltip) {
btn.Click += (sender, args) => {
form.Visible = true;
form.Activate();
};
btn.MouseEnter += (sender, args) => {
btn.Text = enterText;
};
btn.MouseLeave += (sender, args) => {
btn.Text = leaveText;
};
new ToolTip().SetToolTip(btn, tooltip);
}
And for each button call like this:
AttachMenuStripButtonHandlers(Button1, B1, "on enter", "on leave", "tooltip");
For second part of your question, You could do something like this
private void Button_MouseEnter(object sender, EventArgs e)
{
((Button)sender).Text = "Something prdy intriguing";
}
private void Button_MouseLeave(object sender, EventArgs e)
{
((Button)sender).Text = "Hi";
}
You need to attach same event handler to all buttons.
So i am kinda stealing #Evk's code here but essentially this works the way I wanted to.
public void ButtonHandlers(Type NewForm)
{
NewButton.Click += (sender, args) =>
{
Form TheNewMain = (Form)Activator.CreateInstance(NewForm);
if (TheNewMain.ShowDialog() != DialogResult.Cancel)
{
TheNewMain.Activate();
}
};
Essentially what i added was instead of getting the Form i have to get the type, since what I want is that when a form is Visible, it won't open it twice, going by Evks code it opens it yes but upon close it's disposed and it cant create a new instance of it.
In code i just have to ask for typeof(formName) in as NewForm
Thanks Evk!
so I have this code
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.ShowDialog();
ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed);
}
}
public void MyForm_FormClosed(object sender, FormClosedEventArgs e)
{
updaterAccess();
menu.Items[2].Enabled = true;
}
so after I click Update it will run the child form Form1
which is this:
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
notifyIcon1.Visible = true;
notifyIcon1.BalloonTipTitle = "Update Complete";
notifyIcon1.BalloonTipText = "Successfully Update";
notifyIcon1.ShowBalloonTip(500);
timer1.Interval = 4000;
timer1.Enabled = true;
timer1.Tick += new EventHandler(timer1_Tick);
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
notifyIcon1.Dispose();
this.Close();
}
so as you can see it runs on a backgroundworker with a timer to close the child Form1
now my problem is that after closing the Child Form1 it doesn't run the MyForm_FormClosed which it should enable menu.Items[2] again and updaterAccess()
I think I'm missing something in my mainForm
Attached the event handler before firing ShowDialog
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.FormClosed += new FormClosedEventHandler(MyForm_FormClosed); //Attached the event handler before firing ShowDialog
ShowProgress.ShowDialog();
}
}
ShowDialog synchronously shows a modal dialog, meaning it blocks until the form is closed (the following code is not run until the form is closed). Therefore, when ShowDialog returns the form is already closed.
You can attach the event handler before calling ShowDialog() as #Jade suggests, which will work, but honestly you do not need to use the event system at all. Simply wait for ShowDialog to return then perform the actions you would when the form is closed:
public void Update_Click(object sender, EventArgs e)
{
using (PccBiometricsHandler.Form1 ShowProgress = new PccBiometricsHandler.Form1())
{
menu.Items[2].Enabled = false;
ShowProgress.ShowDialog();
}
updaterAccess();
menu.Items[2].Enabled = true;
}
If you want to do this in VB:
AddHandler ShowProgress.FormClosed, AddressOf MyForm_FormClosed
I've made this program in C#:
namespace Spammer
{
public partial class Form1 : Form
{
int delay, y = 1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
delay = int.Parse(textBox2.Text);
timer1.Interval = delay;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
It works fine most of the time, and it can really send keys quickly.
But when I insert a delay of, for example, 10 MS, it's very hard to click the "Stop" button to stop it. The only way to stop the sending is to close the program and I don't want to do that.
Is there anyway I can send keys very quickly, like 5-10 MS, without it impairing my ability to press the buttons inside the program? I can't click while it's sending quickly...
The problem is that you're using SendWait. That will wait for the target application to respond - and while that's happening, your application won't be able to respond to user input. If you use Send instead of SendWait, your UI thread won't be blocked waiting for the key press to be processed.
I was able to reproduce the issue. The app is sending a keystroke every 10 milliseconds. To me, this is not at all surprising that the app is causing freezes. A keystroke every 10 milliseconds is quite a barrage to the active App. Threading is not going to help. Why is this behavior surprising?
In other words, I don't expect things to work out well when I overload the message pump.
using System;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spammer//your own namesapce
{
public partial class Form1 : Form
{
int delayInMilliseconds, y = 1;
private Timer timer1;
public Form1()
{
InitializeComponent();
//StartTimerWithThreading();
SetupTimer();
}
void StartTimerWithThreading()
{
Task.Factory.StartNew(() =>
{
SetupTimer();
});
}
void SetupTimer()
{
timer1 = new Timer();//Assume system.windows.forms.timer
textBox2.Text = "10";//new delay
timer1.Tick += timer1_Tick;//handler
}
private void button1_Click(object sender, EventArgs e)
{
delayInMilliseconds = int.Parse(textBox2.Text);
timer1.Interval = delayInMilliseconds;
timer1.Enabled = true;
}
private void button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
String textt = textBox1.Text;
SendKeys.SendWait(textt);
}
}
}
The simple solution is instead of adding code to a Click event handler for your button, we need a MouseDown event handler:
//MouseDown event handler for the button2
private void button2_MouseDown(object sender, EventArgs e) {
timer1.Enabled = false;
}
Or you can keep using the Click event handler but we send the key only when the MouseButtons is not Left like this:
private void timer1_Tick(object sender, EventArgs e) {
String textt = textBox1.Text;
if(MouseButtons != MouseButtons.Left) SendKeys.Send(textt);
}
//then you can freely click your button to stop it.
I have this code but it's not working. I've tried several different versions but nothing is working. I'm a newbie and still don't understand everything.
OpenFileDialog filedialog = new OpenFileDialog();
private void button3_Click(object sender, EventArgs e)
{
filedialog.ShowDialog();
filedialog.FileOk += filedialog_FileOk;
}
void filedialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
using (StreamReader myStream = new StreamReader(filedialog.FileName))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = myStream.ReadLine()) != null)
{
listBox1.Items.Add(line);
}
}
}
I think there is a requirement for to much plain text in this editor.
You're adding the event handler after the call to ShowDialog() has returned. Move it to before and it might work.
According to the documentation, FileOK event occurs when Open or Save button is clicked.
You are attaching the event handler inside the click.
You might want to do it on page load or somewhere before the click occurs.
Eg :
OpenFileDialog filedialog = new OpenFileDialog();
protected void Page_Load(object sender, EventArgs e)
{
filedialog.FileOk += filedialog_FileOk;
}
private void button3_Click(object sender, EventArgs e)
{
filedialog.ShowDialog();
}
void filedialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
using (StreamReader myStream = new StreamReader(filedialog.FileName))
{
string line;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = myStream.ReadLine()) != null)
{
listBox1.Items.Add(line);
}
}
}
I'm having some trouble with hiding a form when a BackgroundWorker process is completed.
private void submitButton_Click(object sender, EventArgs e)
{
processing f2 = new processing();
f2.MdiParent = this.ParentForm;
f2.StartPosition = FormStartPosition.CenterScreen;
f2.Show();
this.Hide();
backgroundWorker1.RunWorkerAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
// loop through and upload our sound bits
string[] files = System.IO.Directory.GetFiles(System.Environment.GetFolderPath(System.Environment.SpecialFolder.MyDocuments) + "\\wav", "*.wav", System.IO.SearchOption.AllDirectories);
foreach (string soundBit in files)
{
System.Net.WebClient Client = new System.Net.WebClient();
Client.Headers.Add("Content-Type", "audio/mpeg");
byte[] result = Client.UploadFile("http://mywebsite.com/upload.php", "POST", soundBit);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
formSubmitted f3 = new formSubmitted();
f3.MdiParent = this.ParentForm;
f3.StartPosition = FormStartPosition.CenterScreen;
f3.Show();
this.Hide();
}
Basically, after the 'submit' button is pressed, the application begins to upload the files to the webserver via a php script. Once the upload is complete, the RunWorkerCompleted method is triggered, opening the formSubmitted form. The issue I'm having is that the processing form does not close once the backgroundworker is complete and the formSubmitted opens directly on top of the processing form - as opposed to what I want, having the processing form close and then open the formSubmitted form.
Well actually you are never closing processing form:
try following:
private processing _processingForm;
private void submitButton_Click(object sender, EventArgs e)
{
_processingForm = new processing();
_processingForm.MdiParent = this.ParentForm;
_processingForm.StartPosition = FormStartPosition.CenterScreen;
_processingForm.Show();
this.Hide(); //HIDES THE CURRENT FORM CONTAINING SUBMIT BUTTON
backgroundWorker1.RunWorkerAsync();
}
Now on completion hide processing form:
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
formSubmitted f3 = new formSubmitted();
f3.MdiParent = this.ParentForm;
f3.StartPosition = FormStartPosition.CenterScreen;
_processingForm.Close();//CLOSE processing FORM
f3.Show();
this.Hide();//this REFERS TO THE FORM CONTAINING WORKER OBJECT
}