I've just started to learn programming in C# and I have a problem. I made a simple application (just a window with two buttons). One button starts another program and the other button shows this window again in 2 minutes (it's a sort of reminder). There's no X button, but it can still be closed by ALTF4 which I want to disable.
I've tried e.Cancel = true;, but I am obviously doing something wrong. What I did was doubleclick on the main window, then void MainFormLoad(object sender, EventArgs e) appeared in the code and I changed EventArgs e to FormClosingEventArgs and pasted the above e.Cancel = true; in there.
I am getting the following error:
No overload for 'MainFormLoad' matches delegate 'System.EventHandler' (CS0123)
It refers me to this line in MainForm.Designer.cs:
this.Load += new System.EventHandler(this.MainFormLoad);
Here's the whole code:
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace Reminder
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
//
// TODO: Add constructor code after the InitializeComponent() call.
//
}
int duration = 0;
void Timer1Tick(object sender, EventArgs e)
{
duration++;
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = #"C:\ThisProgram.exe";
Process.Start(start);
Application.Exit();
if (duration == 1)
{
Timer.Stop();
}
}
void LaterClick(object sender, EventArgs e)
{
Timer.Enabled = true;
Timer.Start();
Hide();
}
void OKClick(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo start = new System.Diagnostics.ProcessStartInfo();
start.FileName = #"C:\OtherExternalProgram.exe";
Process.Start(start);
Application.Exit();
}
void MainFormLoad(object sender, FormClosingEventArgs e)
{
e.Cancel = true;
}
}
}
And here is the MainForm.Designer.cs code:
namespace Reminder
{
partial class MainForm
{
/// <summary>
/// Designer variable used to keep track of non-visual components.
/// </summary>
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.Button OK;
private System.Windows.Forms.Timer Timer;
private System.Windows.Forms.Button Later;
/// <summary>
/// Disposes resources used by the form.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing) {
if (components != null) {
components.Dispose();
}
}
base.Dispose(disposing);
}
/// <summary>
/// This method is required for Windows Forms designer support.
/// Do not change the method contents inside the source code editor. The Forms designer might
/// not be able to load this method if it was changed manually.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.OK = new System.Windows.Forms.Button();
this.Timer = new System.Windows.Forms.Timer(this.components);
this.Later = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// OK
//
this.OK.Location = new System.Drawing.Point(12, 253);
this.OK.Name = "OK";
this.OK.Size = new System.Drawing.Size(75, 23);
this.OK.TabIndex = 0;
this.OK.Text = "OK";
this.OK.UseVisualStyleBackColor = true;
this.OK.Click += new System.EventHandler(this.OKClick);
//
// Timer
//
this.Timer.Interval = 24000;
this.Timer.Tick += new System.EventHandler(this.Timer1Tick);
//
// Later
//
this.Later.Location = new System.Drawing.Point(425, 253);
this.Later.Name = "Later";
this.Later.Size = new System.Drawing.Size(75, 23);
this.Later.TabIndex = 1;
this.Later.Text = "Later";
this.Later.UseVisualStyleBackColor = true;
this.Later.Click += new System.EventHandler(this.LaterClick);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Window;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Zoom;
this.ClientSize = new System.Drawing.Size(512, 288);
this.Controls.Add(this.Later);
this.Controls.Add(this.OK);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "MainForm";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.TopMost = true;
this.Load += new System.EventHandler(this.MainFormLoad);
this.ResumeLayout(false);
}
}
}
Thank you in advance for your help.
Related
For instance, if there is a push button, when the user press the button, the system should detect and record the time in dd-mm-yy. However, if the user press the button many time in 2 second, the system should only record the first time when the user press the button.
Assumption: you want to collect clicks but no more often than every 2 seconds. This simple form will do it.
Code:
namespace RecordClicks {
public partial class Form1 : Form {
private DateTime _recordedClick = DateTime.MinValue;
public Form1() {
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e) {
DateTime currentClickTime = DateTime.Now;
if (currentClickTime.Subtract(_recordedClick).TotalMilliseconds > 2000 ) {
_recordedClick = currentClickTime;
lblClick.Text = currentClickTime.ToString();
// TODO: add code to record this click
}
}
}
}
Designer:
namespace RecordClicks {
partial class Form1 {
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if (disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent() {
this.button1 = new System.Windows.Forms.Button();
this.lblClick = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(121, 241);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(132, 53);
this.button1.TabIndex = 0;
this.button1.Text = "Clicker";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// lblClick
//
this.lblClick.AutoSize = true;
this.lblClick.Font = new System.Drawing.Font("Segoe UI", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point);
this.lblClick.Location = new System.Drawing.Point(121, 127);
this.lblClick.Name = "lblClick";
this.lblClick.Size = new System.Drawing.Size(90, 45);
this.lblClick.TabIndex = 1;
this.lblClick.Text = "Time";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.lblClick);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Clicker Test";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private Button button1;
private Label lblClick;
}
}
I am working on a Visual Studio 2010 C# web form application. I have this working apart from a display message when the application looses its internet connection. As it stands, when an internet connection can ot be found it displays a blank screen. In my coding I have some code which should catch a comms disconnection and display "Trying to connect ...[{0}]" with a countdown in seconds. The issue I have is the message is not displaying. Can any one see where I am going wrong.
My MainForm.cs:
using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Windows.Forms;
namespace Dis_Browser
{
public partial class MainForm : Form
{
public MainForm()
{
if (!InternetExplorerBrowserEmulation.IsBrowserEmulationSet())
{
InternetExplorerBrowserEmulation.SetBrowserEmulationVersion();
}
InitializeComponent();
Cursor.Hide();
}
private void MainForm_Load(object sender, EventArgs e)
{
browser.DocumentTitleChanged += ValidateConnection;
//this.WindowState = FormWindowState.Maximized;
}
private bool offline;
private void ValidateConnection(object sender, EventArgs e)
{
if (offline)
return;
//if (browser.DocumentTitle == "Internet Explorer cannot display the webpage" || browser.DocumentTitle == "Navigation Canceled" || browser.DocumentTitle == "This page can’t be displayed" || browser.DocumentTitle == "Broadband Link - Error")
if (browser.DocumentTitle != string.Empty)
{
offline = true;
browser.Visible = false;
ThreadPool.QueueUserWorkItem(obj => WaitConnection());
return;
}
browser.Visible = true;
}
private int countdown = 20;
private void WaitConnection()
{
while (!Native.IsOnline("http://www.google.com"))
{
Invoke((MethodInvoker)(() => { connectLbl.Text = String.Format(" Trying to connect ...[{0}]", countdown--); }));
if (countdown == 0)
countdown = 20;
Thread.Sleep(TimeSpan.FromSeconds(5));
}
offline = false;
Invoke((MethodInvoker)(() =>
{
browser.Refresh();
browser.Visible = true;
countdown = 20;
}));
}
private void browser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void connectLbl_Click(object sender, EventArgs e)
{
}
}
public class Native
{
[DllImport("wininet.dll", SetLastError = true)]
static extern bool InternetCheckConnection(string lpszUrl, int dwFlags, int dwReserved);
public static bool IsOnline(string url)
{
return InternetCheckConnection(url, 1, 0);
}
}
}
My MainForm.Designer.cs
namespace Dis_Browser
{
partial class MainForm
{
///
/// Required designer variable.
///
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.browser = new System.Windows.Forms.WebBrowser();
this.connectLbl = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// browser
//
this.browser.Dock = System.Windows.Forms.DockStyle.Fill;
this.browser.Location = new System.Drawing.Point(0, 0);
this.browser.Margin = new System.Windows.Forms.Padding(0);
this.browser.MinimumSize = new System.Drawing.Size(1920, 1080);
this.browser.Name = "browser";
this.browser.ScriptErrorsSuppressed = true;
this.browser.ScrollBarsEnabled = false;
this.browser.Size = new System.Drawing.Size(1920, 1080);
this.browser.TabIndex = 0;
this.browser.Url = new System.Uri("http://www.google.com", System.UriKind.Absolute);
this.browser.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.browser_DocumentCompleted);
//
// connectLbl
//
this.connectLbl.Dock = System.Windows.Forms.DockStyle.Fill;
this.connectLbl.Font = new System.Drawing.Font("Microsoft Sans Serif", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
this.connectLbl.Location = new System.Drawing.Point(0, 0);
this.connectLbl.MaximumSize = new System.Drawing.Size(1920, 1080);
this.connectLbl.MinimumSize = new System.Drawing.Size(1920, 1080);
this.connectLbl.Name = "connectLbl";
this.connectLbl.Size = new System.Drawing.Size(1920, 1080);
this.connectLbl.TabIndex = 1;
this.connectLbl.Text = " Trying to connect ...[20] Please check your Internet router";
this.connectLbl.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.connectLbl.Click += new System.EventHandler(this.connectLbl_Click);
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1920, 1080);
this.Controls.Add(this.browser);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.MaximumSize = new System.Drawing.Size(1920, 1080);
this.MinimumSize = new System.Drawing.Size(1920, 1080);
this.Name = "MainForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Dis Systems";
this.Load += new System.EventHandler(this.MainForm_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.WebBrowser browser;
private System.Windows.Forms.Label connectLbl;
}
}
I know there are many ways to write this but this is my attempt. Many thanks in advance for your time.
I'm using Nlog in my project, and now there's a problem, here it is:
I want to write logs into a RichTextBox, everything is OK at first, but when I clear the lines in RTB, and then restart to write logs, it goes wrong, it will not show any line in the RTB.
But, if I use
log.Error("Error\n"); // there is a "\n" in the end
instead of
log.Error("Error");
it will be OK, I don't know why is that...
any clue of this? thanks very much for any suggestion.
BTW I'm using Win7 X64, VS 2013, C# WinForm, .NET 4.0, NLog 3.2.0
using System;
using System.Windows.Forms;
using NLog;
using NLog.Targets;
namespace RTBLogDemo
{
public partial class LogForm : Form
{
#region Designer
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.logRichTextBox = new System.Windows.Forms.RichTextBox();
this.btnStart = new System.Windows.Forms.Button();
this.btnStop = new System.Windows.Forms.Button();
this.btnClear = new System.Windows.Forms.Button();
this.logTimer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// logRichTextBox
//
this.logRichTextBox.Dock = System.Windows.Forms.DockStyle.Bottom;
this.logRichTextBox.Location = new System.Drawing.Point(0, 173);
this.logRichTextBox.Name = "logRichTextBox";
this.logRichTextBox.Size = new System.Drawing.Size(656, 299);
this.logRichTextBox.TabIndex = 0;
this.logRichTextBox.Text = "";
//
// btnStart
//
this.btnStart.Location = new System.Drawing.Point(100, 48);
this.btnStart.Name = "btnStart";
this.btnStart.Size = new System.Drawing.Size(75, 39);
this.btnStart.TabIndex = 1;
this.btnStart.Text = "start";
this.btnStart.UseVisualStyleBackColor = true;
this.btnStart.Click += new System.EventHandler(this.btnStart_Click);
//
// btnStop
//
this.btnStop.Location = new System.Drawing.Point(261, 48);
this.btnStop.Name = "btnStop";
this.btnStop.Size = new System.Drawing.Size(75, 39);
this.btnStop.TabIndex = 2;
this.btnStop.Text = "stop";
this.btnStop.UseVisualStyleBackColor = true;
this.btnStop.Click += new System.EventHandler(this.btnStop_Click);
//
// btnClear
//
this.btnClear.Location = new System.Drawing.Point(417, 48);
this.btnClear.Name = "btnClear";
this.btnClear.Size = new System.Drawing.Size(75, 39);
this.btnClear.TabIndex = 3;
this.btnClear.Text = "Clear";
this.btnClear.UseVisualStyleBackColor = true;
this.btnClear.Click += new System.EventHandler(this.btnClear_Click);
//
// logTimer
//
this.logTimer.Tick += new System.EventHandler(this.logTimer_Tick);
//
// LogForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(656, 472);
this.Controls.Add(this.btnClear);
this.Controls.Add(this.btnStop);
this.Controls.Add(this.btnStart);
this.Controls.Add(this.logRichTextBox);
this.Name = "LogForm";
this.Text = "Log Form";
this.Load += new System.EventHandler(this.LogForm_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.RichTextBox logRichTextBox;
private System.Windows.Forms.Button btnStart;
private System.Windows.Forms.Button btnStop;
private System.Windows.Forms.Button btnClear;
private System.Windows.Forms.Timer logTimer;
#endregion
public LogForm()
{
InitializeComponent();
}
private Logger log;
private void logTimer_Tick(object sender, EventArgs e)
{
log.Error("Error"); // WRONG
// log.Error("Error\n"); // OK
}
private void btnStart_Click(object sender, EventArgs e)
{
logTimer.Start();
}
private void btnStop_Click(object sender, EventArgs e)
{
logTimer.Stop();
}
private void btnClear_Click(object sender, EventArgs e)
{
logRichTextBox.Clear();
}
private void InitLogger()
{
RichTextBoxTarget target = new RichTextBoxTarget();
target.Layout = "${date:format=HH\\:MM\\:ss} ${logger} ${message}";
target.ControlName = logRichTextBox.Name;
target.FormName = this.Name;
target.UseDefaultRowColoringRules = true;
target.AutoScroll = true;
target.MaxLines = 50;
NLog.Config.SimpleConfigurator.ConfigureForTargetLogging(target, LogLevel.Trace);
log = LogManager.GetCurrentClassLogger();
}
private void LogForm_Load(object sender, EventArgs e)
{
InitLogger();
}
}
}
I have created a small project with a progress bar to test how BackgroundWorker does its job. So the progress bar goes from 0 to 100%. But now what I want to do is to trigger another BackgroundWorker after the first one finishes its job. The second BackgroundWorker simply has to display a MessageBox. I have put the second worker's RunWorkerAsync() event at the end of the first worker's RunWorkerCompleted() event. To make sure the first worker's method is completed.
Strange thing that happens is that my second worker's DoWork() event is triggered twice.
This is a simplified version of the error I am getting in my main program.
Can anyone spot the issue?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace backgroundworker
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bw1 = new BackgroundWorker();
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.ProgressChanged += new ProgressChangedEventHandler(bw1_ProgressChanged);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw1.WorkerReportsProgress = true;
bw1.WorkerSupportsCancellation = true;
bw2.DoWork += new DoWorkEventHandler(bw2_DoWork);
bw2.ProgressChanged += new ProgressChangedEventHandler(bw2_ProgressChanged);
bw2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted);
}
private void bw1_DoWork(object sender, DoWorkEventArgs e)
{
for (int i = 0; i < 100; i++)
{
Thread.Sleep(50);
bw1.ReportProgress(i);
if (bw1.CancellationPending)
{
e.Cancel = true;
bw1.ReportProgress(0);
return;
}
}
bw1.ReportProgress(100);
}
private void bw1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%";
}
private void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (e.Cancelled)
{
lblStatus.Text = "Task Cancelled.";
}
else if (e.Error != null)
{
lblStatus.Text = "Error while performing background operation.";
}
else
{
lblStatus.Text = "Task Completed...";
}
btnStartAsyncOperation.Enabled = true;
btnCancel.Enabled = false;
bw2.RunWorkerAsync();
}
private void btnStartAsyncOperation_Click(object sender, EventArgs e)
{
btnStartAsyncOperation.Enabled = false;
btnCancel.Enabled = true;
bw1.RunWorkerAsync();
}
private void btnCancel_Click(object sender, EventArgs e)
{
if (bw1.IsBusy)
{
bw1.CancelAsync();
}
}
private void bw2_DoWork(object sender, DoWorkEventArgs e)
{
}
private void bw2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
}
private void bw2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
MessageBox.Show("Second task is done too.");
}
}
}
Update: Form1.Designer.cs
namespace backgroundworker
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.lblStatus = new System.Windows.Forms.Label();
this.bw1 = new System.ComponentModel.BackgroundWorker();
this.btnStartAsyncOperation = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.bw2 = new System.ComponentModel.BackgroundWorker();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(12, 154);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(1164, 100);
this.progressBar1.TabIndex = 1;
//
// lblStatus
//
this.lblStatus.AutoSize = true;
this.lblStatus.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblStatus.Location = new System.Drawing.Point(469, 42);
this.lblStatus.Name = "lblStatus";
this.lblStatus.Size = new System.Drawing.Size(70, 25);
this.lblStatus.TabIndex = 2;
this.lblStatus.Text = "label1";
//
// bw1
//
this.bw1.WorkerReportsProgress = true;
this.bw1.WorkerSupportsCancellation = true;
this.bw1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bw1_DoWork);
this.bw1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bw1_ProgressChanged);
this.bw1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw1_RunWorkerCompleted);
//
// btnStartAsyncOperation
//
this.btnStartAsyncOperation.Location = new System.Drawing.Point(12, 109);
this.btnStartAsyncOperation.Name = "btnStartAsyncOperation";
this.btnStartAsyncOperation.Size = new System.Drawing.Size(160, 39);
this.btnStartAsyncOperation.TabIndex = 3;
this.btnStartAsyncOperation.Text = "Start";
this.btnStartAsyncOperation.UseVisualStyleBackColor = true;
this.btnStartAsyncOperation.Click += new System.EventHandler(this.btnStartAsyncOperation_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(1024, 109);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(152, 39);
this.btnCancel.TabIndex = 4;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// bw2
//
this.bw2.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bw2_DoWork);
this.bw2.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.bw2_ProgressChanged);
this.bw2.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw2_RunWorkerCompleted);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1188, 272);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnStartAsyncOperation);
this.Controls.Add(this.lblStatus);
this.Controls.Add(this.progressBar1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Label lblStatus;
private System.ComponentModel.BackgroundWorker bw1;
private System.Windows.Forms.Button btnStartAsyncOperation;
private System.Windows.Forms.Button btnCancel;
private System.ComponentModel.BackgroundWorker bw2;
}
}
You have registered the completed event on the designer.
this.bw1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw1_RunWorkerCompleted);
this.bw2.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.bw2_RunWorkerCompleted);
And then you registered it again on code behind.
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted);
You need to remove ones of them.
I write simply code:
using System.Windows.Forms;
namespace Test01
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(33, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(186, 20);
this.textBox1.TabIndex = 0;
this.textBox1.Text = "Text";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(38, 65);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(80, 17);
this.checkBox1.TabIndex = 1;
this.checkBox1.Text = "checkBox1";
this.checkBox1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(234, 86);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.textBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
void textBox1_TextChanged(object sender, System.EventArgs e)
{
if (this.checkBox1.Checked)
this.checkBox1.CheckState = CheckState.Unchecked;
else
this.checkBox1.CheckState = CheckState.Checked;
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.CheckBox checkBox1;
}
}
And:
namespace Test01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
I get a windows with textbox("Text") and checkbox. When i focus on textbox and press CtrlA checkbox toggle state, because TextBox.TextChanged raised and textBox1_TextChanged executed.
But, i can`t understand why TextChanged event raised on CtrlA?
I met the same problem, use this code below:
private void textBox1_TextChanged(Object sender, EventArgs e)
{
if (textBox1.SelectedText == textBox1.Text && textBox1.Text != "")
return;
}
You can suppress this behaviour by adding the condition below:
void textBox1_TextChanged(object sender, System.EventArgs e)
{
if (ModifierKeys == Keys.Control)
return;
//rest of the code
}
Workaround for my problem:
void textBox1_TextChanged(object sender, System.EventArgs e)
{
if (_oldText.Equals(this.textBox1.Text))
return;
_oldText = this.textBox1.Text;
//rest of code
}
I can`t find better solution.