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());
}
}
}
Related
I have a issue : I have a main form who plays music when loaded : and I made a function who plays a sound when a button is pressed : the next form is minimized and their content too (from config.cs to every form), it's supposed to be at fullscreen and I want to keep it fullscreen and not minimized when playing a sound.
Code :
using System;
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 System.Media;
using System.IO;
using System.Runtime.InteropServices;
namespace Poker
{
public partial class Main : Form
{
[DllImport("wmp.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
//LoadForm lform = new LoadForm();
Config config = new Config();
Settings settings = new Settings();
public Main()
{
InitializeComponent();
}
public void Main_Load(object sender, EventArgs e)
{
FormBorderStyle = FormBorderStyle.None;
WindowState = FormWindowState.Maximized;
new System.Threading.Thread(() =>
{
try
{
player1.PlayLooping();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message,
"Impossible de jouer le morceau.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}).Start();
}
private void btnJouer_Click_1(object sender, EventArgs e)
{
btnClick();
this.Hide();
config.Show();
}
Sound function :
void btnClick()
{
new System.Threading.Thread(() =>
{
try
{
var _bt = new System.Windows.Media.MediaPlayer();
_bt.Open(new System.Uri(Environment.CurrentDirectory + "\\sfx\\button.wav"));
_bt.Play();
}
catch(Exception xe)
{
MessageBox.Show(xe.Message,
"Impossible de jouer ce son.",
MessageBoxButtons.OK,
MessageBoxIcon.Error);
}
}).Start();
}
Main form code :
namespace Poker
{
partial class Main
{
/// <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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Main));
this.titre = new System.Windows.Forms.TableLayoutPanel();
this.label1 = new System.Windows.Forms.Label();
this.btnQuitter = new System.Windows.Forms.Button();
this.btnJouer = new System.Windows.Forms.Button();
this.btnPara = new System.Windows.Forms.Button();
this.titre.SuspendLayout();
this.SuspendLayout();
//
// titre
//
this.titre.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.titre.AutoSize = true;
this.titre.BackColor = System.Drawing.Color.Transparent;
this.titre.ColumnCount = 1;
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Absolute, 20F));
this.titre.Controls.Add(this.label1, 0, 0);
this.titre.Location = new System.Drawing.Point(0, 0);
this.titre.Name = "titre";
this.titre.RowCount = 1;
this.titre.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.titre.Size = new System.Drawing.Size(1920, 300);
this.titre.TabIndex = 0;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Segoe UI Semibold", 36F, ((System.Drawing.FontStyle)((System.Drawing.FontStyle.Bold | System.Drawing.FontStyle.Underline))), System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(3, 0);
this.label1.MinimumSize = new System.Drawing.Size(100, 100);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(225, 100);
this.label1.TabIndex = 0;
this.label1.Text = "Poker";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// btnQuitter
//
this.btnQuitter.BackColor = System.Drawing.Color.Transparent;
this.btnQuitter.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnQuitter.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnQuitter.ForeColor = System.Drawing.Color.White;
this.btnQuitter.Location = new System.Drawing.Point(258, 754);
this.btnQuitter.Name = "btnQuitter";
this.btnQuitter.Size = new System.Drawing.Size(1361, 95);
this.btnQuitter.TabIndex = 3;
this.btnQuitter.Text = "Quitter";
this.btnQuitter.UseVisualStyleBackColor = false;
this.btnQuitter.Click += new System.EventHandler(this.btnQuitter_Click);
//
// btnJouer
//
this.btnJouer.BackColor = System.Drawing.Color.Transparent;
this.btnJouer.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnJouer.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnJouer.ForeColor = System.Drawing.Color.White;
this.btnJouer.Location = new System.Drawing.Point(258, 489);
this.btnJouer.Name = "btnJouer";
this.btnJouer.Size = new System.Drawing.Size(1361, 95);
this.btnJouer.TabIndex = 4;
this.btnJouer.Text = "Jouer";
this.btnJouer.UseVisualStyleBackColor = false;
this.btnJouer.Click += new System.EventHandler(this.btnJouer_Click_1);
//
// btnPara
//
this.btnPara.BackColor = System.Drawing.Color.Transparent;
this.btnPara.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.btnPara.Font = new System.Drawing.Font("Segoe UI", 16F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnPara.ForeColor = System.Drawing.Color.White;
this.btnPara.Location = new System.Drawing.Point(258, 621);
this.btnPara.Name = "btnPara";
this.btnPara.Size = new System.Drawing.Size(1361, 95);
this.btnPara.TabIndex = 5;
this.btnPara.Text = "Paramètres";
this.btnPara.UseVisualStyleBackColor = false;
this.btnPara.Click += new System.EventHandler(this.btnPara_Click);
//
// Main
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(1920, 1046);
this.Controls.Add(this.btnPara);
this.Controls.Add(this.btnJouer);
this.Controls.Add(this.btnQuitter);
this.Controls.Add(this.titre);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Main";
this.Text = "Poker";
this.Load += new System.EventHandler(this.Main_Load);
this.titre.ResumeLayout(false);
this.titre.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TableLayoutPanel titre;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button btnQuitter;
private System.Windows.Forms.Button btnJouer;
private System.Windows.Forms.Button btnPara;
}
}
Target form : (config.cs)
namespace Poker
{
partial class Config
{
/// <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.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(525, 270);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(113, 65);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Config
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Config";
this.Text = "Configuration";
this.Load += new System.EventHandler(this.Config_Load);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
}
}
The problem is you creating a new thread, and the Program process switches to the second thread.
How to solve: Create one object of MediaPlayer, and switch a song, when you need.
Or: Create two separated MediaPlayers and pause one when you need.
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.
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'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 pasted two function from my code. They are for adding a tab in tabcontrol and removing a tab in tabcontrol. Both the functions are inside the same form where tabcontrol resides.
I am able to add the tabs in the tabcontrol with ease. I am calling AddTab() from a another class. And it works perfectly.
I am trying to do the same thing for Removing a tab from another class. But tabpage tp always returns null even though there are still two tabs in my tabcontrol.
what am i missing ??
public void AddTab(string strProcessName)
{
try
{
Global.ExistingTabProcessNames.Add(strProcessName);
this.Show();
//this below line dosent makes duplicate tabs.
TabPage tp = new TabPage();
tp.Text = strProcessName;
tp.Name = strProcessName;
tabControl1.TabPages.Add(tp);
//Activate the newly created Tabpage.
tabControl1.SelectedTab = tp;
tabControl1.ItemSize = new Size(200, 32);
tp.Height = tp.Parent.Height;
tp.Width = tp.Parent.Width;
}
catch (Exception ex)
{
}
}
public void RemoveUnusedTabs(string strTabToRemove)
{
TabPage tp = tabControl1.TabPages[strTabToRemove];
tp.Controls.Remove(this);
tabControl1.TabPages.Remove(tp);
}
I am calling the RemoveUnusedTabs from another class like below..
//create a instance for that class.
Taskbar RemoveTabs = new Taskbar();
RemoveTabs.RemoveUnusedTabs(strTabtoRemove);
I would recommend taking a look at this tab page example for adding/removing tabs.
Here is a simple example:
Form1.cs file:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace TabPageExample
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void mAddTabButton_Click(object sender, EventArgs e)
{
TabPage new_tab_page = new TabPage();
if (!mTabNameTextBox.Text.Equals(""))
{
new_tab_page.Text = mTabNameTextBox.Text;
new_tab_page.Name = mTabNameTextBox.Text;
mTabControl.TabPages.Add(new_tab_page);
mTabNameTextBox.Text = "";
}
}
private void mRemoveTabButton_Click(object sender, EventArgs e)
{
if (mTabControl.TabPages.Count > 0)
{
TabPage tab_page = mTabControl.TabPages[mTabNameTextBox.Text];
if (tab_page != null)
{
mTabControl.TabPages.Remove(tab_page);
}
}
mTabNameTextBox.Text = "";
}
}
}
Here is Form1.Designer.cs
namespace TabPageExample
{
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.mTabControl = new System.Windows.Forms.TabControl();
this.mAddTabButton = new System.Windows.Forms.Button();
this.mRemoveTabButton = new System.Windows.Forms.Button();
this.mTabNameTextBox = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// mTabControl
//
this.mTabControl.Location = new System.Drawing.Point(12, 12);
this.mTabControl.Name = "mTabControl";
this.mTabControl.SelectedIndex = 0;
this.mTabControl.Size = new System.Drawing.Size(499, 379);
this.mTabControl.TabIndex = 0;
//
// mAddTabButton
//
this.mAddTabButton.Location = new System.Drawing.Point(162, 408);
this.mAddTabButton.Name = "mAddTabButton";
this.mAddTabButton.Size = new System.Drawing.Size(75, 23);
this.mAddTabButton.TabIndex = 1;
this.mAddTabButton.Text = "Add Tab";
this.mAddTabButton.UseVisualStyleBackColor = true;
this.mAddTabButton.Click += new System.EventHandler(this.mAddTabButton_Click);
//
// mRemoveTabButton
//
this.mRemoveTabButton.Location = new System.Drawing.Point(243, 408);
this.mRemoveTabButton.Name = "mRemoveTabButton";
this.mRemoveTabButton.Size = new System.Drawing.Size(75, 23);
this.mRemoveTabButton.TabIndex = 2;
this.mRemoveTabButton.Text = "Remove Tab";
this.mRemoveTabButton.UseVisualStyleBackColor = true;
this.mRemoveTabButton.Click += new System.EventHandler(this.mRemoveTabButton_Click);
//
// mTabNameTextBox
//
this.mTabNameTextBox.Location = new System.Drawing.Point(194, 444);
this.mTabNameTextBox.Name = "mTabNameTextBox";
this.mTabNameTextBox.Size = new System.Drawing.Size(100, 20);
this.mTabNameTextBox.TabIndex = 3;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(30, 447);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(158, 13);
this.label1.TabIndex = 4;
this.label1.Text = "Enter tab name to Add/Remove";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(523, 476);
this.Controls.Add(this.label1);
this.Controls.Add(this.mTabNameTextBox);
this.Controls.Add(this.mRemoveTabButton);
this.Controls.Add(this.mAddTabButton);
this.Controls.Add(this.mTabControl);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TabControl mTabControl;
private System.Windows.Forms.Button mAddTabButton;
private System.Windows.Forms.Button mRemoveTabButton;
private System.Windows.Forms.TextBox mTabNameTextBox;
private System.Windows.Forms.Label label1;
}
}