I cannot figure out how to get the MouseDown event when the mouse is down/clicked on the border of a Form. It is easy to see that it is raised when the mouse is down in the (I think its called) client area of the form, but it is never raised when it is down on the border.
Here is a SSCCE that demonstrates the issue. The label in the center of the form is only changed when the mouse is down on the client area and not the border.
Is there anyway to catch this event or have it be raised?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MouseEventTest
{
public partial class Form1 : Form
{
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
label1.Text = rand.Next().ToString();
}
/// <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.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(42, 30);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(91, 13);
this.label1.TabIndex = 0;
this.label1.Text = "99999999999999";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(185, 75);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseDown);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
}
}
EDIT: I have solved this problem, but it created another. If you are visting this question in the future here is a related question I asked. It (hopefully) will help me/us figure out when the mouse has been released too. WM_NCLBUTTONUP message not sent at the end of dragging a form, how to do so?
Override the WndProc method:
const int WM_NCLBUTTONDWN = 0xA1;
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_NCLBUTTONDWN)
{
this.Text = "got it!";
}
base.WndProc(ref m);
}
Use this method and calculate for the border of your form. There's no way to handle it via "Form" because the border is not a part of the Form...
EDIT1:
As commented by Lars, there's no way to do this using WinForms. You will need to "go down" a floor and work with the WinForms infrastructure...
Related
I'm working on two C# winforms and WPF applications that utilize the iTunes COM and have noticed some peculiar behavior accessing the PlayerPositionMS property, which is the player's position in milliseconds. The precision of the PlayerPositionMS value is dependent on whether or not the iTunes application has focus or not. The goal for my 2 apps would be to update the player position with the most accuracy. Any assistance would be appreciated.
Below is some sample code to illustrate my point. This is a winforms application with a single label and timer. The timer interval is the default 100 milliseconds, and the tick method updates the label text with the PlayerPositionMS property value. When iTunes has focus the label is updated as expected. But when the winforms application, or anything else, has focus the label is updated roughly every second. The behavior is the same on winforms and WPF.
iTunes version: 12.9.0.167
iTunesLib version: 1.13
Form snip:
Form code:
namespace Test
{
partial class Form_Test
{
/// <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.label_Position = new System.Windows.Forms.Label();
this.timer_Test = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// label_Position
//
this.label_Position.AutoSize = true;
this.label_Position.Location = new System.Drawing.Point(12, 9);
this.label_Position.Name = "label_Position";
this.label_Position.Size = new System.Drawing.Size(16, 13);
this.label_Position.TabIndex = 0;
this.label_Position.Text = "---";
//
// timer_Test
//
this.timer_Test.Enabled = true;
this.timer_Test.Tick += new System.EventHandler(this.UpdateLabel);
//
// Form_Test
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(205, 33);
this.Controls.Add(this.label_Position);
this.Name = "Form_Test";
this.Text = "Test Form";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label_Position;
private System.Windows.Forms.Timer timer_Test;
}
}
Code behind:
using iTunesLib;
using System;
using System.Windows.Forms;
namespace Test
{
public partial class Form_Test : Form
{
private static iTunesApp _itunes = new iTunesLib.iTunesApp();
public Form_Test()
{
InitializeComponent();
}
private void UpdateLabel(object sender, EventArgs e)
{
if (_itunes.CurrentTrack != null && _itunes.PlayerState == ITPlayerState.ITPlayerStatePlaying)
label_Position.Text = _itunes.PlayerPositionMS.ToString();
}
}
}
Calling the Resume method before accessing the PlayerPositionMS property seems to work to resolve this:
private void UpdateLabel(object sender, EventArgs e)
{
if (_itunes.CurrentTrack != null && _itunes.PlayerState == ITPlayerState.ITPlayerStatePlaying)
{
_itunes.Resume();
label_Position.Text = _itunes.PlayerPositionMS.ToString();
}
}
I have an issue with C# richtextbox and Microsoft equation editor. My goal is to read, modify and save changes in rtf file.
I followed tutorial below:
C# Tutorial : How to insert Math Equation to RichTextBox | FoxLearn
I can read and modify equation in editor, however I can't update richtextbox after modification, modified value is not changed in richtboxtext.
Initial state
Modification
Update(F3) and exit in equation editor doesn't cause change in richtextbox.
However when I double click equation in editor there is changed value.
Here is my code:
using System;
using System.Windows.Forms;
namespace Panel.Views
{
public partial class Axis : Form
{
public Axis()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedRtf = Panel.Properties.Resources.Plot1_x_axis;
}
}
}
namespace Panel.Views
{
partial class Axis
{
/// <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.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(723, 431);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(28, 36);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(770, 372);
this.richTextBox1.TabIndex = 1;
this.richTextBox1.Text = "";
//
// Axis
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(822, 478);
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.button1);
this.Name = "Axis";
this.Text = "Axis";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.RichTextBox richTextBox1;
}
}
I would be appreciated for any help, hints or suggestions how to solve this issue.
Thank you in advance.
I'm using Visual Studio 2010 to create an application. I have a class derived from the System.Windows.Forms.Form control. This class, called CViewport, is a standard for all forms in my application( ie. I intend to derive other forms from this class).
After deriving a class from CViewport, I go to the designer to edit the 2nd generation form and in the designer the form is not displayed correctly. It appears transparent if I'm not mistaken...the previous window shines through the client area of the form as if the backbuffer has not been drawn to by the designer...
...Also, after changing the size of the 2nd gen form, the designer resets the size to arbitrary dimensions...
Why might this be happening? Here is Form derived class:
namespace MyApp
{
public partial class CViewport : Form
{
public CViewport()
{
InitializeComponent();
}
}
}
here is CViewport.Designer.cs:
namespace MyApp
{
partial class CViewport
{
/// <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(GDEViewport));
this.SuspendLayout();
//
// CViewport
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 600);
this.ControlBox = false;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "GDEViewport";
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Hide;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
this.Activated += new System.EventHandler(this.ViewportForm_Activated);
this.Deactivate += new System.EventHandler(this.ViewportForm_Deactivate);
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.OnFormClosing);
this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
this.Resize += new System.EventHandler(this.MainViewport_UserResized);
this.ResumeLayout(false);
}
#endregion
}
}
here is 2nd gen form derived from CViewport
namespace MyApp
{
public partial class CMainViewport: CViewport
{
public CMainViewport()
{
InitializeComponent();
}
}
}
here is CMainViewport.Designer.cs:
namespace MyApp
{
partial class CMainViewport
{
/// <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.SuspendLayout();
//
// CMainViewport
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1218, 573);
this.ControlBox = true;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.Sizable;
this.MaximizeBox = true;
this.MinimizeBox = true;
this.MinimumSize = new System.Drawing.Size(800, 600);
this.Name = "TestForm";
this.RightToLeft = System.Windows.Forms.RightToLeft.No;
this.ShowIcon = true;
this.ShowInTaskbar = true;
this.SizeGripStyle = System.Windows.Forms.SizeGripStyle.Auto;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "TestForm";
this.PauseRendering += new System.EventHandler<System.EventArgs>(this.ViewportForm_PauseRendering);
this.ResumeRendering += new System.EventHandler<System.EventArgs>(this.ViewportForm_ResumeRendering);
this.SystemResume += new System.EventHandler<System.EventArgs>(this.MainViewport_SystemResume);
this.SystemSuspend += new System.EventHandler<System.EventArgs>(this.MainViewport_SystemSuspend);
this.UserResized += new System.EventHandler<System.EventArgs>(this.MainViewport_UserResized);
this.Activated += new System.EventHandler(this.ViewportForm_Activated);
this.Deactivate += new System.EventHandler(this.ViewportForm_Deactivate);
this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
this.Resize += new System.EventHandler(this.ViewportForm_Resize);
this.ResumeLayout(false);
}
#endregion
}
}
this.ResizeBegin += new System.EventHandler(this.ViewportForm_ResizeBegin);
this.ResizeEnd += new System.EventHandler(this.ViewportForm_ResizeEnd);
this.Paint += new System.Windows.Forms.PaintEventHandler(this.ViewportForm_Paint);
this.Resize += new System.EventHandler(this.MainViewport_UserResized);
The designer runs the code in the base class to provide the WYSIWYG view. That's normally the Form class, it doesn't have any bugs. Now it is your CViewPort class. It has bugs. In one those event handlers that we cannot see.
Like the Paint event handler, strong lead to "is not displayed correctly". You have a handler for Resize, strong lead to "resets the size to arbitrary dimensions". Having these events also run at design time is what is getting you into trouble. And you are doing it wrong, a base class for a Form should override OnPaint() and OnResize() so the order in which code runs is predictable and controllable.
Having code run at design-time requires deeper insight in the way Winforms works, getting guidance from a book is rather important. You are not there yet, you didn't realize that posting the code for these event handlers was even relevant. There's a keep-out-of-trouble approach that gives you time to read the book, add this line of code to every event handler you added to your CViewPort class:
if (this.DesignMode) return;
The answer posted by Hans Passant was very helpful...the problem though, was that I was setting the DockStyle property of the base class to 'Fill' in the base form's constructor. Removing this line of code fixed the problem...
I am looking over someone Else's code and noticed that some of the user control items have Class.Designer.cs and Class.resx files while others only have the .resx file. Is there a way I can add a .cs file to the controls with only a .resx file?
Why/how would this occur?
Is there any difference between the two? It looks like designer code is in the Designer.cs file while the UC with only has .rexs has all the code in a single .cs file.
Thanks!
Some people automatically delete the Class.Designer.cs file when creating User Controls and Forms. If they do this, then the code to manage the placement of controls on the Form and such are not longer separated into a partial class, but are instead included into the code behind of the Class.cs file.
For example here is Form 1.designer.cs.
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()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(49, 87);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
}
}
If I create another form, but delete the designer file, you will see this sort of code will go into the Form1.cs code file instead:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
private Panel panel1;
public Form1()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
this.panel1.Location = new System.Drawing.Point(101, 62);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(200, 100);
this.panel1.TabIndex = 0;
//
// Form1
//
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.ResumeLayout(false);
}
}
}
This doesn't cause any harm, however having the code that deals with the form controls in a different file keeps your code looking leaner and cleaner and is therefore easier to navigate and manage.
this is the very first time I try to anything in this language or applications anyway. I just installed c# and I can't get this code to do a simple value change.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Value = 22;
MessageBox.Show("completed!");
}
}
}
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()
{
this.progressBar1 = new System.Windows.Forms.ProgressBar();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// progressBar1
//
this.progressBar1.Location = new System.Drawing.Point(42, 309);
this.progressBar1.Name = "progressBar1";
this.progressBar1.Size = new System.Drawing.Size(428, 54);
this.progressBar1.TabIndex = 0;
this.progressBar1.Value = 55;
//
// button1
//
this.button1.Location = new System.Drawing.Point(42, 48);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(428, 45);
this.button1.TabIndex = 1;
this.button1.Text = "start";
this.button1.UseVisualStyleBackColor = true;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.WhiteSmoke;
this.ClientSize = new System.Drawing.Size(525, 409);
this.Controls.Add(this.button1);
this.Controls.Add(this.progressBar1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ProgressBar progressBar1;
private System.Windows.Forms.Button button1;
}
}
All I did is add
progressBar1.Value = 22;
MessageBox.Show("completed!");
but when I click on button1 nothing happens.
By the way all this code was created by the software, is this how people develop? I thought it was all code with no "help", this looks like me 10y ago when I first started with HTML and frontpage did all the code for me.
:)
What is missing, in the InitializeComponent() method, is something like this:
this.button1.Click += new System.EventHandler(this.button1_Click);
Why this didn't get created for you is hard to tell. Normally you open the form desginer and double click on the button and that code is created and you are taken to a button1_Click method stub.
Try deleting your current button1_Click method code, save, open the designer and double click on the button to see if that works.
You can add similar code yourself by doing:
button1.Click += button1_Click;
But be careful that it is not also in the InitializeComponent() method, which would cause it to be called twice.
Add
button1.Click += button1_Click;
somewhere (constructor of Form1).
You need to add the button1_Click handler to the Click event.
Unlike VB6, that's not done implicitly.
You can do that in the designer by selecting the button and going to the events tab.
To answer your question, the VS WinForms designer generates C# source code that creates the layout you designed.
If you don't like that paradigm, you may want to use WPF.
It is certainly possible to build WinForms UIs entirely by hand, but it will be extremely painful.