I am new to C# and programming in general. I was able to create the required program in Console but want to get one working with Forms as well. I am running into an issue when trying to get int from textboxes.
On Debug I am getting error:
Error 3 'int' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?) D:\Dropbox\Classwork\C_Sharp\InProgress\PaintDeterminator\Paint Determinator Form\Paint Determinator Form\Form1.cs 30 57 Paint Determinator Form
Here is the code I've written so far.
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;
namespace Paint_Determinator_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int LengthtextBox;
int WidthtextBox;
int HeighttextBox;
int paint;
int answer;
LengthtextBox = int.Parse(LengthtextBox.Text);
WidthtextBox = int.Parse(WidthtextBox.Text);
HeighttextBox = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (LengthtextBox * WidthtextBox * HeighttextBox) / paint;
MessageBox.Show( answer.ToString() );
}
}
}
namespace Paint_Determinator_Form
{
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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.WidthtextBox = new System.Windows.Forms.TextBox();
this.HeighttextBox = new System.Windows.Forms.TextBox();
this.LengthtextBox = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(454, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" +
"ields below!";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 91);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Width";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(28, 139);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(38, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Height";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(28, 183);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Length";
//
// WidthtextBox
//
this.WidthtextBox.Location = new System.Drawing.Point(175, 83);
this.WidthtextBox.Name = "WidthtextBox";
this.WidthtextBox.Size = new System.Drawing.Size(100, 20);
this.WidthtextBox.TabIndex = 5;
//
// HeighttextBox
//
this.HeighttextBox.Location = new System.Drawing.Point(175, 131);
this.HeighttextBox.Name = "HeighttextBox";
this.HeighttextBox.Size = new System.Drawing.Size(100, 20);
this.HeighttextBox.TabIndex = 6;
//
// LengthtextBox
//
this.LengthtextBox.Location = new System.Drawing.Point(175, 183);
this.LengthtextBox.Name = "LengthtextBox";
this.LengthtextBox.Size = new System.Drawing.Size(100, 20);
this.LengthtextBox.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(349, 402);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "Paint";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(511, 447);
this.Controls.Add(this.button1);
this.Controls.Add(this.LengthtextBox);
this.Controls.Add(this.HeighttextBox);
this.Controls.Add(this.WidthtextBox);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox WidthtextBox;
private System.Windows.Forms.TextBox HeighttextBox;
private System.Windows.Forms.TextBox LengthtextBox;
private System.Windows.Forms.Button button1;
}
Why do you name your ints like your textboxes? It is really a bad practice and confusing at the uttermost level. As you can see, the compiler thinks that you are using the int variables instead of the textboxes and complains that an int type have no property called Text.
So simply change the name of the ints inside the click method
private void button1_Click(object sender, EventArgs e)
{
int l;
int w;
int h;
int paint;
int answer;
l = int.Parse(LengthtextBox.Text);
w = int.Parse(WidthtextBox.Text);
h = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (l * w * h) / paint;
MessageBox.Show( answer.ToString() );
}
Said that, I suggest to use Int32.TryParse to convert the data typed by your user in a valid integer. The Parse method will throw an exception if your user types something that cannot be translated to an integer, instead TryParse returns false without a costly exception
For example
int l;
if(!Int32.TryParse(LengthtextBox.Text, out l))
{
MessageBox.Show("Please type a valid number for Length");
return;
}
When the Int32.TryParse returns, the out parameter (l) contains the
32-bit signed integer value equivalent of the number contained in your
textbox, if the conversion succeeds, or zero if the conversion fails
You're declaring locally scoped variables that will replace your actual textboxes. Yo ushould use local variables that have different names like:
int length = int.Parse(LengthtextBox.Text);
int width = int.Parse(WidthtextBox.Text);;
int height = int.Parse(HeighttextBox.Text);;
I updated your code, you were using variables with the same names as your actual text boxes... That's not a good idea:
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;
namespace Paint_Determinator_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int length;
int width;
int height;
int paint;
int answer;
length = int.Parse(LengthtextBox.Text);
width = int.Parse(WidthtextBox.Text);
height = int.Parse(HeighttextBox.Text);
paint = 350;
answer = (length* width* height) / paint;
MessageBox.Show( answer.ToString() );
}
}
}
namespace Paint_Determinator_Form
{
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.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.WidthtextBox = new System.Windows.Forms.TextBox();
this.HeighttextBox = new System.Windows.Forms.TextBox();
this.LengthtextBox = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 29);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(454, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Welcome to Paint Determinator! Please enter the measurements in the appropriate f" +
"ields below!";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(28, 91);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 1;
this.label2.Text = "Width";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(28, 139);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(38, 13);
this.label3.TabIndex = 2;
this.label3.Text = "Height";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(28, 183);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(40, 13);
this.label4.TabIndex = 3;
this.label4.Text = "Length";
//
// WidthtextBox
//
this.WidthtextBox.Location = new System.Drawing.Point(175, 83);
this.WidthtextBox.Name = "WidthtextBox";
this.WidthtextBox.Size = new System.Drawing.Size(100, 20);
this.WidthtextBox.TabIndex = 5;
//
// HeighttextBox
//
this.HeighttextBox.Location = new System.Drawing.Point(175, 131);
this.HeighttextBox.Name = "HeighttextBox";
this.HeighttextBox.Size = new System.Drawing.Size(100, 20);
this.HeighttextBox.TabIndex = 6;
//
// LengthtextBox
//
this.LengthtextBox.Location = new System.Drawing.Point(175, 183);
this.LengthtextBox.Name = "LengthtextBox";
this.LengthtextBox.Size = new System.Drawing.Size(100, 20);
this.LengthtextBox.TabIndex = 7;
//
// button1
//
this.button1.Location = new System.Drawing.Point(349, 402);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 9;
this.button1.Text = "Paint";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(511, 447);
this.Controls.Add(this.button1);
this.Controls.Add(this.LengthtextBox);
this.Controls.Add(this.HeighttextBox);
this.Controls.Add(this.WidthtextBox);
this.Controls.Add(this.label4);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox WidthtextBox;
private System.Windows.Forms.TextBox HeighttextBox;
private System.Windows.Forms.TextBox LengthtextBox;
private System.Windows.Forms.Button button1;
}
Remove those definitions from button1_Click() method as you use the same names as the class variables inside this method:
int LengthtextBox;
int WidthtextBox;
int HeighttextBox;
And change the name of the variables:
int length;
int width;
int height;
length= int.Parse(LengthtextBox.Text);
width= int.Parse(WidthtextBox.Text);
height= int.Parse(HeighttextBox.Text);
paint = 350;
answer = (length* width* height) / paint;
In your situatuation it's better to rename ints, but you are ablealso to use this in simmilar situation
LengthtextBox = int.Parse(this.LengthtextBox.Text);
WidthtextBox = int.Parse(this.WidthtextBox.Text);
HeighttextBox = int.Parse(this.HeighttextBox.Text);
I think if you are new in programing it's good to know about this too, instead just renaming variables without understanding.
Related
I have a tabControl in a Winform application. The user is able to change settings that will impact each tab visually, and I need to get each tabs as images.
I'm trying to understand why/how some controls are updating even if not visible and some other don't.
Is there a way to force an existing UserControl to update/redraw even if not visible so that Control.DrawToBitmap(...) gets a valid image?
I created an example with a Chart and a FlowLayoutPanel to explain what I mean and to make sure I could reproduce the issue I have.
In the example below:
The image created from the FlowLayoutPanel is only correct if it was updated and visible before DrawToBitmap is called.
The image from the Chart is alway up to date even if updated when not visible.
Questions:
How does it work with one control and not the other?
How could I ensure the FlowLayoutPanel has the same behavior as the Chart?
(EDIT) I have tried a few things without success:
Suspend/Resume layout when updating
call Refresh / Update / Invalidate on the layout that do not update as expected
Here is a gif showing that the chart get updated even when not visible but not the FlowLayoutPanel:
Here is the code of this example:
using LiveChartsCore.SkiaSharpView;
using System.Collections.ObjectModel;
namespace WinFormsDrawToBitmapTest
{
public partial class Form1 : Form
{
int legendCount = 1;
ViewModel viewModel;
public Form1()
{
InitializeComponent();
viewModel = new ViewModel();
cartesianChart1.Series = viewModel.Series;
updateLegend(this.flowLayoutPanel1);
}
private async void button1_Click(object sender, EventArgs e)
{
Bitmap bitmap = await Task.Run(() => getImg3());
this.pictureBox1.Image = bitmap;
this.pictureBox1.Invoke((MethodInvoker)delegate
{ this.pictureBox1.Image = bitmap; });
}
private Bitmap getImg3()
{
Bitmap bitmap = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height + this.flowLayoutPanel1.Height);
Bitmap chart = new Bitmap(this.cartesianChart1.Width, this.cartesianChart1.Height);
Bitmap legend = new Bitmap(this.flowLayoutPanel1.Width, this.flowLayoutPanel1.Height);
this.cartesianChart1.Invoke((MethodInvoker)delegate
{
this.cartesianChart1.DrawToBitmap(chart, new Rectangle(new Point(0, 0), this.cartesianChart1.Size));
});
this.flowLayoutPanel1.Invoke((MethodInvoker)delegate
{
this.flowLayoutPanel1.DrawToBitmap(legend, new Rectangle(new Point(0, 0), this.flowLayoutPanel1.Size));
});
using (Graphics g = Graphics.FromImage(bitmap))
{
g.DrawImage(legend, 0, 0);
g.DrawImage(chart, 0, legend.Height);
}
return bitmap;
}
private void button2_Click(object sender, EventArgs e)
{
Random r = new Random();
viewModel.Data.Add(r.Next(0, 10));
legendCount++;
updateLegend(this.flowLayoutPanel1);
}
private void updateLegend(FlowLayoutPanel flow)
{
flow.Controls.Clear();
Color[] colorList = new Color[]
{
Color.FromArgb(15, 1, 215),
Color.FromArgb(255, 0, 0),
Color.FromArgb(0, 176, 80),
Color.FromArgb(112, 48, 160)
};
for (int i = 0; i < legendCount; i++)
{
//flow.Controls.Add(new LegendLabel($"Label {i}", colorList[i % colorList.Length]));
Button btn = new Button();
btn.Name = $"Button {i}";
btn.TabIndex = 0;
btn.Text = $"Button {i}";
flow.Controls.Add(btn);
}
}
}
public partial class ViewModel
{
public ObservableCollection<double> Data { get; set; }
public List<LineSeries<double>> Series { get; set; }
public ViewModel()
{
Data = new ObservableCollection<double>();
Data.Add(1); Data.Add(2); Data.Add(5); Data.Add(4);
Series = new List<LineSeries<double>>();
Series.Add(new LineSeries<double> { Values = Data, Fill = null });
}
}
}
With the associated .Designer.cs in case it helps
namespace WinFormsDrawToBitmapTest
{
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.panel2 = new System.Windows.Forms.Panel();
this.button2 = new System.Windows.Forms.Button();
this.button1 = new System.Windows.Forms.Button();
this.tabControl1 = new System.Windows.Forms.TabControl();
this.tabPage1 = new System.Windows.Forms.TabPage();
this.cartesianChart1 = new LiveChartsCore.SkiaSharpView.WinForms.CartesianChart();
this.flowLayoutPanel1 = new System.Windows.Forms.FlowLayoutPanel();
this.tabPage2 = new System.Windows.Forms.TabPage();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel2.SuspendLayout();
this.tabControl1.SuspendLayout();
this.tabPage1.SuspendLayout();
this.tabPage2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// panel2
//
this.panel2.Controls.Add(this.button2);
this.panel2.Controls.Add(this.button1);
this.panel2.Dock = System.Windows.Forms.DockStyle.Top;
this.panel2.Location = new System.Drawing.Point(0, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(1042, 24);
this.panel2.TabIndex = 1;
//
// button2
//
this.button2.Location = new System.Drawing.Point(167, 0);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(86, 23);
this.button2.TabIndex = 1;
this.button2.Text = "Change Tab1";
this.button2.UseVisualStyleBackColor = true;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(0, 0);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(161, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Get Tab1 as Image in Tab 2";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// tabControl1
//
this.tabControl1.Controls.Add(this.tabPage1);
this.tabControl1.Controls.Add(this.tabPage2);
this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tabControl1.Location = new System.Drawing.Point(0, 24);
this.tabControl1.Name = "tabControl1";
this.tabControl1.SelectedIndex = 0;
this.tabControl1.Size = new System.Drawing.Size(1042, 585);
this.tabControl1.TabIndex = 2;
//
// tabPage1
//
this.tabPage1.Controls.Add(this.cartesianChart1);
this.tabPage1.Controls.Add(this.flowLayoutPanel1);
this.tabPage1.Location = new System.Drawing.Point(4, 24);
this.tabPage1.Name = "tabPage1";
this.tabPage1.Padding = new System.Windows.Forms.Padding(3);
this.tabPage1.Size = new System.Drawing.Size(1034, 557);
this.tabPage1.TabIndex = 0;
this.tabPage1.Text = "tabPage1";
this.tabPage1.UseVisualStyleBackColor = true;
//
// cartesianChart1
//
this.cartesianChart1.Location = new System.Drawing.Point(8, 116);
this.cartesianChart1.Name = "cartesianChart1";
this.cartesianChart1.Size = new System.Drawing.Size(1018, 435);
this.cartesianChart1.TabIndex = 4;
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Location = new System.Drawing.Point(8, 6);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(1018, 100);
this.flowLayoutPanel1.TabIndex = 3;
//
// tabPage2
//
this.tabPage2.Controls.Add(this.pictureBox1);
this.tabPage2.Location = new System.Drawing.Point(4, 24);
this.tabPage2.Name = "tabPage2";
this.tabPage2.Padding = new System.Windows.Forms.Padding(3);
this.tabPage2.Size = new System.Drawing.Size(1034, 557);
this.tabPage2.TabIndex = 1;
this.tabPage2.Text = "tabPage2";
this.tabPage2.UseVisualStyleBackColor = true;
//
// pictureBox1
//
this.pictureBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.pictureBox1.Location = new System.Drawing.Point(3, 3);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1028, 551);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1042, 609);
this.Controls.Add(this.tabControl1);
this.Controls.Add(this.panel2);
this.Name = "Form1";
this.Text = "Form1";
this.panel2.ResumeLayout(false);
this.tabControl1.ResumeLayout(false);
this.tabPage1.ResumeLayout(false);
this.tabPage2.ResumeLayout(false);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private Panel panel2;
private Button button1;
private TabControl tabControl1;
private TabPage tabPage1;
private TabPage tabPage2;
private PictureBox pictureBox1;
private FlowLayoutPanel flowLayoutPanel1;
private LiveChartsCore.SkiaSharpView.WinForms.CartesianChart cartesianChart1;
private Button button2;
}
}
Thank you for your help with this.
I am developing a simple windows form application. But I am not able to center align the contents of the form. here are the images:
Here's the code of the form:
namespace WindowsFormsApplication1
{
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.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(1, 1);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
//
// button1
//
this.button1.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("button1.BackgroundImage")));
this.button1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center;
this.button1.FlatAppearance.BorderColor = System.Drawing.SystemColors.Highlight;
this.button1.FlatAppearance.BorderSize = 5;
this.button1.FlatAppearance.MouseOverBackColor = System.Drawing.SystemColors.HotTrack;
this.button1.ForeColor = System.Drawing.Color.Coral;
this.button1.Image = ((System.Drawing.Image)(resources.GetObject("button1.Image")));
this.button1.Location = new System.Drawing.Point(330, 275);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(97, 123);
this.button1.TabIndex = 1;
this.button1.UseMnemonic = false;
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// pictureBox1
//
this.pictureBox1.Enabled = false;
this.pictureBox1.Image = ((System.Drawing.Image)(resources.GetObject("pictureBox1.Image")));
this.pictureBox1.ImageLocation = "";
this.pictureBox1.Location = new System.Drawing.Point(270, 27);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(221, 176);
this.pictureBox1.TabIndex = 2;
this.pictureBox1.TabStop = false;
this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("OCR A Extended", 14.25F, System.Drawing.FontStyle.Italic, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(251, 226);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(251, 20);
this.label1.TabIndex = 3;
this.label1.Text = "Please Touch Your Card";
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.ForeColor = System.Drawing.Color.Red;
this.label2.Location = new System.Drawing.Point(359, 433);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(35, 13);
this.label2.TabIndex = 4;
this.label2.Text = "label2";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Highlight;
this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.ClientSize = new System.Drawing.Size(752, 502);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.pictureBox1);
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.WindowsDefaultBounds;
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.PictureBox pictureBox1;
}
}
I tried using this,
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
But even that did not help.
I suggest you try the TableLayoutPanel, if you add one you shall get a small 2x2 grid. If you right click and say edit rows and columns you can add a third column and the amount of rows you need. for the settings of the panel I'd suggest something along these lines:
column1 : 50%
column2 : autosize
column3 : 50%
This will make it so the middle column is the minimal required size for your controls, and the remainder of the width is divided over the other 2 columns. Don't forget to dock the tableLayoutPanel to the form.
Thought I'd post it as an awnser instead of the comment I left before.
One way to do is to use TableLayoutPanel as #maam27 commented. Another way is to use the Anchor property of the control. By default a control is anchored to the top-left corner of the parent form. So when form resizes, that point remains fixed. You may choose to break the left anchor. Manually center-align your components in designer. After that if your form resizes, then the relative position will remain unchanged.
you take a DIV than assign align inline property for label
I'm trying to create a project in c# , mainly a memory game. It was all going pretty well till i came upon an issue that i haven't seen anyone else have and that really baffles me since i don't think i'm doing anything really complicated. On my "Form" are 16 labels , all with a single method on click. When i run my program , the click part never happens. The buttons work fine and the line-by-line debug starts when they are clicked on.But on the labels it just does nothing , never calls it.Here are a few things that might be needed:
Forum1.Designer.cs*
namespace MatchingGame1
{
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.button2 = new System.Windows.Forms.Button();
this.label5 = new System.Windows.Forms.Label();
this.label7 = new System.Windows.Forms.Label();
this.label9 = new System.Windows.Forms.Label();
this.label10 = new System.Windows.Forms.Label();
this.label11 = new System.Windows.Forms.Label();
this.label12 = new System.Windows.Forms.Label();
this.label13 = new System.Windows.Forms.Label();
this.label14 = new System.Windows.Forms.Label();
this.label15 = new System.Windows.Forms.Label();
this.label16 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label8 = new System.Windows.Forms.Label();
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(567, 57);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(97, 69);
this.button1.TabIndex = 1;
this.button1.Text = "Start";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Enabled = false;
this.button2.Location = new System.Drawing.Point(570, 152);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(93, 81);
this.button2.TabIndex = 2;
this.button2.Text = "Hide";
this.button2.UseVisualStyleBackColor = true;
this.button2.Visible = false;
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// label5
//
this.label5.Dock = System.Windows.Forms.DockStyle.Fill;
this.label5.Location = new System.Drawing.Point(5, 129);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(125, 125);
this.label5.TabIndex = 4;
this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label5.Click += new System.EventHandler(this.label1_Click);
//
// label7
//
this.label7.Dock = System.Windows.Forms.DockStyle.Fill;
this.label7.Location = new System.Drawing.Point(271, 129);
this.label7.Name = "label7";
this.label7.Size = new System.Drawing.Size(125, 125);
this.label7.TabIndex = 6;
this.label7.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label7.Click += new System.EventHandler(this.label1_Click);
//
// label9
//
this.label9.Dock = System.Windows.Forms.DockStyle.Fill;
this.label9.Location = new System.Drawing.Point(5, 256);
this.label9.Name = "label9";
this.label9.Size = new System.Drawing.Size(125, 125);
this.label9.TabIndex = 8;
this.label9.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label9.Click += new System.EventHandler(this.label1_Click);
//
// label10
//
this.label10.Dock = System.Windows.Forms.DockStyle.Fill;
this.label10.Location = new System.Drawing.Point(138, 256);
this.label10.Name = "label10";
this.label10.Size = new System.Drawing.Size(125, 125);
this.label10.TabIndex = 9;
this.label10.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label10.Click += new System.EventHandler(this.label1_Click);
//
// label11
//
this.label11.Dock = System.Windows.Forms.DockStyle.Fill;
this.label11.Location = new System.Drawing.Point(271, 256);
this.label11.Name = "label11";
this.label11.Size = new System.Drawing.Size(125, 125);
this.label11.TabIndex = 10;
this.label11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label11.Click += new System.EventHandler(this.label1_Click);
//
// label12
//
this.label12.Dock = System.Windows.Forms.DockStyle.Fill;
this.label12.Location = new System.Drawing.Point(404, 256);
this.label12.Name = "label12";
this.label12.Size = new System.Drawing.Size(125, 125);
this.label12.TabIndex = 11;
this.label12.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label12.Click += new System.EventHandler(this.label1_Click);
//
// label13
//
this.label13.Dock = System.Windows.Forms.DockStyle.Fill;
this.label13.Location = new System.Drawing.Point(5, 383);
this.label13.Name = "label13";
this.label13.Size = new System.Drawing.Size(125, 127);
this.label13.TabIndex = 12;
this.label13.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label13.Click += new System.EventHandler(this.label1_Click);
//
// label14
//
this.label14.Dock = System.Windows.Forms.DockStyle.Fill;
this.label14.Location = new System.Drawing.Point(138, 383);
this.label14.Name = "label14";
this.label14.Size = new System.Drawing.Size(125, 127);
this.label14.TabIndex = 13;
this.label14.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label14.Click += new System.EventHandler(this.label1_Click);
//
// label15
//
this.label15.Dock = System.Windows.Forms.DockStyle.Fill;
this.label15.Location = new System.Drawing.Point(271, 383);
this.label15.Name = "label15";
this.label15.Size = new System.Drawing.Size(125, 127);
this.label15.TabIndex = 14;
this.label15.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label15.Click += new System.EventHandler(this.label1_Click);
//
// label16
//
this.label16.Dock = System.Windows.Forms.DockStyle.Fill;
this.label16.Location = new System.Drawing.Point(404, 383);
this.label16.Name = "label16";
this.label16.Size = new System.Drawing.Size(125, 127);
this.label16.TabIndex = 15;
this.label16.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label16.Click += new System.EventHandler(this.label1_Click);
//
// label6
//
this.label6.Dock = System.Windows.Forms.DockStyle.Fill;
this.label6.Location = new System.Drawing.Point(138, 129);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(125, 125);
this.label6.TabIndex = 5;
this.label6.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label6.Click += new System.EventHandler(this.label1_Click);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Location = new System.Drawing.Point(5, 2);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(125, 125);
this.label1.TabIndex = 0;
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label1.Click += new System.EventHandler(this.label1_Click);
//
// label2
//
this.label2.Dock = System.Windows.Forms.DockStyle.Fill;
this.label2.Location = new System.Drawing.Point(138, 2);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(125, 125);
this.label2.TabIndex = 1;
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label2.Click += new System.EventHandler(this.label1_Click);
//
// label3
//
this.label3.Dock = System.Windows.Forms.DockStyle.Fill;
this.label3.Location = new System.Drawing.Point(271, 2);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(125, 125);
this.label3.TabIndex = 2;
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label3.Click += new System.EventHandler(this.label1_Click);
//
// label4
//
this.label4.Dock = System.Windows.Forms.DockStyle.Fill;
this.label4.Location = new System.Drawing.Point(404, 2);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(125, 125);
this.label4.TabIndex = 3;
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label4.Click += new System.EventHandler(this.label1_Click);
//
// label8
//
this.label8.Dock = System.Windows.Forms.DockStyle.Fill;
this.label8.Location = new System.Drawing.Point(404, 129);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(125, 125);
this.label8.TabIndex = 7;
this.label8.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
this.label8.Click += new System.EventHandler(this.label1_Click);
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.AccessibleRole = System.Windows.Forms.AccessibleRole.None;
this.tableLayoutPanel1.BackColor = System.Drawing.SystemColors.HotTrack;
this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Inset;
this.tableLayoutPanel1.ColumnCount = 4;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Controls.Add(this.label8, 3, 1);
this.tableLayoutPanel1.Controls.Add(this.label13, 0, 3);
this.tableLayoutPanel1.Controls.Add(this.label14, 1, 3);
this.tableLayoutPanel1.Controls.Add(this.label15, 2, 3);
this.tableLayoutPanel1.Controls.Add(this.label16, 3, 3);
this.tableLayoutPanel1.Controls.Add(this.label12, 3, 2);
this.tableLayoutPanel1.Controls.Add(this.label11, 2, 2);
this.tableLayoutPanel1.Controls.Add(this.label10, 1, 2);
this.tableLayoutPanel1.Controls.Add(this.label9, 0, 2);
this.tableLayoutPanel1.Controls.Add(this.label5, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.label6, 1, 1);
this.tableLayoutPanel1.Controls.Add(this.label7, 2, 1);
this.tableLayoutPanel1.Controls.Add(this.label4, 3, 0);
this.tableLayoutPanel1.Controls.Add(this.label3, 2, 0);
this.tableLayoutPanel1.Controls.Add(this.label1, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.label2, 1, 0);
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 4;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 25F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(534, 512);
this.tableLayoutPanel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(128)))), ((int)(((byte)(0)))));
this.ClientSize = new System.Drawing.Size(671, 512);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "Form1";
this.Text = "Matching Game";
this.tableLayoutPanel1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.Label label9;
private System.Windows.Forms.Label label10;
private System.Windows.Forms.Label label11;
private System.Windows.Forms.Label label12;
private System.Windows.Forms.Label label13;
private System.Windows.Forms.Label label14;
private System.Windows.Forms.Label label15;
private System.Windows.Forms.Label label16;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label8;
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
}
}
Form1.cs
using System;
using System.IO;
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;
namespace MatchingGame1
{
public partial class Form1 : Form
{
Random random = new Random();
private List<Image> takeIm()
{
string directory = #".\";
List<Image> ImageList = new List<Image>();
foreach (string myFile in Directory.GetFiles(directory, "*.jpg", SearchOption.AllDirectories))
{
ImageList.Add(Image.FromFile(myFile));
ImageList.Add(Image.FromFile(myFile));
}
return ImageList;
}
private void AssignImagesToSquares(List<Image> a)
{
foreach (Control control in tableLayoutPanel1.Controls)
{
Label imageLabel = control as Label;
if (imageLabel != null)
{
int randomNumber = random.Next(a.Count);
imageLabel.Image = a[randomNumber];
a.RemoveAt(randomNumber);
}
}
}
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Label clickedLabel = sender as Label;
clickedLabel.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
List<Image> a = new List<Image>();
a = takeIm();
AssignImagesToSquares(a);
button2.Visible = true;
button2.Enabled = true;
button1.Visible = false;
button1.Enabled = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Enabled = false;
button2.Visible = false;
foreach (Control control in tableLayoutPanel1.Controls)
{
Label imageLabel = control as Label;
imageLabel.Visible = false;
}
}
}
}
I assume your labels was hidden before you try click on it
Control will not fire event Click if property Visible set to false.
If you want show/hidden control by clicking on it, then try to play with background color or picture in the control.
Hidden - no picture/transparent
Visible - picture or some background color will be shown
I'm looking for the fastest way to refresh an image (and draw some shapes) on a Winform. I'm using a PictureBox now, but I'm open for suggestions.
The way I'm getting the max FPS possible I took from here: "My last post on render loops (hopefully)"
This is a game loop pattern, but it can also be used to other purpose, like to display live images acquired from a camera.
I'm drawing a red circle in different positions.
As you can see, the FPS drops a lot when a Bitmap is loaded at the PictureBox. Especially in the SizeMode Zoom (the one I want to use).
My PC values for FPS:
Form size (start size)
Bitmap ON: 140 (Zoom); 1000 (Normal); 135 (Stretch); 880 (Auto); 1000 (Center)
Bitmap OFF: 3400 !!
Form size (after Zoom 100% click)
Bitmap ON: 40 (Zoom); 150 (Normal); 65 (Stretch); 150 (Auto); 150 (Center)
Bitmap OFF: 540 !!
Edit 1:
Well, this results was for a 1024x1024 image. But I realizy that the form Height was not getting the right value, I found out that it is because the form size is limited by the Screen resolution. So I've edited the code to load a 800x600 Bitmap. Now the Zoom 100% button works. As you can see, at zoom 100% the FPS is 350, but if you increase the size of the PictureBox one more pixel, the FPS drops to 35, about 10x slower ¬¬ !
The big question is: How can I improve the FPS, specially in zoom mode?
PS: I know WinForm is not the best way to go, I also know WPF. But for now I'm looking the solution using WinForm. Ok? The reason is that I have a big scary project using it. I'm planing to move to WPF, but I'm still learning it. :)
Here is the full code, so you can test it by yourself.
Form1.cs:
//.NET 4.0 Client Profile
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Windows.Forms;
//You need to add System.Activities.Presentation.dll
using System.Activities.Presentation.Hosting;
namespace WindowsFormsApplication2
{
public enum DrawSource
{
None = 0,
PictureBox = 1,
Grapghics = 2,
}
public partial class Form1 : Form
{
private readonly Timer _updateFPSTimer;
private bool _isIdle;
private Point _location;
private double _updateCount;
private readonly Bitmap _backImage;
public Form1()
{
InitializeComponent();
this.DoubleBuffered = this.ckbDoubleBufferForm.Checked;
this.pictureBox1.DoubleBuffered = this.ckbDoubleBufferPictureBox.Checked;
_backImage = new Bitmap(800, 600, PixelFormat.Format24bppRgb);
Graphics g = Graphics.FromImage(_backImage);
int penWidth = 10;
Pen pen = new Pen(Brushes.Blue, penWidth);
g.DrawRectangle(pen, new Rectangle(new Point(penWidth, penWidth), new Size(_backImage.Size.Width - 2 * penWidth, _backImage.Size.Height - 2 * penWidth)));
this.cbxSizeMode.DataSource = Enum.GetValues(typeof(PictureBoxSizeMode));
this.cbxSizeMode.SelectedItem = PictureBoxSizeMode.Zoom;
this.cbxBitmapDrawSource.DataSource = Enum.GetValues(typeof(DrawSource));
this.cbxBitmapDrawSource.SelectedItem = DrawSource.PictureBox;
this.pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
Application.Idle += Application_Idle;
_updateFPSTimer = new Timer();
_updateFPSTimer.Tick += UpdateFPSTimer_Tick;
_updateFPSTimer.Interval = Convert.ToInt32(1000.0 / 5); //Period in s = 1 s / 5 Hz
_updateFPSTimer.Start();
}
private void Application_Idle(object sender, EventArgs e)
{
while (this.ckbRun.Checked && IsAppStillIdle())
this.pictureBox1.Refresh();
}
void UpdateFPSTimer_Tick(object sender, EventArgs e)
{
GetFPS();
}
private void GetFPS()
{
double fps = _updateCount / (Convert.ToDouble(_updateFPSTimer.Interval) / 1000.0);
_updateCount = 0;
lblFps.Text = fps.ToString("0.00");
}
private void PictureBox1_Paint(object sender, PaintEventArgs e)
{
DrawCircle(e.Graphics);
}
private void DrawCircle(Graphics g)
{
if (this.pictureBox1.Image == null && ((DrawSource)this.cbxBitmapDrawSource.SelectedItem == DrawSource.Grapghics))
g.DrawImage(_backImage, 0, 0, g.ClipBounds.Width, g.ClipBounds.Height);
var rect = new Rectangle(_location, new Size(30, 30));
g.DrawEllipse(Pens.Red, rect);
_location.X += 1;
_location.Y += 1;
if (_location.X > g.ClipBounds.Width)
_location.X = 0;
if (_location.Y > g.ClipBounds.Height)
_location.Y = 0;
_updateCount++;
}
/// <summary>
/// Gets if the app still idle.
/// http://blogs.msdn.com/b/tmiller/archive/2005/05/05/415008.aspx
/// </summary>
/// <returns></returns>
private bool IsAppStillIdle()
{
Message msg;
return !PeekMessage(out msg, IntPtr.Zero, 0, 0, 0);
}
#region Unmanaged Get PeekMessage
// http://blogs.msdn.com/b/tmiller/archive/2005/05/05/415008.aspx
[System.Security.SuppressUnmanagedCodeSecurity] // We won't use this maliciously
[DllImport("User32.dll", CharSet = CharSet.Auto)]
public static extern bool PeekMessage(out Message msg, IntPtr hWnd, uint messageFilterMin, uint messageFilterMax, uint flags);
#endregion
private void btnZoomReset_Click(object sender, EventArgs e)
{
if (_backImage != null)
{
//Any smarter way to do this?
//Note that the maximum Form size is limmited by designe by the Screen resolution.
//Rectangle screenRectangle = RectangleToScreen(this.ClientRectangle);
//int titleHeight = screenRectangle.Top - this.Top;
//Borders
Size border = new Size();
border.Width = this.Width - this.pictureBox1.Width;
border.Height = this.Height - this.pictureBox1.Height;
this.Width = border.Width + _backImage.Width;
this.Height = border.Height + _backImage.Height;
Console.WriteLine("PictureBox size: " + this.pictureBox1.Size.ToString());
}
}
private void SizeMode_SelectedIndexChanged(object sender, EventArgs e)
{
PictureBoxSizeMode mode;
Enum.TryParse<PictureBoxSizeMode>(cbxSizeMode.SelectedValue.ToString(), out mode);
this.pictureBox1.SizeMode = mode;
}
private void DoubleBufferForm_CheckedChanged(object sender, EventArgs e)
{
this.DoubleBuffered = this.ckbDoubleBufferForm.Checked;
}
private void DoubleBufferPictureBox_CheckedChanged(object sender, EventArgs e)
{
this.pictureBox1.DoubleBuffered = this.ckbDoubleBufferPictureBox.Checked;
}
private void BitmapDrawSource_SelectedIndexChanged(object sender, EventArgs e)
{
if ((DrawSource)this.cbxBitmapDrawSource.SelectedItem == DrawSource.PictureBox)
this.pictureBox1.Image = _backImage;
else
this.pictureBox1.Image = null;
}
}
}
Designer.CS:
namespace WindowsFormsApplication2
{
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.pictureBox1 = new WindowsFormsApplication2.PictureBoxDoubleBuffer();
this.label1 = new System.Windows.Forms.Label();
this.lblFps = new System.Windows.Forms.Label();
this.ckbRun = new System.Windows.Forms.CheckBox();
this.btnZoomReset = new System.Windows.Forms.Button();
this.label2 = new System.Windows.Forms.Label();
this.cbxSizeMode = new System.Windows.Forms.ComboBox();
this.gpbDoubleBuffer = new System.Windows.Forms.GroupBox();
this.ckbDoubleBufferPictureBox = new System.Windows.Forms.CheckBox();
this.ckbDoubleBufferForm = new System.Windows.Forms.CheckBox();
this.cbxBitmapDrawSource = new System.Windows.Forms.ComboBox();
this.label3 = new System.Windows.Forms.Label();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.gpbDoubleBuffer.SuspendLayout();
this.SuspendLayout();
//
// pictureBox1
//
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlDarkDark;
this.pictureBox1.DoubleBuffered = true;
this.pictureBox1.Location = new System.Drawing.Point(8, 84);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(347, 215);
this.pictureBox1.TabIndex = 0;
this.pictureBox1.TabStop = false;
this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.PictureBox1_Paint);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(134, 13);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(30, 13);
this.label1.TabIndex = 3;
this.label1.Text = "FPS:";
//
// lblFps
//
this.lblFps.AutoSize = true;
this.lblFps.Location = new System.Drawing.Point(160, 13);
this.lblFps.Name = "lblFps";
this.lblFps.Size = new System.Drawing.Size(10, 13);
this.lblFps.TabIndex = 4;
this.lblFps.Text = "-";
//
// ckbRun
//
this.ckbRun.Appearance = System.Windows.Forms.Appearance.Button;
this.ckbRun.AutoSize = true;
this.ckbRun.Checked = true;
this.ckbRun.CheckState = System.Windows.Forms.CheckState.Checked;
this.ckbRun.Location = new System.Drawing.Point(8, 8);
this.ckbRun.Name = "ckbRun";
this.ckbRun.Size = new System.Drawing.Size(37, 23);
this.ckbRun.TabIndex = 5;
this.ckbRun.Text = "Run";
this.ckbRun.UseVisualStyleBackColor = true;
//
// btnZoomReset
//
this.btnZoomReset.Location = new System.Drawing.Point(51, 8);
this.btnZoomReset.Name = "btnZoomReset";
this.btnZoomReset.Size = new System.Drawing.Size(74, 23);
this.btnZoomReset.TabIndex = 7;
this.btnZoomReset.Text = "Zoom 100%";
this.btnZoomReset.UseVisualStyleBackColor = true;
this.btnZoomReset.Click += new System.EventHandler(this.btnZoomReset_Click);
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(5, 37);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(57, 13);
this.label2.TabIndex = 8;
this.label2.Text = "SizeMode:";
//
// cbxSizeMode
//
this.cbxSizeMode.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxSizeMode.FormattingEnabled = true;
this.cbxSizeMode.Location = new System.Drawing.Point(73, 34);
this.cbxSizeMode.Name = "cbxSizeMode";
this.cbxSizeMode.Size = new System.Drawing.Size(84, 21);
this.cbxSizeMode.TabIndex = 9;
this.cbxSizeMode.SelectedIndexChanged += new System.EventHandler(this.SizeMode_SelectedIndexChanged);
//
// gpbDoubleBuffer
//
this.gpbDoubleBuffer.Controls.Add(this.ckbDoubleBufferPictureBox);
this.gpbDoubleBuffer.Controls.Add(this.ckbDoubleBufferForm);
this.gpbDoubleBuffer.Location = new System.Drawing.Point(221, 8);
this.gpbDoubleBuffer.Name = "gpbDoubleBuffer";
this.gpbDoubleBuffer.Size = new System.Drawing.Size(99, 65);
this.gpbDoubleBuffer.TabIndex = 10;
this.gpbDoubleBuffer.TabStop = false;
this.gpbDoubleBuffer.Text = "Double Buffer";
//
// ckbDoubleBufferPictureBox
//
this.ckbDoubleBufferPictureBox.AutoSize = true;
this.ckbDoubleBufferPictureBox.Checked = true;
this.ckbDoubleBufferPictureBox.CheckState = System.Windows.Forms.CheckState.Checked;
this.ckbDoubleBufferPictureBox.Location = new System.Drawing.Point(9, 38);
this.ckbDoubleBufferPictureBox.Name = "ckbDoubleBufferPictureBox";
this.ckbDoubleBufferPictureBox.Size = new System.Drawing.Size(77, 17);
this.ckbDoubleBufferPictureBox.TabIndex = 8;
this.ckbDoubleBufferPictureBox.Text = "PictureBox";
this.ckbDoubleBufferPictureBox.UseVisualStyleBackColor = true;
this.ckbDoubleBufferPictureBox.CheckedChanged += new System.EventHandler(this.DoubleBufferPictureBox_CheckedChanged);
//
// ckbDoubleBufferForm
//
this.ckbDoubleBufferForm.AutoSize = true;
this.ckbDoubleBufferForm.Checked = true;
this.ckbDoubleBufferForm.CheckState = System.Windows.Forms.CheckState.Checked;
this.ckbDoubleBufferForm.Location = new System.Drawing.Point(9, 15);
this.ckbDoubleBufferForm.Name = "ckbDoubleBufferForm";
this.ckbDoubleBufferForm.Size = new System.Drawing.Size(49, 17);
this.ckbDoubleBufferForm.TabIndex = 7;
this.ckbDoubleBufferForm.Text = "Form";
this.ckbDoubleBufferForm.UseVisualStyleBackColor = true;
this.ckbDoubleBufferForm.CheckedChanged += new System.EventHandler(this.DoubleBufferForm_CheckedChanged);
//
// cbxBitmapDrawSource
//
this.cbxBitmapDrawSource.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cbxBitmapDrawSource.FormattingEnabled = true;
this.cbxBitmapDrawSource.Items.AddRange(new object[] {
"PictureBox",
"Graphics"});
this.cbxBitmapDrawSource.Location = new System.Drawing.Point(73, 57);
this.cbxBitmapDrawSource.Name = "cbxBitmapDrawSource";
this.cbxBitmapDrawSource.Size = new System.Drawing.Size(84, 21);
this.cbxBitmapDrawSource.TabIndex = 12;
this.cbxBitmapDrawSource.SelectedIndexChanged += new System.EventHandler(this.BitmapDrawSource_SelectedIndexChanged);
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(5, 60);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(68, 13);
this.label3.TabIndex = 11;
this.label3.Text = "Bitmap draw:";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(361, 304);
this.Controls.Add(this.cbxBitmapDrawSource);
this.Controls.Add(this.label3);
this.Controls.Add(this.gpbDoubleBuffer);
this.Controls.Add(this.cbxSizeMode);
this.Controls.Add(this.label2);
this.Controls.Add(this.btnZoomReset);
this.Controls.Add(this.ckbRun);
this.Controls.Add(this.lblFps);
this.Controls.Add(this.label1);
this.Controls.Add(this.pictureBox1);
this.DoubleBuffered = true;
this.Name = "Form1";
this.Text = "Max FPS tester";
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.gpbDoubleBuffer.ResumeLayout(false);
this.gpbDoubleBuffer.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
PictureBoxDoubleBuffer pictureBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label lblFps;
private System.Windows.Forms.CheckBox ckbRun;
private System.Windows.Forms.Button btnZoomReset;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.ComboBox cbxSizeMode;
private System.Windows.Forms.GroupBox gpbDoubleBuffer;
private System.Windows.Forms.CheckBox ckbDoubleBufferPictureBox;
private System.Windows.Forms.CheckBox ckbDoubleBufferForm;
private System.Windows.Forms.ComboBox cbxBitmapDrawSource;
private System.Windows.Forms.Label label3;
}
}
PictureBoxDoubleBuffer.cs:
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public class PictureBoxDoubleBuffer : PictureBox
{
public bool DoubleBuffered
{
get { return base.DoubleBuffered; }
set { base.DoubleBuffered = value; }
}
public PictureBoxDoubleBuffer()
{
base.DoubleBuffered = true;
}
}
}
"The big question is: How can I improve the FPS, specially in zoom mode?"
In Zoom (and Stretch) mode the Image() is probably being resized for each Refresh()...a very expensive operation. You could roll your own "zoom" mode that creates a new image that is already zoomed and assign that instead, leaving the mode at "Normal".
Here's a quick example using a cheat zoom with another PictureBox (obviously you could do the zoom mathematically by computing aspect ratio, etc...but that hurts my brain in the morning):
private void cbxSizeMode_SelectedIndexChanged(object sender, EventArgs e)
{
PictureBoxSizeMode mode;
Enum.TryParse<PictureBoxSizeMode>(cbxSizeMode.SelectedValue.ToString(), out mode);
this.pictureBox1.SizeMode = mode;
if (this.pictureBox1.SizeMode == PictureBoxSizeMode.Zoom)
{
using (PictureBox pb = new PictureBox())
{
pb.Size = pictureBox1.Size;
pb.SizeMode = PictureBoxSizeMode.Zoom;
pb.Image = _backImage;
Bitmap bmp = new Bitmap(pb.Size.Width, pb.Size.Height);
pb.DrawToBitmap(bmp, pb.ClientRectangle);
this.pictureBox1.SizeMode = PictureBoxSizeMode.Normal;
this.pictureBox1.Image = bmp;
}
}
else
{
pictureBox1.Image = _backImage;
}
}
You'll need to call this method when the form first runs if Zoom is the default selection, otherwise it won't have custom zoom image.
Now your Zoom mode should zoom along! (sorry, couldn't resist doing that)
I created a user control for Windows forms application. All it has is a TableLayoutPanel with four cells. Cell[0,0] and cell[0,1] have labels. Cell[1,0] has a treeview and cell[1,1] has CheckedListBox.
For all four controls, I have set docking to Fill. That freaking CheckedListBox appears smaller than TreeView. Is there any way to get proper docking for the controls?
BTW, I am using .Net 3.5 and VS 2010.
Following is the designer file code for the control:
namespace UserControls
{
partial class LinkedContent
{
/// <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 Component 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.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.lblContentGroups = new System.Windows.Forms.Label();
this.lblModules = new System.Windows.Forms.Label();
this.tvContent = new System.Windows.Forms.TreeView();
this.chkListBoxModules = new System.Windows.Forms.CheckedListBox();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.CellBorderStyle = System.Windows.Forms.TableLayoutPanelCellBorderStyle.Single;
this.tableLayoutPanel1.ColumnCount = 2;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.lblContentGroups, 0, 0);
this.tableLayoutPanel1.Controls.Add(this.lblModules, 1, 0);
this.tableLayoutPanel1.Controls.Add(this.tvContent, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.chkListBoxModules, 1, 1);
this.tableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 7.760532F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 92.23947F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(501, 451);
this.tableLayoutPanel1.TabIndex = 0;
//
// lblContentGroups
//
this.lblContentGroups.AutoSize = true;
this.lblContentGroups.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblContentGroups.Location = new System.Drawing.Point(4, 1);
this.lblContentGroups.Name = "lblContentGroups";
this.lblContentGroups.Size = new System.Drawing.Size(243, 34);
this.lblContentGroups.TabIndex = 0;
this.lblContentGroups.Text = "Content Groups";
this.lblContentGroups.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// lblModules
//
this.lblModules.AutoSize = true;
this.lblModules.Dock = System.Windows.Forms.DockStyle.Fill;
this.lblModules.Location = new System.Drawing.Point(254, 1);
this.lblModules.Name = "lblModules";
this.lblModules.Size = new System.Drawing.Size(243, 34);
this.lblModules.TabIndex = 1;
this.lblModules.Text = "Modules";
this.lblModules.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// tvContent
//
this.tvContent.CheckBoxes = true;
this.tvContent.Dock = System.Windows.Forms.DockStyle.Fill;
this.tvContent.Location = new System.Drawing.Point(4, 39);
this.tvContent.Name = "tvContent";
this.tvContent.Size = new System.Drawing.Size(243, 408);
this.tvContent.TabIndex = 2;
//
// chkListBoxModules
//
this.chkListBoxModules.Dock = System.Windows.Forms.DockStyle.Fill;
this.chkListBoxModules.FormattingEnabled = true;
this.chkListBoxModules.Location = new System.Drawing.Point(254, 39);
this.chkListBoxModules.Name = "chkListBoxModules";
this.chkListBoxModules.Size = new System.Drawing.Size(243, 408);
this.chkListBoxModules.TabIndex = 3;
//
// LinkedContent
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.tableLayoutPanel1);
this.Name = "LinkedContent";
this.Size = new System.Drawing.Size(501, 451);
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.TableLayoutPanel tableLayoutPanel1;
private System.Windows.Forms.Label lblContentGroups;
private System.Windows.Forms.Label lblModules;
private System.Windows.Forms.TreeView tvContent;
private System.Windows.Forms.CheckedListBox chkListBoxModules;
}
}
Check to make sure the IntegralHeight Property is not set. It defaults to true.
From above MSDN Link:
When this property is set to true, the control automatically resizes
to ensure that an item is not partially displayed. If you want to
maintain the original size of the ListBox based on the space
requirements of your form, set this property to false.