I created a minimal WinForm Application in C#.
Everything Works Fine but for some reason a "Green border" Surrounds My Application.
I Pasted the code down below.
[program.cs]
namespace WinFormsApp1
{
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
// To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize();
Application.Run(new Form1());
}
}
}
[Form1.cs]
namespace WinFormsApp1
{
public partial class Form1 : Form
{
int counter = 0;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.backgroundWorker1 = new System.ComponentModel.BackgroundWorker();
this.textanimationWorker = new System.ComponentModel.BackgroundWorker();
this.label2 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.label1.AutoSize = true;
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("Segoe UI", 40F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
this.label1.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label1.Location = new System.Drawing.Point(543, 209);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(466, 72);
this.label1.TabIndex = 0;
this.label1.Text = "Uploading RootKit.";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// button1
//
this.button1.Font = new System.Drawing.Font("Segoe UI", 25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
this.button1.Location = new System.Drawing.Point(43, 508);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(222, 70);
this.button1.TabIndex = 1;
this.button1.Text = "Upload RootKit";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(43, 284);
this.progressBar1.MarqueeAnimationSpeed = 1;
this.progressBar1.Maximum = 10;
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(1037, 23);
this.progressBar1.Step = 1;
this.progressBar1.TabIndex = 2;
this.progressBar1.Click += new System.EventHandler(this.progressBar1_Click);
//
// backgroundWorker1
//
this.backgroundWorker1.WorkerReportsProgress = true;
this.backgroundWorker1.DoWork += new System.ComponentModel.DoWorkEventHandler(this.backgroundWorker1_DoWork);
this.backgroundWorker1.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.backgroundWorker1_ProgressChanged);
this.backgroundWorker1.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.backgroundWorker1_RunWorkerCompleted);
//
// textanimationWorker
//
this.textanimationWorker.WorkerReportsProgress = true;
this.textanimationWorker.DoWork += new System.ComponentModel.DoWorkEventHandler(this.textanimationWorker_DoWork);
this.textanimationWorker.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(this.textanimationWorker_ProgressChanged);
this.textanimationWorker.RunWorkerCompleted += new System.ComponentModel.RunWorkerCompletedEventHandler(this.textanimationWorker_RunWorkerCompleted);
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.SystemColors.ButtonHighlight;
this.label2.Location = new System.Drawing.Point(1036, 594);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(86, 15);
this.label2.TabIndex = 3;
this.label2.Text = "DECRAT V1.0.0";
this.label2.Click += new System.EventHandler(this.label2_Click);
//
// Form1
//
this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
this.ClientSize = new System.Drawing.Size(1134, 618);
this.Controls.Add(this.label2);
this.Controls.Add(this.progressBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.label1);
this.Font = new System.Drawing.Font("Segoe UI", 9F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
private void Form1_Load(object sender, EventArgs e)
{
Console.WriteLine("Loading Form1.. Please wait..");
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 0;
progressBar1.Maximum = 10;
backgroundWorker1.RunWorkerAsync();
textanimationWorker.RunWorkerAsync();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
for (int i = 0; i < 10; i++) {
backgroundWorker1.ReportProgress(0);
Thread.Sleep(1000);
}
}
private void backgroundWorker1_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
counter = 50;
MessageBox.Show("Uploaded RootKit Successfully!");
counter = 0;
}
private void backgroundWorker1_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
progressBar1.Value += 1;
}
private void textanimationWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
{
while (counter <= 10)
{
if (counter <= 3)
{
counter++;
textanimationWorker.ReportProgress(0);
Thread.Sleep(500);
}
else if(counter == 50){
break;
}
else {
counter = 0;
textanimationWorker.ReportProgress(0);
}
}
}
private void textanimationWorker_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e)
{
if (counter == 1)
{
label1.Text = "Uploading RootKit";
}
else {
label1.Text += ".";
}
}
private void textanimationWorker_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
}
private void label2_Click(object sender, EventArgs e)
{
}
}
}
FYI: I also published it to an exe file but still the green border does not disappear!
I Thought the green border appears just when I code so that I know where the border is but the issue seems with C# ? or probably with something else ?
EDIT [Adding More information]: I want the green border to disappear.
Here is a photo of the Application..
Green box Indicates The Executable or the program is sandboxed.
I am using COMODO Antivirus and it sandboxed it.
By Turning the antivirus off for a min and running the program the green box disappeared indicating that the executable is not sandboxed.
Related
I know how to scroll vertically/horizontally with buttons but I am not able to figure out how to scroll towards Left/Right.
What I want is that when I click the button LeftScrlBtn I should scroll to left and when I click the RightScrlBtn I should scroll to right.
My FlowLayoutPanel's AutoScroll property is set to false because I don't want the scroll bar, only the buttons.
Edit
To make this post more understandable, here is the code:
using System;
using [...];
//rest of the code
private void LeftScrlBtn_Click(object sender, EventArgs e) {
//Code to scroll to Left
}
private void RightScrlBtn_Click(object sender, EventArgs e) {
//Code to scroll to Right
}
//I want to know the code to scroll left and the code to scroll right
Note
I wanted to tell that this question didn't help me.
You can try the following code to scroll the FlowLayoutPanel by using the button.
public Form1()
{
InitializeComponent();
// need to disable AutoScroll, otherwise disabling the horizontal scrollbar doesn't work
flowLayoutPanel.AutoScroll = false;
// disable horizontal scrollbar
flowLayoutPanel.HorizontalScroll.Enabled = false;
}
public int scrollValue = 0;
public int ScrollValue
{
get
{
return scrollValue;
}
set
{
scrollValue = value;
if (scrollValue < flowLayoutPanel1.HorizontalScroll.Minimum)
{
scrollValue = flowLayoutPanel1.HorizontalScroll.Minimum;
}
if (scrollValue > flowLayoutPanel1.HorizontalScroll.Maximum)
{
scrollValue = flowLayoutPanel1.HorizontalScroll.Maximum;
}
flowLayoutPanel1.HorizontalScroll.Value = scrollValue;
flowLayoutPanel1.PerformLayout();
}
}
private void btnleft_Click(object sender, EventArgs e)
{
ScrollValue -= flowLayoutPanel1.HorizontalScroll.LargeChange;
}
private void btnright_Click(object sender, EventArgs e)
{
ScrollValue += flowLayoutPanel1.HorizontalScroll.LargeChange;
}
private void button1_Click(object sender, EventArgs e)
{
flowLayoutPanel1.Controls.Add(new Button() { Width = 67, Height = flowLayoutPanel1.Height});
}
Result:
You could place all your controls in a panel that is transparent and is overflowing over the form's bounds (eg. if form is 500px in width and 600px in height you can make the panel be wider than the form and place controls in there). Leave some space under the panel and add your buttons there.
To the LeftScrlBtn_Click block of code add the following: this.panel1.Left--;
Add this to the RightScrlBtn_Click: this.panel1.Left++;
Of course, you can change the incremention value of the panels so it would scroll faster. Just replace ++ with +=5 or -- with -=5 to scroll 5 pixels on click, per example.
Here is a code sample I did (goes into Form1.cs) :
using System.Collections.Generic;
using System.Windows.Forms;
namespace StackOfAnswer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Scrolling left
this.panel1.Left--;
}
private void button2_Click(object sender, EventArgs e)
{
//Scrolling right
this.panel1.Left++;
}
}
}
Here is the code that goes into Form1.Designer.cs :
namespace StackOfAnswer
{
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()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1));
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.Black;
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(-3, -1);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(904, 422);
this.panel1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(24, 427);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 1;
this.button1.Text = "scroll left";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(713, 427);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(75, 23);
this.button2.TabIndex = 1;
this.button2.Text = "scroll right";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.ForeColor = System.Drawing.Color.White;
this.label1.Location = new System.Drawing.Point(24, 136);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(1561, 13);
this.label1.TabIndex = 0;
this.label1.Text = " example example example example example example example example example example example example example example example example example example example example example example example ";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
}
}
This should do it.
I have a bounded DataGridView. How can I add a button in one field with data? I will attach a screenshot of how I see it. Do you have any recommendations on this?
it's WinForms and I think that I need to write a custom column type.
I don't think the standard DataGridViewColumn subclasses provide what you're after.
It can be done, though: you will have to create your own custom Control (I guess a TextBox with a Button right next to it), and the appropriateDataGridViewColumn and DataGridViewCell subclasses to host your custom control.
Follow the documentation for further details.
Of course, the alternative would be using third-party, smarter grids.
Create custom column:
class TextAndButtonControl : UserControl
{
private TextBox textbox1;
private Button button1;
public TextAndButtonControl()
{
this.textbox1 = new TextBox();
this.Controls.Add(this.textbox1);
this.button1 = new Button();
this.Controls.Add(this.button1);
this.RenderControl();
this.button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Hi");
}
public string Text
{
get { return this.textbox1.Text; }
set { this.textbox1.Text = value; }
}
public string ButtonText
{
get { return this.button1.Text; }
set { this.button1.Text = value; }
}
public void RenderControl()
{
this.textbox1.Location = new Point(0, 0);
this.textbox1.Width = 2 * this.Width / 3;
this.textbox1.Height = this.Height;
this.button1.Location = new Point(2 * this.Width / 3, 0);
this.button1.Width = this.Width / 3;
this.button1.Height = this.Height;
}
}
Add the control in the following way
private void Form1_Load(object sender, EventArgs e)
{
TextAndButtonControl bcol = new TextAndButtonControl();
bcol.Text = "Button Column ";
bcol.ButtonText = "Click Me";
bcol.Name = "btnClickMe";
bcol.RenderControl();
dgMainGrid.Controls.Add(bcol);
}
There is a DataGridViewButtonColumn-Type (Or if you just want a single Cell as Button -> DataGridViewButtonCell-Type).
You could create the DataGridButtonColumn and then add it to your DataGridView:
DataGridViewButtonColumn tempBtnColumn = new DataGridViewButtonColumn();
tempBtnColumn.HeaderText = "Button";
tempBtnColumn.Text = "Button-Text";
tempBtnColumn.Name = "Button-Name";
tempBtnColumn.UseColumnTextForButtonValue = true;
Grid.Columns.Add(tempBtnColumn);
//or if you want a specified position for the Grid:
Grid.Columns.Insert(0, tempBtnColumn);
Update
Here is an update, you could give the ButtonCell the Value you want and then read that with CurrentCell.Value (here is a example, hope it's understandable):
private void ButtonCellWithValue()
{
DataGridViewButtonCell dgvbc = new DataGridViewButtonCell();
dgvbc.Value = "1";
DataGridViewRow dgvr = new DataGridViewRow();
dgvr.Cells.Add(dgvbc);
dataGridView1.Rows.Add(dgvr);
dgvbc = new DataGridViewButtonCell();
dgvbc.Value = "2";
dataGridView1.Rows[0].Cells[1] = dgvbc;
GridView1.CellContentClick += new DataGridViewCellEventHandler(dataGridView1_CellContentClick);
}
void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (((DataGridView)sender).CurrentCell.Value == "1")
{
MessageBox.Show("Super");
}
else if (((DataGridView)sender).CurrentCell.Value == "2")
{
MessageBox.Show("Better");
}
}
Thank a lot #RavirajPalvankar.
If somebody will need, I will write the code here, because cann't write it to Ravuraj's comment, because it's long:
Use the class as Raviraj write:
class TextAndButtonControl : UserControl
{
private TextBox textbox1;
private Button button1;
public TextAndButtonControl()
{
this.textbox1 = new TextBox();
this.Controls.Add(this.textbox1);
this.button1 = new Button();
this.Controls.Add(this.button1);
this.renderControl();
this.button1.Click += new EventHandler(button1_Click);
}
void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Click! The value is:" + this.Text);
}
public string Text
{
get { return this.textbox1.Text; }
set { this.textbox1.Text = value; }
}
public string ButtonText
{
get { return this.button1.Text; }
set { this.button1.Text = value; }
}
public void renderControl()
{
this.textbox1.Location = new Point(0, 0);
this.textbox1.Width = 2 * this.Width / 3;
this.textbox1.Height = this.Height;
this.button1.Location = new Point(2 * this.Width / 3, 0);
this.button1.Width = this.Width / 3;
this.button1.Height = this.Height;
}
}
then in main Form:
private void Form1_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
for (int j = 0; j < 20; j++)
{
dt.Rows.Add("col1" + j.ToString(), "col2" + j.ToString());
}
this.dataGridView1.DataSource = dt;
this.dataGridView1.Columns[0].Width = 150;
this.txbtnControl = new TextAndButtonControl();
this.txbtnControl.Visible = false;
this.dataGridView1.Controls.Add(this.txbtnControl);
//Handle the cellbeginEdit event to show the usercontrol in the cell while editing
this.dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);
}
TextAndButtonControl txbtnControl;
void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
if (e.ColumnIndex == 0 && e.RowIndex > -1 && e.RowIndex != this.dataGridView1.NewRowIndex)
{
Rectangle rect = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
this.txbtnControl.Location = rect.Location;
this.txbtnControl.Size = rect.Size;
this.txbtnControl.Text = this.dataGridView1.CurrentCell.Value.ToString();
this.txbtnControl.ButtonText = "...";
this.txbtnControl.renderControl();
this.txbtnControl.Visible = true;
}
}
a few days back i started reading Head first C#, i use visual c# 2015 for building code and learning C# but however the book is based on visual studio 2010, i never ran into any problem so far while learning until i ran into this exercise when i have to build a typing game, i followed all the procedure as mentioned by the book and built it with no errors. but in the end when i run the code a key press from the keyboard should initialize the game but nothing seems t o start the game not even a mouse click or even from virtual keyboard.
Here is the code
namespace WindowsFormsApplication15{
public partial class Form1 : Form
{
Random random = new Random();
Stats stats = new Stats();
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
listBox1.Items.Add((Keys)random.Next(65, 70));
if (listBox1.Items.Count > 7)
{
listBox1.Items.Clear();
listBox1.Items.Add("Game over");
timer1.Stop();
}
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (listBox1.Items.Contains(e.KeyCode))
{
listBox1.Items.Remove(e.KeyCode);
listBox1.Refresh();
if (timer1.Interval > 400)
timer1.Interval -= 10;
if (timer1.Interval > 250)
timer1.Interval -= 7;
if (timer1.Interval > 100)
timer1.Interval -= 2;
difficultyProgressBar.Value = 800 - timer1.Interval;
stats.Update(true);
}
else
{
stats.Update(false);
}
correctLabel.Text = "Correct:" + stats.Correct;
missedLabel.Text = "Missed:" + stats.Missed;
totalLabel.Text = "Total:" + stats.Total;
accuracyLabel.Text = "Accuracy:" + stats.Accuracy + "%";
}
}
}
class for the code
namespace WindowsFormsApplication15{
class Stats
{
public int Total = 0;
public int Missed = 0;
public int Correct = 0;
public int Accuracy = 0;
public void Update(bool correctKey)
{
Total++;
if (!correctKey)
{
Missed++;
}
else
{
Correct++;
}
Accuracy = 100 * Correct / (Missed + Correct);
}
}
}
Form designer code
namespace WindowsFormsApplication15{
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.listBox1 = new System.Windows.Forms.ListBox();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.statusStrip1 = new System.Windows.Forms.StatusStrip();
this.correctLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.missedLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.totalLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.accuracyLabel = new System.Windows.Forms.ToolStripStatusLabel();
this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel();
this.difficultyProgressBar = new System.Windows.Forms.ToolStripProgressBar();
this.statusStrip1.SuspendLayout();
this.SuspendLayout();
//
// listBox1
//
this.listBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listBox1.Font = new System.Drawing.Font("Microsoft Sans Serif", 80.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 120;
this.listBox1.Location = new System.Drawing.Point(0, 0);
this.listBox1.MultiColumn = true;
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(887, 261);
this.listBox1.TabIndex = 0;
//
// timer1
//
this.timer1.Interval = 800;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// statusStrip1
//
this.statusStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.correctLabel,
this.missedLabel,
this.totalLabel,
this.accuracyLabel,
this.toolStripStatusLabel1,
this.difficultyProgressBar});
this.statusStrip1.Location = new System.Drawing.Point(0, 239);
this.statusStrip1.Name = "statusStrip1";
this.statusStrip1.Size = new System.Drawing.Size(887, 22);
this.statusStrip1.SizingGrip = false;
this.statusStrip1.TabIndex = 1;
this.statusStrip1.Text = "statusStrip1";
//
// correctLabel
//
this.correctLabel.Name = "correctLabel";
this.correctLabel.Size = new System.Drawing.Size(58, 17);
this.correctLabel.Text = "Correct: 0";
//
// missedLabel
//
this.missedLabel.Name = "missedLabel";
this.missedLabel.Size = new System.Drawing.Size(56, 17);
this.missedLabel.Text = "Missed: 0";
//
// totalLabel
//
this.totalLabel.Name = "totalLabel";
this.totalLabel.Size = new System.Drawing.Size(45, 17);
this.totalLabel.Text = "Total: 0";
//
// accuracyLabel
//
this.accuracyLabel.Name = "accuracyLabel";
this.accuracyLabel.Size = new System.Drawing.Size(78, 17);
this.accuracyLabel.Text = "Accuracy: 0%";
//
// toolStripStatusLabel1
//
this.toolStripStatusLabel1.Name = "toolStripStatusLabel1";
this.toolStripStatusLabel1.Size = new System.Drawing.Size(533, 17);
this.toolStripStatusLabel1.Spring = true;
this.toolStripStatusLabel1.Text = "Difficulty";
this.toolStripStatusLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
//
// difficultyProgressBar
//
this.difficultyProgressBar.Name = "difficultyProgressBar";
this.difficultyProgressBar.Size = new System.Drawing.Size(100, 16);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(887, 261);
this.Controls.Add(this.statusStrip1);
this.Controls.Add(this.listBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Text = "Form1";
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.Form1_KeyDown);
this.statusStrip1.ResumeLayout(false);
this.statusStrip1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.StatusStrip statusStrip1;
private System.Windows.Forms.ToolStripStatusLabel correctLabel;
private System.Windows.Forms.ToolStripStatusLabel missedLabel;
private System.Windows.Forms.ToolStripStatusLabel totalLabel;
private System.Windows.Forms.ToolStripStatusLabel accuracyLabel;
private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabel1;
private System.Windows.Forms.ToolStripProgressBar difficultyProgressBar;
}
}
I just started learning C# and working with visual studio this month, i don't know much about programming
My guess is the problem lies some where with the KeyDown event
In all of that code I cannot see where timer1 is started - try adding
timer1.Start();
after InitializeComponent();
The problem is simple - your input focus is on the listbox. The guy who wrote the sample probably didn't test it all that much, or you didn't follow the procedure precisely enough :)
To make sure the form receives key press on a child control, you need to set the form's KeyPreview property to true.
Also, as PaulF noted, you never start the timer. The simplest solution is to set Enabled to true.
I'm late to this, but here's what I've got. I'm working through the same book and ran into the same problem, which is that the instructions tell you to set the listbox font at 72pt size. This makes the letters too large to display properly in the listbox on the form, and the Dock Fill property (or some other property, I'm new at C#) causes the ListBox (and its contents) to not display on the form during runtime.
The fix was to reduce ListBox1's font size down to the next size down, at 48pt.
I had the same problem and I had to set the timer to start or enabled and also set the font size to 48pt.
Add one of these lines in Form1.cs underneath InitializeComponent().
this.timer1.Enabled = true;
this.timer1.Start();
I have Label in Panel. In timer.Tick new value write in Label.Text, but in form not change.
Program.cs:
static void Main(string[] args)
{
MainForm mainForm = new MainForm();
Engine engine = new Engine(mainForm);
Application.Run(mainForm);
}
MainForm.cs
public partial class MainForm : Form
{
public MatchesPanel panel;
public MainForm()
{
InitializeComponent();
this.Load += MainForm_Load;
this.panel = new MatchesPanel("panel", new System.Drawing.Point(0, 52));
this.panel.Name = "panel";
this.panel.TabIndex = 8;
this.Controls.Add(this.panel);
}
}
MatchesPanel.cs
public class MatchesPanel : Panel
{
public string Name;
int VWheelSize = 0;
Dictionary<string, Label> Items = new Dictionary<string, Label>();
Label InFocus = null;
public MatchesPanel(string name, Point location)
{
Name = name;
this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Location = location;
this.Margin = new System.Windows.Forms.Padding(0);
this.Size = new System.Drawing.Size(400, 323);
this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
this.MouseWheel += This_MouseWheel;
this.Click += This_Click;
this.Invalidated += MatchesPanel_Invalidated;
}
void Label_Click(object s, EventArgs e)
{
Label l = (Label)s;
l.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
l.BackColor = System.Drawing.Color.MediumTurquoise;
if (InFocus != null && InFocus != l)
{
InFocus.BorderStyle = System.Windows.Forms.BorderStyle.None;
InFocus.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
}
InFocus = l;
l.Focus();
}
public void SetText(string key, string text)
{
Label l = Items[key];
l.Text = text;
}
public void Add(string key, string text)
{
Label label = new Label();
label.AllowDrop = true;
label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
label.Location = new Point(0, Items.Count * 37 + VWheelSize);
label.Margin = new System.Windows.Forms.Padding(0);
label.Padding = new System.Windows.Forms.Padding(3);
label.Size = new System.Drawing.Size(400, 37);
label.Text = text;
label.Click += Label_Click;
label.LostFocus += Label_LostFocus;
label.MouseLeave += Label_MouseLeave;
label.MouseMove += Label_MouseMove;
this.Controls.Add(label);
Items.Add(key, label);
}
public void UpdateMatches(Dictionary<string, Match> Matches)
{
string newStr;
// calculate newStr...
// in debugger i check newStr value and this right
// but text not updating
this.SetText(key, newStr);
}
}
Engine.cs
public class Engine
{
System.Windows.Forms.Timer timer = null;
public Dictionary<string, Match> Matches;
MainForm MainForm = null;
public Engine(MainForm mainForm)
{
MainForm = mainForm;
Matches = new Dictionary<string, Match>();
timer = new System.Windows.Forms.Timer();
timer.Tick += timer_Tick;
timer.Interval = 1000;
timer.Start();
}
public void timer_Tick(object s, EventArgs e)
{
System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
timer.Stop();
Proc();
timer.Start();
}
public async void Proc()
{
// do something...
MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
{
MainForm.panel.UpdateMatches(Matches);
});
}
}
I try use:
Label.Update();
Label.Refresh();
Label.Invalidate();
but it not work.
If I click in label, when it not in focus (InFocus != sourceLabel in clickHandler), text value updating in sourceLabel one time.
Help me pls. I read another topics and not find solve.
If need more code, tell me.
Thx.
[EDIT]
I simplified my code.
Program.cs:
static void Main(string[] args)
{
MainForm mainForm = new MainForm();
Engine engine = new Engine(mainForm);
Application.Run(mainForm);
}
MatchesPanel.cs
public class MatchesPanel : Panel
{
Dictionary<string, Label> Items = new Dictionary<string, Label>();
public MatchesPanel(Point location)
{
this.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
this.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.Location = location;
this.Margin = new System.Windows.Forms.Padding(0);
this.Size = new System.Drawing.Size(400, 323);
this.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)));
}
public void SetText(string key)
{
Label l = Items[key];
l.Text = DateTime.Now.ToString();
}
public void Add(string key)
{
Label label = new Label();
label.Name = key;
label.AllowDrop = true;
label.BackColor = System.Drawing.SystemColors.GradientActiveCaption;
label.Font = new System.Drawing.Font("Consolas", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
label.Location = new Point(0, Items.Count * 37 + VWheelSize);
label.Margin = new System.Windows.Forms.Padding(0);
label.Padding = new System.Windows.Forms.Padding(3);
label.Size = new System.Drawing.Size(400, 37);
label.Text = DateTime.Now.ToString();
this.Controls.Add(label);
Items.Add(key, label);
}
}
Engine.cs
public class Engine
{
System.Windows.Forms.Timer timer = null;
public MatchesPanel panel = null;
MainForm MainForm = null;
public Engine(MainForm mainForm)
{
MainForm = mainForm;
timer = new System.Windows.Forms.Timer();
timer.Tick += timer_Tick;
timer.Interval = 1000;
timer.Start();
}
public void timer_Tick(object s, EventArgs e)
{
System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)s;
timer.Stop();
MainForm.Invoke((System.Windows.Forms.MethodInvoker)delegate()
{
if (panel == null)
{
panel = new MatchesPanel(new System.Drawing.Point(0, 52));
panel.Name = "key1";
// add label in main form
panel.Add("key1");
MainForm.Controls.Add(panel);
}
panel.SetText("key1");
});
timer.Start();
}
}
Problem:
Label.Text not updating.
Solve:
public class UpdatableLabel : Button
{
public UpdatableLabel() : base()
{
FlatAppearance.BorderSize = 0;
FlatStyle = System.Windows.Forms.FlatStyle.Flat;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen pen = new Pen(FlatAppearance.BorderColor, 1);
Rectangle rectangle = new Rectangle(0, 0, Size.Width - 1, Size.Height - 1);
e.Graphics.DrawRectangle(pen, rectangle);
}
}
Now my "label" is updated.
I have a TChart Component, with a Fastline Series and a ColorBand Tool. On the form I also have a buttom which starts a timer. On every timer elapse event I generate 2048 samples of random data and update the Fasline Series. When I start the timer, there is no animation on the TChart! It seems to work randomly, though... And, when I hide and show the form (by minimizing/maximizing, or by tChart1.Hide()/tChart1.Show()) then the animation starts to work again, OR when I drag one of the ColorBand lines BEFORE starting the timer, then the animation works. But the animation does not work when I start the timer first. And, additionaly, when it does not work, the TChart seems to be frozen, i.e, ot does not respond to any mouse commands such as panning or zooming. Here is some code:
In my form.designer.cs:
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.tChart1 = new Steema.TeeChart.TChart();
this.checkBox1 = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// button1
//
this.button1.BackColor = System.Drawing.SystemColors.Control;
this.button1.Location = new System.Drawing.Point(12, 12);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(60, 23);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = false;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tChart1
//
//
//
//
//
//
//
this.tChart1.Axes.Depth.LabelsAsSeriesTitles = true;
//
//
//
this.tChart1.Axes.DepthTop.LabelsAsSeriesTitles = true;
this.tChart1.Location = new System.Drawing.Point(12, 41);
this.tChart1.Name = "tChart1";
this.tChart1.Size = new System.Drawing.Size(789, 318);
this.tChart1.TabIndex = 2;
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Location = new System.Drawing.Point(78, 16);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(49, 17);
this.checkBox1.TabIndex = 3;
this.checkBox1.Text = "Drag";
this.checkBox1.UseVisualStyleBackColor = true;
this.checkBox1.Click += new System.EventHandler(this.checkBox1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(823, 371);
this.Controls.Add(this.checkBox1);
this.Controls.Add(this.tChart1);
this.Controls.Add(this.button1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Fixed3D;
this.Name = "Form1";
this.Text = "Form1";
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.SizeChanged += new System.EventHandler(this.Form1_SizeChanged);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private Steema.TeeChart.TChart tChart1;
private System.Windows.Forms.CheckBox checkBox1;
}
And then in my form.cs:
public partial class Form1 : Form
{
System.Timers.Timer timer;
private Steema.TeeChart.Tools.ColorBand tool;
Steema.TeeChart.Styles.FastLine primaryLine;
double w = 0;
bool enabled = false;
public Form1()
{
InitializeComponent();
initPrimaryGraph();
initTool();
timer = new System.Timers.Timer();
timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
timer.Interval = 50;
timer.Stop();
}
private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
Random rnd = new Random();
for (int i = 0; i < 2048; i++)
{
primaryLine.XValues[i] = i;
primaryLine.YValues[i] = 20 + rnd.Next(50);
}
primaryLine.BeginUpdate();
primaryLine.EndUpdate();
}
private void initTool()
{
tool = new Steema.TeeChart.Tools.ColorBand();
tChart1.Tools.Add(tool);
tool.Axis = tChart1.Axes.Bottom;
tool.Start = 300;
tool.End = 400;
tool.Brush.Color = Color.Yellow;
tool.Pen.Color = Color.Blue;
tool.Pen.Width = 2;
tool.Transparency = 60;
tool.StartLine.AllowDrag = true;
tool.StartLine.DragRepaint = true;
tool.ResizeStart = true;
tool.StartLine.DragLine += new EventHandler(StartLine_DragLine);
tool.EndLine.AllowDrag = true;
tool.EndLine.DragRepaint = true;
tool.ResizeEnd = true;
tool.EndLine.DragLine += new EventHandler(EndLine_DragLine);
}
void StartLine_DragLine(object sender, EventArgs e)
{
if (enabled)
{
tool.End = tool.Start + w;
}
}
void EndLine_DragLine(object sender, EventArgs e)
{
if (enabled)
{
tool.Start = tool.End - w;
}
}
private void initPrimaryGraph()
{
tChart1.Header.Visible = true;
tChart1.Axes.Bottom.Automatic = false;
tChart1.Axes.Bottom.Minimum = 0;
tChart1.Axes.Bottom.Maximum = 2048;
tChart1.Axes.Bottom.Labels.Font.Color = Color.White;
tChart1.Axes.Bottom.Grid.Visible = false;
tChart1.Axes.Left.Automatic = false;
tChart1.Axes.Left.Minimum = 0;
tChart1.Axes.Left.Maximum = 300;
tChart1.Axes.Left.Labels.Font.Color = Color.White;
tChart1.Aspect.View3D = false;
tChart1.Walls.Back.Visible = false;
tChart1.Walls.Bottom.Visible = false;
tChart1.Walls.Left.Visible = false;
tChart1.Walls.Right.Visible = false;
tChart1.Legend.Visible = false;
tChart1.BackColor = Color.Black;
tChart1.Panel.Visible = false;
//PRIMARY GRAPH.....
primaryLine = new Steema.TeeChart.Styles.FastLine();
tChart1.Series.Add(primaryLine);
Random rnd = new Random();
for (int i = 0; i < 2048; i++)
{
double x = i;
double y = 20 + rnd.Next(50);
primaryLine.Add(x, y);
}
primaryLine.LinePen.Style = System.Drawing.Drawing2D.DashStyle.Solid;
primaryLine.LinePen.Color = Color.White;
primaryLine.LinePen.Width = 1;
primaryLine.VertAxis = Steema.TeeChart.Styles.VerticalAxis.Left;
}
private void tool_DragLine(object sender, EventArgs e)
{
Steema.TeeChart.Tools.ColorLine t = sender as Steema.TeeChart.Tools.ColorLine;
this.Text = t.Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
button1.Text = "Start";
}
else
{
timer.Start();
button1.Text = "Stop";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timer.Stop();
}
private void checkBox1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
w = tool.End - tool.Start;
}
enabled = checkBox1.Checked;
}
}
I have another question. I want to create a black-box implementation of a TeeChart Component by writing a custom API (a Custom User Control) to expose specific functionality so that I can use it in other projects, and so that one or more of my colleagues at work can use it in their projects. What version/license of TeeChart should I purchase which would allow me to wrap TeeChart functionality in a custom component/dll which may be used over various projects/computers?
Thanks in advance :-)
I have a TChart Component, with a Fastline Series and a ColorBand
Tool. On the form I also have a buttom which starts a timer. On every
timer elapse event I generate 2048 samples of random data and update
the Fasline Series. When I start the timer, there is no animation on
the TChart! It seems to work randomly, though... And, when I hide and
show the form (by minimizing/maximizing, or by
tChart1.Hide()/tChart1.Show()) then the animation starts to work
again, OR when I drag one of the ColorBand lines BEFORE starting the
timer, then the animation works. But the animation does not work when
I start the timer first. And, additionaly, when it does not work, the
TChart seems to be frozen, i.e, ot does not respond to any mouse
commands such as panning or zooming. Here is some code:
I have modified a little your code and now it works in my end. See next:
Timer timer;
private Steema.TeeChart.Tools.ColorBand tool;
Steema.TeeChart.Styles.FastLine primaryLine;
double w = 0;
bool enabled = false;
public Form1()
{
InitializeComponent();
initPrimaryGraph();
initTool();
timer = new Timer();
//Enable Timer
timer.Enabled = true;
timer.Interval = 50;
timer.Tick += timer_Tick;
}
void timer_Tick(object sender, EventArgs e)
{
AnimateSeries(tChart1);
}
private void AnimateSeries(TChart tChart)
{
Random rnd = new Random();
tChart.AutoRepaint = false;
primaryLine.BeginUpdate();
foreach (Steema.TeeChart.Styles.Series s in tChart.Series)
{
for (int i = 0; i < 2048; i++)
{
primaryLine.XValues[i] = i;
primaryLine.YValues[i] = 20 + rnd.Next(50);
}
}
tChart.AutoRepaint = true;
primaryLine.EndUpdate();
}
private void initTool()
{
tool = new Steema.TeeChart.Tools.ColorBand();
tChart1.Tools.Add(tool);
tool.Axis = tChart1.Axes.Bottom;
tool.Start = 300;
tool.End = 400;
tool.Brush.Color = Color.Yellow;
tool.Pen.Color = Color.Blue;
tool.Pen.Width = 2;
tool.Transparency = 60;
tool.StartLine.AllowDrag = true;
tool.StartLine.DragRepaint = true;
tool.ResizeStart = true;
tool.StartLine.DragLine += new EventHandler(StartLine_DragLine);
tool.EndLine.AllowDrag = true;
tool.EndLine.DragRepaint = true;
tool.ResizeEnd = true;
tool.EndLine.DragLine += new EventHandler(EndLine_DragLine);
}
void StartLine_DragLine(object sender, EventArgs e)
{
if (enabled)
{
tool.End = tool.Start + w;
}
}
void EndLine_DragLine(object sender, EventArgs e)
{
if (enabled)
{
tool.Start = tool.End - w;
}
}
private void initPrimaryGraph()
{
tChart1.Header.Visible = true;
tChart1.Aspect.View3D = false;
tChart1.Walls.Back.Visible = false;
tChart1.Walls.Bottom.Visible = false;
tChart1.Walls.Left.Visible = false;
tChart1.Walls.Right.Visible = false;
tChart1.Legend.Visible = false;
tChart1.BackColor = Color.Black;
tChart1.Panel.Visible = false;
//PRIMARY GRAPH.....
primaryLine = new Steema.TeeChart.Styles.FastLine();
tChart1.Series.Add(primaryLine);
Random rnd = new Random();
for (int i = 0; i < 2048; i++)
{
double x = i;
double y = 20 + rnd.Next(50);
primaryLine.Add(x, y);
}
primaryLine.LinePen.Style = System.Drawing.Drawing2D.DashStyle.Solid;
primaryLine.LinePen.Color = Color.White;
primaryLine.LinePen.Width = 1;
//AXES
tChart1.Axes.Bottom.Automatic = false;
tChart1.Axes.Bottom.Minimum = primaryLine.XValues.Minimum;
tChart1.Axes.Bottom.Maximum = primaryLine.XValues.Maximum;
tChart1.Axes.Bottom.Increment = 200;
tChart1.Axes.Bottom.Labels.Font.Color = Color.White;
tChart1.Axes.Bottom.Grid.Visible = false;
tChart1.Axes.Left.Automatic = false;
tChart1.Axes.Left.Minimum = 0;
tChart1.Axes.Left.Maximum = 300;
tChart1.Axes.Left.Labels.Font.Color = Color.White;
primaryLine.VertAxis = Steema.TeeChart.Styles.VerticalAxis.Left;
tChart1.Draw();
}
private void tool_DragLine(object sender, EventArgs e)
{
Steema.TeeChart.Tools.ColorLine t = sender as Steema.TeeChart.Tools.ColorLine;
this.Text = t.Value.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
if (timer.Enabled)
{
timer.Stop();
button1.Text = "Start";
}
else
{
timer.Start();
button1.Text = "Stop";
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
timer.Stop();
}
As you see in the code I have changed the type of Timer for other I consider more appropriate. Could you tell us if previous code works as you expected?
I have another question. I want to create a black-box implementation
of a TeeChart Component by writing a custom API (a Custom User
Control) to expose specific functionality so that I can use it in
other projects, and so that one or more of my colleagues at work can
use it in their projects. What version/license of TeeChart should I
purchase which would allow me to wrap TeeChart functionality in a
custom component/dll which may be used over various
projects/computers?
TeeChart may be reused by another designtime assembly (dll) that publishes certain characteristics of teeChart. Please not though that machines that reuse tee component in designtime should also install a TeeChart Developer license.
I hope will helps.
Thanks,