I'm randomperson heres my code and it is a calculator:
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.num1 = new System.Windows.Forms.NumericUpDown();
((System.ComponentModel.ISupportInitialize)(this.num1)).BeginInit();
this.SuspendLayout();
//
// num1
//
this.num1.AccessibleName = "";
this.num1.Location = new System.Drawing.Point(12, 12);
this.num1.Name = "num1";
this.num1.Size = new System.Drawing.Size(120, 20);
this.num1.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.num1);
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.num1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.NumericUpDown num1;
}
}
There is only a NumericUpDown control in your form so i'm assuming you want to fetch that value of that control. You'll have to use num1.Value to get it's value.
Try
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
num1.ValueChanged += new EventHandler(numericUpDown1_ValueChanged);
}
void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
MessageBox.Show("New value : " +num1.Value);
}
Related
Can someone give me a sample code to confine the cursor on to a Form. I found this (ClipCursor API, that says it can be done using it). I have a C# Windows form application and using VS 2008.
My code:
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 Form1_CursorChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
MoveCursor ();
}
private void Form1_Resize(object sender, EventArgs e)
{
MoveCursor();
}
private void Form1_LocationChanged(object sender, EventArgs e)
{
MoveCursor();
}
private void MoveCursor()
{
Cursor.Clip = Bounds;
this.Capture = true;
}
}
}
Form1.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.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.AutoCompleteCustomSource.AddRange(new string[] {
"all",
"allah",
"allo"});
this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
this.textBox1.Location = new System.Drawing.Point(30, 70);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(227, 20);
this.textBox1.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, 264);
this.Controls.Add(this.textBox1);
this.Cursor = System.Windows.Forms.Cursors.No;
this.Name = "Form1";
this.Text = "Form1";
this.CursorChanged += new System.EventHandler(this.Form1_CursorChanged);
this.Load += new System.EventHandler(this.Form1_Load);
this.Resize += new System.EventHandler(this.Form1_Resize);
this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.TextBox textBox1;
}
}
There's an option that doesn't require you to p/invoke: Cursor.Clip
Edit: new code. Complete form code in single file.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
private void Form1_Activate(object sender, EventArgs e) {
MoveCursor ();
}
private void Form1_Resize(object sender, EventArgs e) {
MoveCursor();
}
private void Form1_LocationChanged(object sender, EventArgs e) {
MoveCursor();
}
private void MoveCursor()
{
this.Capture = true;
System.Windows.Forms.Cursor.Clip = Bounds;
}
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
this.textBox1.AutoCompleteCustomSource.AddRange(new string[] {"all", "allak", "allo"});
this.textBox1.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.textBox1.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.CustomSource;
this.textBox1.Location = new System.Drawing.Point(30, 70);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(227, 20);
this.textBox1.TabIndex = 0;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.Controls.Add(this.textBox1);
this.Cursor = System.Windows.Forms.Cursors.No;
this.Name = "Form1";
this.Text = "Form1";
this.Activated += new System.EventHandler(this.Form1_Activate);
this.Resize += new System.EventHandler(this.Form1_Resize);
this.LocationChanged += new System.EventHandler(this.Form1_LocationChanged);
this.ResumeLayout(false);
this.PerformLayout();
}
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
components.Dispose();
base.Dispose(disposing);
}
private System.ComponentModel.IContainer components = null;
private System.Windows.Forms.TextBox textBox1 = null;
}
}
Unless this is a particularly locked-down computer like a kiosk, users will probably hate it. Think about if they are needing to alt+tab to a different app to copy something to the clipboard to fill out your form... Just be careful.
I've an application that use two forms: Form1 (main) and Form2 (secondary). I show the Form2 with the following code:
Form2 frm = new Form2();
frm.TopMost = true;
frm.Show();
When the Form2 is visible, it hasn't the focus. How can I do the focus at the Form2 and keep the focus at the Form1? Sorry for my bad english!
try bellow code within MDI Parent Form (Main Form)
private Form2 _form2;
#region UtilOpenForm
/// <summary>
/// UtilOpenForm
/// </summary>
/// <param name="appContainer"></param>
/// <param name="childForm"></param>
private void UtilOpenForm(Form appContainer, Form childForm)
{
this.Cursor = Cursors.WaitCursor;
if (childForm == null)
{
throw new ArgumentNullException("childForm");
}
childForm.MdiParent = appContainer;
childForm.StartPosition = FormStartPosition.CenterScreen;
childForm.MaximizeBox = false;
childForm.MinimizeBox = false;
childForm.Closed += new EventHandler(childForm_Closed);
childForm.Show();
this.Cursor = Cursors.Default;
}
Now from Button / Menu click within MDI Parent
if (_form2 == null)
{
UtilOpenForm(this, _form2 = new Form2());
}
Now child form close Function within MDI Parent
#region childForm_Closed
/// <summary>
///
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void childForm_Closed(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Form2))
{
_form2.Dispose();
if (_form2 != null)
{
_form2 = null;
}
}
Is it possible to drag and drop a path in a wpf using Mouse Eventhandlers? In partcular I want to drag a path with the left mouse button and to mouse it on the grid. How can this be done?
Try this:
Given:
TextBox name is "TextBox1"
public MainWindow()
{
// Initialize UI
InitializeComponent();
// Loaded event
this.Loaded += delegate
{
TextBox1.AllowDrop = true;
TextBox1.PreviewDragEnter += TextBox1PreviewDragEnter;
TextBox1.PreviewDragOver += TextBox1PreviewDragOver;
TextBox1.Drop += TextBox1DragDrop;
};
}
/// <summary>
/// We have to override this to allow drop functionality.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void TextBox1PreviewDragOver(object sender, DragEventArgs e)
{
e.Handled = true;
}
/// <summary>
/// Evaluates the Data and performs the DragDropEffect
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBox1PreviewDragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
e.Effects = DragDropEffects.Copy;
}
else
{
e.Effects = DragDropEffects.None;
}
}
/// <summary>
/// The drop activity on the textbox.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void TextBox1DragDrop(object sender, DragEventArgs e)
{
// Get data object
var dataObject = e.Data as DataObject;
// Check for file list
if (dataObject.ContainsFileDropList())
{
// Clear values
TextBox1.Text = string.Empty;
// Process file names
StringCollection fileNames = dataObject.GetFileDropList();
StringBuilder bd = new StringBuilder();
foreach (var fileName in fileNames)
{
bd.Append(fileName + "\n");
}
// Set text
TextBox1.Text = bd.ToString();
}
}
I have a checkbox shown as button. I want to make it flash when it is checked. From what Ive found, i think the simplest way is to use a timer to rotate the background color of the button.
Where I am stuck is finding the back color of the checked button. Can someone tell me what the back color is changed to by default (via designer) when the button is checked? Without that I cannot get the timer to begin the oscillation.
What I have is a Mute Button. When the mute is active i want the button to flash until it is pressed again to turn the mute off.
In case I'm wrong and the back color actually does not change, what changes about the button to make it appear pressed?
code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
instructorTimer.Enabled = true;
}
private void instructorTimer_Tick(object sender, EventArgs e)
{
// interval is 2000
if (checkBox1.BackColor == System.Drawing.SystemColors.Control)
checkBox1.BackColor = System.Drawing.SystemColors.ControlDark;
else
checkBox1.BackColor = System.Drawing.SystemColors.Control;
}
Maybe SystemColors.Control is what you are looking for.
Make sure you have the tick event hooked up. It looks suspect:
private void Form1_Load(object sender, EventArgs e) {
timer1.Tick += instructorTimer_Tick;
}
I would also change the color immediately, for instant feedback:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
checkBox1.BackColor = SystemColors.ControlDark;
timer1.Enabled = true;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
checkBox1.BackColor = Color.Green;
Application.DoEvents();
TimeSpan ts = new TimeSpan();
do
{
}
while (ts.Milliseconds == 2000);
checkBox1.BackColor = SystemColors.Control;
}
If you're willing to use a UserControl instead of trying to repurpose Button - the following should work great and you can extend it if something doesn't work like you like:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FlashyButton
{
public partial class FlashyButton : UserControl
{
private CheckState _Checked = CheckState.Unchecked;
[Browsable(true)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
lblText.Text = value;
Invalidate();
}
}
public FlashyButton()
{
this.CausesValidation = true;
InitializeComponent();
lblText.MouseClick += (sender, e) => { OnMouseClick(null); };
}
public void SetFont(Font WhichFont)
{
this.Font = WhichFont;
}
public CheckState GetCheckedState()
{
return this._Checked;
}
public void SetCheckedState(CheckState NewCheckState)
{
this._Checked = NewCheckState;
}
protected override void OnMouseClick(MouseEventArgs e)
{
this._Checked = (this._Checked == CheckState.Checked) ? CheckState.Unchecked : CheckState.Checked;
this.BorderStyle = (this._Checked == CheckState.Checked) ? System.Windows.Forms.BorderStyle.Fixed3D : System.Windows.Forms.BorderStyle.FixedSingle;
tmrRedraw.Enabled = (this._Checked == CheckState.Checked);
if (this._Checked == CheckState.Unchecked)
{
this.BackColor = SystemColors.Control;
}
this.Invalidate(); //Force redraw
base.OnMouseClick(e);
}
private float Percent = 100;
private void tmrRedraw_Tick(object sender, EventArgs e)
{
Percent -= 2;
if (Percent < -100) Percent = 100;
this.BackColor = Color.FromArgb(
255,
Lerp(255, SystemColors.Control.R, (int)Math.Abs(Percent)),
Lerp(0, SystemColors.Control.G, (int)Math.Abs(Percent)),
Lerp(0, SystemColors.Control.B, (int)Math.Abs(Percent))
);
}
private int Lerp(int Start, int End, int Percent)
{
return ((int) ((float)(End - Start) * ((float)Percent / 100f)) + Start);
}
}
}
And here is the .Designer code as well (just replace what you already have when you make a new control by this name)
namespace FlashyButton
{
partial class FlashyButton
{
/// <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.components = new System.ComponentModel.Container();
this.lblText = new System.Windows.Forms.Label();
this.tmrRedraw = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// lblText
//
this.lblText.AutoSize = true;
this.lblText.Location = new System.Drawing.Point(4, 4);
this.lblText.Name = "lblText";
this.lblText.Size = new System.Drawing.Size(55, 17);
this.lblText.TabIndex = 0;
this.lblText.Text = "Sample";
//
// tmrRedraw
//
this.tmrRedraw.Interval = 10;
this.tmrRedraw.Tick += new System.EventHandler(this.tmrRedraw_Tick);
//
// FlashyButton
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.AutoSize = true;
this.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.Controls.Add(this.lblText);
this.Name = "FlashyButton";
this.Size = new System.Drawing.Size(148, 148);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label lblText;
private System.Windows.Forms.Timer tmrRedraw;
}
}
This worked for me when I had a CheckBox with Appearance = Button and FlatStyle = Flat and wanted it flashing when checked:
private void timer_Flashing_Tick(object sender, EventArgs e)
{
if (checkBox_Refresh.Checked)
{
if (checkBox_Refresh.FlatAppearance.CheckedBackColor == Color.Red)
{
checkBox_Refresh.FlatAppearance.CheckedBackColor = Color.Transparent;
}
else
{
checkBox_Refresh.FlatAppearance.CheckedBackColor = Color.Red;
}
}
}
So I'm trying to create a tooltip at some point on the screen.
ToolTip tip = new ToolTip();
tip.Show("foobar", **IWin32Window window**, new Point(100, 100))
The problem is I don't know what to insert as the window parameter in the above. My app runs entirely out of the system tray, and has no other GUI elements. It's called notifyIcon1. That is created through Form1. Neither of these values work when plugged in to tip.Show().
How can I generate a tooltip anywhere on my screen using only the system tray?
Thanks.
The IWin32Window interface is a simple interface that only provides a IntPtr property named Handle. Feasibly something like this should work:
using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace SO_ToolTip
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
public static extern IntPtr GetDesktopWindow();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
WindowWrapper windowWrapper = new WindowWrapper(GetDesktopWindow());
ToolTip toolTip = new ToolTip();
toolTip.Show("Blah blah... Blah blah... Blah blah...", windowWrapper, 1, 1, 10000);
}
}
public class WindowWrapper : IWin32Window
{
public WindowWrapper(IntPtr handle)
{
Handle = handle;
}
public IntPtr Handle { get; protected set; }
}
}
But it doesn't. It complains about a NullReferenceException and I haven't debugged further. This does work:
...
private void button1_Click(object sender, EventArgs e)
{
ToolTip toolTip = new ToolTip();
toolTip.Show("Blah blah... Blah blah... Blah blah...", this, 1, 1, 10000);
}
...
Although the position is relative to the current form. Maybe that will get you going in the right direction.
Edit: Even this doesn't work so I'm not sure if it's an issue with WindowWrapper (how?) or what:
...
private void button1_Click(object sender, EventArgs e)
{
WindowWrapper windowWrapper = new WindowWrapper(this.Handle);
ToolTip toolTip = new ToolTip();
toolTip.Show("Blah blah... Blah blah... Blah blah...", windowWrapper, 1, 1, 10000);
}
...
Here you go, use a transparent, maximized form that you BringToFront() before showing the ToolTip
Form1 Code:
using System;
using System.Windows.Forms;
namespace SO_ToolTip
{
public partial class Form1 : Form
{
Random _Random = new Random();
ToolTip _ToolTip = new ToolTip();
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
BringToFront();
_ToolTip.Show("Blah blah... Blah blah... Blah blah...", this, _Random.Next(0, Width), _Random.Next(0, Height), 10000);
}
}
}
Form1 Designer Code: So you can see the forms properties:
namespace SO_ToolTip
{
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.timer1 = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1000;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 264);
this.ControlBox = false;
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Form1";
this.Opacity = 0;
this.ShowIcon = false;
this.ShowInTaskbar = false;
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
}
}
Joining late to the party:
In case you prefer/need to have WPF window:
private class ToolTipWPFWindow : Window
{
private readonly TextBlock m_txtToDisplay = new TextBlock();
private readonly DispatcherTimer m_timer = new DispatcherTimer();
public ToolTipWindow(string p_strStringToDisplay, int p_intXOnScreen = 0, int p_intYOnScreen = 0, double p_dblDurationInMilliSeconds = 1500)
{
if (p_intXOnScreen == 0 && p_intYOnScreen == 0)
{
p_intXOnScreen = System.Windows.Forms.Cursor.Position.X;
p_intYOnScreen = System.Windows.Forms.Cursor.Position.Y;
}
m_txtToDisplay.Text = p_strStringToDisplay;
m_txtToDisplay.Margin = new Thickness(3);
Background = new SolidColorBrush(Colors.LightGoldenrodYellow);
ShowInTaskbar = false;
ResizeMode = System.Windows.ResizeMode.NoResize;
Topmost = true;
// Location on screen - As Set
WindowStartupLocation = WindowStartupLocation.Manual;
Left = p_intXOnScreen;
Top = p_intYOnScreen;
WindowStyle = WindowStyle.None;
SizeToContent = SizeToContent.WidthAndHeight;
Content = m_txtToDisplay;
m_timer.Interval = TimeSpan.FromMilliseconds(p_dblDurationInMilliSeconds);
m_timer.Tick += timer_Tick;
m_timer.Start();
}
private void timer_Tick(object sender, EventArgs e)
{
if (m_timer != null)
{
m_timer.Stop();
m_timer.Tick -= timer_Tick;
}
Close();
}
Usage:
// Display the ToolTip Window to the right of the Cursor
int intX = Cursor.Position.X + 20;
int intY = Cursor.Position.Y;
ToolTipWindow wpfWindow = new ToolTipWindow("Text To Display", intX, intY, 800);
wpfWindow.Show();
Result:
I didn't implement the Mouse leave event, since I've used short display duration.