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();
}
}
}
Related
I am trying to create a winform application. Accidentally I found that Radiobutton.Checked event is fired when we programmatically assign true to RadioButton.Checked. But it is not fired when we programmatically assign false to Radiobutton.Checked. But it fires, when we manually checked/unchecked it. Here is a sample Code. Copy it,compile it and run it to check
using System.Windows.Forms;
using System;
using System.ComponentModel;
using System.Windows;
namespace Temp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//radioButton1.Checked = true;
//radioButton2.Checked = true;
radioButton1.Checked = false;
radioButton2.Checked = false;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton tempRB = (System.Windows.Forms.RadioButton)sender;
MessageBox.Show(tempRB.Name + " : " + (tempRB.Checked ? "Checked" : "Unchecked"));
//radioButton2.Checked = !radioButton1.Checked;
}
}
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.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(173, 84);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(385, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 100);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(21, 28);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(21, 52);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(85, 17);
this.radioButton2.TabIndex = 1;
this.radioButton2.Text = "radioButton2";
this.radioButton2.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(663, 261);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
}
static class Temp
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Can anybody give an explanation about this. And also help me on how to fire RadioButton.Checked event when it got assigned false programmatically.
Note: I am using .net 3.5,Windows 10.
EDIT:
I am adding slightly modified code. In this "Uncheck" button didn't trigger anything. But "Check Both" triggers two times CheckedChanged event. Check this code
using System.Windows.Forms;
using System;
using System.ComponentModel;
using System.Windows;
namespace Temp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
radioButton1.Checked = false;
radioButton2.Checked = false;
}
private void button2_Click(object sender, EventArgs e)
{
radioButton1.Checked = true;
radioButton2.Checked = true;
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
System.Windows.Forms.RadioButton tempRB = (System.Windows.Forms.RadioButton)sender;
MessageBox.Show(tempRB.Name + " : " + (tempRB.Checked ? "Checked" : "Unchecked"));
//radioButton2.Checked = !radioButton1.Checked;
}
}
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.groupBox1 = new System.Windows.Forms.GroupBox();
this.radioButton1 = new System.Windows.Forms.RadioButton();
this.radioButton2 = new System.Windows.Forms.RadioButton();
this.button2 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(173, 84);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "UnCheck";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(173, 132);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 3;
this.button2.Text = "Check Both";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// groupBox1
//
this.groupBox1.Controls.Add(this.radioButton2);
this.groupBox1.Controls.Add(this.radioButton1);
this.groupBox1.Location = new System.Drawing.Point(385, 56);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(200, 100);
this.groupBox1.TabIndex = 2;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1";
//
// radioButton1
//
this.radioButton1.AutoSize = true;
this.radioButton1.Location = new System.Drawing.Point(21, 28);
this.radioButton1.Name = "radioButton1";
this.radioButton1.Size = new System.Drawing.Size(85, 17);
this.radioButton1.TabIndex = 0;
this.radioButton1.Text = "radioButton1";
this.radioButton1.UseVisualStyleBackColor = true;
this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// radioButton2
//
this.radioButton2.AutoSize = true;
this.radioButton2.Location = new System.Drawing.Point(21, 52);
this.radioButton2.Name = "radioButton2";
this.radioButton2.Size = new System.Drawing.Size(85, 17);
this.radioButton2.TabIndex = 1;
this.radioButton2.Text = "radioButton2";
this.radioButton2.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(663, 261);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.button2);
this.Name = "Form1";
this.Text = "Form1";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.RadioButton radioButton2;
private System.Windows.Forms.RadioButton radioButton1;
}
static class Temp
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
I have some code that starts a new thread every time a button is clicked. Each new thread draws a randomly colored circle in a random location in a primitive graphics engine and is added to a Listview. The link to this engine is https://github.com/NAIT-CNT/GDIDrawer.
I need to find a way to remove and abort a thread based on whatever Listview item the user clicks.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using GDIDrawer;
using System.Threading;
namespace ica7
{
public partial class Form1 : Form
{
CDrawer _gdi = new CDrawer();
List<Thread> _threads = new List<Thread>();
Random _rng = new Random();
Point _coords;
int _diameter;
bool _isRunning;
public Form1()
{
InitializeComponent();
_diameter = 10;
_isRunning = true;
btnStopThreads.Enabled = false;
}
private Color RandomColor()
{
return Color.FromArgb(_rng.Next(256), _rng.Next(256), _rng.Next(256));
}
private void DrawCircle(int diameter, Point coords, CDrawer gdi)
{
gdi.AddCenteredEllipse(coords, diameter, diameter, RandomColor());
}
private void btnStartThread_Click(object sender, EventArgs e)
{
btnStopThreads.Enabled = true;
Thread t = new Thread(() =>
{
int diameter = _diameter;
while (_isRunning)
{
_coords.X = _rng.Next(800);
_coords.Y = _rng.Next(600);
DrawCircle(diameter, _coords, _gdi);
Thread.Sleep(10);
}
});
_threads.Add(t);
ListViewItem lvi = new ListViewItem(t.ToString());
lboxThreads.Items.Add(lvi);
t.Start();
}
private void btnStopThreads_Click(object sender, EventArgs e)
{
_threads.ElementAt(lboxThreads.SelectedIndex).Abort();
lboxThreads.Items.RemoveAt(lboxThreads.SelectedIndex);
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
_diameter = trackBar1.Value;
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
_threads.ForEach(x => x.Abort());
}
}
}
Here is the designer file, in case it's needed:
namespace ica7
{
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.components = new System.ComponentModel.Container();
this.btnStartThread = new System.Windows.Forms.Button();
this.btnStopThreads = new System.Windows.Forms.Button();
this.lboxThreads = new System.Windows.Forms.ListBox();
this.trackBar1 = new System.Windows.Forms.TrackBar();
this.lblTbMin = new System.Windows.Forms.Label();
this.lblTbMax = new System.Windows.Forms.Label();
this.timer1 = new System.Windows.Forms.Timer(this.components);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).BeginInit();
this.SuspendLayout();
//
// btnStartThread
//
this.btnStartThread.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.btnStartThread.Location = new System.Drawing.Point(9, 11);
this.btnStartThread.Name = "btnStartThread";
this.btnStartThread.Size = new System.Drawing.Size(121, 23);
this.btnStartThread.TabIndex = 0;
this.btnStartThread.Text = "Start a Thread";
this.btnStartThread.UseVisualStyleBackColor = true;
this.btnStartThread.Click += new System.EventHandler(this.btnStartThread_Click);
//
// btnStopThreads
//
this.btnStopThreads.ForeColor = System.Drawing.SystemColors.ActiveCaptionText;
this.btnStopThreads.Location = new System.Drawing.Point(9, 40);
this.btnStopThreads.Name = "btnStopThreads";
this.btnStopThreads.Size = new System.Drawing.Size(121, 23);
this.btnStopThreads.TabIndex = 1;
this.btnStopThreads.Text = "Stop Selected Thread";
this.btnStopThreads.UseVisualStyleBackColor = true;
this.btnStopThreads.Click += new System.EventHandler(this.btnStopThreads_Click);
//
// lboxThreads
//
this.lboxThreads.FormattingEnabled = true;
this.lboxThreads.Location = new System.Drawing.Point(138, 11);
this.lboxThreads.Name = "lboxThreads";
this.lboxThreads.Size = new System.Drawing.Size(303, 134);
this.lboxThreads.TabIndex = 2;
//
// trackBar1
//
this.trackBar1.Location = new System.Drawing.Point(9, 69);
this.trackBar1.Maximum = 50;
this.trackBar1.Minimum = 10;
this.trackBar1.Name = "trackBar1";
this.trackBar1.Size = new System.Drawing.Size(121, 45);
this.trackBar1.TabIndex = 3;
this.trackBar1.Value = 10;
this.trackBar1.Scroll += new System.EventHandler(this.trackBar1_Scroll);
//
// lblTbMin
//
this.lblTbMin.AutoSize = true;
this.lblTbMin.Location = new System.Drawing.Point(6, 101);
this.lblTbMin.Name = "lblTbMin";
this.lblTbMin.Size = new System.Drawing.Size(19, 13);
this.lblTbMin.TabIndex = 4;
this.lblTbMin.Text = "10";
//
// lblTbMax
//
this.lblTbMax.AutoSize = true;
this.lblTbMax.Location = new System.Drawing.Point(111, 101);
this.lblTbMax.Name = "lblTbMax";
this.lblTbMax.Size = new System.Drawing.Size(19, 13);
this.lblTbMax.TabIndex = 5;
this.lblTbMax.Text = "50";
//
// timer1
//
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(453, 157);
this.Controls.Add(this.lblTbMax);
this.Controls.Add(this.lblTbMin);
this.Controls.Add(this.trackBar1);
this.Controls.Add(this.lboxThreads);
this.Controls.Add(this.btnStopThreads);
this.Controls.Add(this.btnStartThread);
this.ForeColor = System.Drawing.SystemColors.ActiveCaption;
this.Name = "Form1";
this.Text = "Form1";
this.FormClosed += new System.Windows.Forms.FormClosedEventHandler(this.Form1_FormClosed);
((System.ComponentModel.ISupportInitialize)(this.trackBar1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnStartThread;
private System.Windows.Forms.Button btnStopThreads;
private System.Windows.Forms.ListBox lboxThreads;
private System.Windows.Forms.TrackBar trackBar1;
private System.Windows.Forms.Label lblTbMin;
private System.Windows.Forms.Label lblTbMax;
private System.Windows.Forms.Timer timer1;
}
}
If there are superior ways of doing any of this, please let me know. This is my first foray into threads. This is homework.
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.
Whereas MSDN suggests that some incompatible behavior might also cause this issue
In my case, The incompatible behavior is caused by some possibly weird(messed up mappings) on Entity Framework.
I have done due diligence to ensure one-to-one correspondence between the data model and the EF model(even recreated it twice, ground up)
The project builds and runs when I comment out a certain section of code.
public partial class TestForm : SecureForm
{
private myEntities _db = new myEntities();
public TestForm()
{
InitializeComponent();
PopulateTestsList();
}
private void PopulateTestsList()
{
listViewTypes.Items.Clear();
var query = from q in _db.Types orderby q.TypeName select q;
foreach (Type dt in query)
{
ListViewItem lvi = new ListViewItem(new string[]
{
dt.TypeName
});
lvi.Tag = dt;
listViewTypes.Items.Add(lvi);
}
}
private void buttonAdd_Click(object sender, EventArgs e)
{
TestEditForm myEditForm = new TestEditForm(_db);
if (myEditForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
PopulateTestsList();
}
private void buttonEdit_Click(object sender, EventArgs e)
{
if (listViewTypes.SelectedItems.Count > 0)
{
Test tsdt = listViewTypes.SelectedItems[0].Tag as Test;
if (myEditForm != null)
{
TestEditForm myEditForm = new TestEditForm(_db, mysForm);
if (myEditForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
PopulateTestsList();
}
}
}
}
It still does not show up the designer.
Here's the designer.cs for reference
partial class TestForm
{
/// <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.buttonDelete = new System.Windows.Forms.Button();
this.buttonEdit = new System.Windows.Forms.Button();
this.buttonAdd = new System.Windows.Forms.Button();
this.listViewTypes = new System.Windows.Forms.ListView();
this.columnName = ((System.Windows.Forms.ColumnHeader)(new System.Windows.Forms.ColumnHeader()));
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// buttonDelete
//
this.buttonDelete.Location = new System.Drawing.Point(304, 86);
this.buttonDelete.Name = "buttonDelete";
this.buttonDelete.Size = new System.Drawing.Size(75, 23);
this.buttonDelete.TabIndex = 9;
this.buttonDelete.Text = "Delete";
this.buttonDelete.UseVisualStyleBackColor = true;
this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click);
//
// buttonEdit
//
this.buttonEdit.Location = new System.Drawing.Point(304, 56);
this.buttonEdit.Name = "buttonEdit";
this.buttonEdit.Size = new System.Drawing.Size(75, 23);
this.buttonEdit.TabIndex = 8;
this.buttonEdit.Text = "Edit";
this.buttonEdit.UseVisualStyleBackColor = true;
this.buttonEdit.Click += new System.EventHandler(this.buttonEdit_Click);
//
// buttonAdd
//
this.buttonAdd.Location = new System.Drawing.Point(304, 26);
this.buttonAdd.Name = "buttonAdd";
this.buttonAdd.Size = new System.Drawing.Size(75, 23);
this.buttonAdd.TabIndex = 7;
this.buttonAdd.Text = "Add";
this.buttonAdd.UseVisualStyleBackColor = true;
this.buttonAdd.Click += new System.EventHandler(this.buttonAdd_Click);
//
// listViewTypes
//
this.listViewTypes.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnName});
this.listViewTypes.FullRowSelect = true;
this.listViewTypes.HideSelection = false;
this.listViewTypes.Location = new System.Drawing.Point(12, 26);
this.listViewTypes.Name = "listViewTypes";
this.listViewTypes.Size = new System.Drawing.Size(274, 205);
this.listViewTypes.TabIndex = 6;
this.listViewTypes.UseCompatibleStateImageBehavior = false;
this.listViewTypes.View = System.Windows.Forms.View.Details;
//
// columnName
//
this.columnName.Text = "Document Type";
this.columnName.Width = 270;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 10);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(79, 13);
this.label1.TabIndex = 5;
this.label1.Text = "Test Standards";
//
// TestsForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(395, 242);
this.Controls.Add(this.buttonDelete);
this.Controls.Add(this.buttonEdit);
this.Controls.Add(this.buttonAdd);
this.Controls.Add(this.listViewTypes);
this.Controls.Add(this.label1);
this.Name = "TestsForm";
this.Opacity = 1D;
this.Text = "Test Standards";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button buttonDelete;
private System.Windows.Forms.Button buttonEdit;
private System.Windows.Forms.Button buttonAdd;
private System.Windows.Forms.ListView listViewTypes;
private System.Windows.Forms.ColumnHeader columnName;
private System.Windows.Forms.Label label1;
}
Code analysis shows that the form is missing,(MSBuild does not). MSBUILD compiles the form into an exe thats kinda defunct(on the broken entity)
The entity in question has Name & ID properties. Both Name and ID are mapped properly, The collection is pluralized and loaded into a list.
How to identify the broken entity and/or
How to Identify the non-compatible behavior in the code
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.