Hey everyone, This should be real easy, but I've been staring at it over an hour now and can't figure out what's happening. I have a textbox on a form. When the cursor is in the textbox so someone can start typing, I want the tooltip displayed. It works when I click on the textbox. However, when I'm tabbing through the fields on the form and hit the textbox so the user can start typing, the tooltip does not display. I have tried the keydown and keypress events to no avail. Below is the code I use on the Enter (focus) event. Any ideas on how I can get this tool tip to show when tabbing instead of pointing/clicking with the mouse? Using VS 2005 on WinXP. Thanks!
private void txtUserName_Enter(object sender, EventArgs e)
{
ToolTip tt = new ToolTip();
if (Control.IsKeyLocked(Keys.CapsLock))
{
tt.IsBalloon = true;
tt.ToolTipIcon = ToolTipIcon.Warning;
tt.UseAnimation = false;
tt.ToolTipTitle = "Caps lock is On";
tt.SetToolTip(this.txtUserName, "Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.");
}
}
| | FireMyst Friday, September 11, 2009 4:35 AM | try to use this Adjust topDiff variable according to your tooltip. instead of textbox1 use your textbox ToolTip tt = new ToolTip(); if (Control.IsKeyLocked(Keys.CapsLock)) { int topDiff = 50; tt.IsBalloon = true; tt.ToolTipIcon = ToolTipIcon.Warning; tt.UseAnimation = false; tt.ToolTipTitle = "Caps lock is On"; //tt.SetToolTip(this.txtUserName, "Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password."); tt.Show("Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.", this,textBox1.Left,textBox1.Top-topDiff, 5000); } - Marked As Answer byFireMyst Friday, September 11, 2009 8:07 AM
-
| | AUmidh Friday, September 11, 2009 6:42 AM | private
void
txtUserName_Enter(object
sender, EventArgs e)
{
ToolTip tt = new ToolTip();
if (Control.IsKeyLocked(Keys.CapsLock))
{
tt.IsBalloon = true;
tt.ToolTipIcon = ToolTipIcon.Warning;
tt.UseAnimation = false;
tt.Show("Caps lock is on", txtUserName,300);
}
}
Try the above code it work, i test it. | | AUmidh Friday, September 11, 2009 4:55 AM | Hi there,
Thanks for your reply!
It does work with the tabbing and clicking. However, on my test form I have two textboxes (one being the txtUserName) which are not multiline, two buttons, and along the bottom a bigger textbox that is multiline.
When I tab or click into the txtUserName textbox, the bubble appears, but now its origin always points to something else on the form other than the txtUserName textbox.
How do I get around this?
Below is the code for my test project so hopefully it'll act the same way for you.
Please note there are TWO files in the code below. This editor is giving me problems and not allowing me to separate the code.
First Form1.Designer.cs:
namespace TestADSecurity
{
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.btnTest = new System.Windows.Forms.Button();
this.txtLogonServer = new System.Windows.Forms.TextBox();
this.lblLogonServer = new System.Windows.Forms.Label();
this.txtUserName = new System.Windows.Forms.TextBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.lblUserName = new System.Windows.Forms.Label();
this.btnReset = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnTest
//
this.btnTest.Location = new System.Drawing.Point(90, 89);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(75, 23);
this.btnTest.TabIndex = 2;
this.btnTest.Text = "Test";
this.btnTest.UseVisualStyleBackColor = true;
this.btnTest.Click += new System.EventHandler(this.button1_Click);
//
// txtLogonServer
//
this.txtLogonServer.Location = new System.Drawing.Point(90, 12);
this.txtLogonServer.Name = "txtLogonServer";
this.txtLogonServer.Size = new System.Drawing.Size(100, 20);
this.txtLogonServer.TabIndex = 1;
//
// lblLogonServer
//
this.lblLogonServer.AutoSize = true;
this.lblLogonServer.Location = new System.Drawing.Point(13, 15);
this.lblLogonServer.Name = "lblLogonServer";
this.lblLogonServer.Size = new System.Drawing.Size(71, 13);
this.lblLogonServer.TabIndex = 3;
this.lblLogonServer.Text = "Logon Server";
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(90, 49);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(100, 20);
this.txtUserName.TabIndex = 4;
this.txtUserName.Enter += new System.EventHandler(this.txtUserName_Enter);
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(90, 131);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.Size = new System.Drawing.Size(406, 53);
this.txtResult.TabIndex = 5;
//
// lblUserName
//
this.lblUserName.AutoSize = true;
this.lblUserName.Location = new System.Drawing.Point(13, 52);
this.lblUserName.Name = "lblUserName";
this.lblUserName.Size = new System.Drawing.Size(60, 13);
this.lblUserName.TabIndex = 6;
this.lblUserName.Text = "User Name";
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(180, 89);
this.btnReset.Name = "btnReset";
this.btnReset.Size = new System.Drawing.Size(75, 23);
this.btnReset.TabIndex = 7;
this.btnReset.Text = "Reset";
this.btnReset.UseVisualStyleBackColor = true;
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// Form1
//
this.AcceptButton = this.btnTest;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(505, 195);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.lblUserName);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.lblLogonServer);
this.Controls.Add(this.txtLogonServer);
this.Controls.Add(this.btnTest);
this.Name = "Form1";
this.Text = "Test AD Security";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnTest;
private System.Windows.Forms.TextBox txtLogonServer;
private System.Windows.Forms.Label lblLogonServer;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Label lblUserName;
private System.Windows.Forms.Button btnReset;
}
}
<strong><br/><br/></strong>
//<br/><strong>//Form1.cs:<br/>//<br/></strong>using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
namespace TestADSecurity
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Reset
this.Reset();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void btnReset_Click(object sender, EventArgs e)
{
this.Reset();
}
private void Reset()
{
}
private void txtUserName_Enter(object sender, EventArgs e)
{
ToolTip tt = new ToolTip();
if (Control.IsKeyLocked(Keys.CapsLock))
{
tt.IsBalloon = true;
tt.ToolTipIcon = ToolTipIcon.Warning;
tt.UseAnimation = false;
tt.ToolTipTitle = "Caps lock is On";
//tt.SetToolTip(this.txtUserName, "Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.");
tt.Show("Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.", this.txtUserName, 5000);
}
}
}
}
| | FireMyst Friday, September 11, 2009 5:45 AM | You mean to say that it works now but it pointing to the bigger multiline text box which is beneath of textUserName? | | AUmidh Friday, September 11, 2009 6:16 AM | You mean to say that it works now but it pointing to the bigger multiline text box which is beneath of textUserName? | | AUmidh Friday, September 11, 2009 6:22 AM | try to use this Adjust topDiff variable according to your tooltip. instead of textbox1 use your textbox ToolTip tt = new ToolTip(); if (Control.IsKeyLocked(Keys.CapsLock)) { int topDiff = 50; tt.IsBalloon = true; tt.ToolTipIcon = ToolTipIcon.Warning; tt.UseAnimation = false; tt.ToolTipTitle = "Caps lock is On"; //tt.SetToolTip(this.txtUserName, "Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password."); tt.Show("Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.", this,textBox1.Left,textBox1.Top-topDiff, 5000); } - Marked As Answer byFireMyst Friday, September 11, 2009 8:07 AM
-
| | AUmidh Friday, September 11, 2009 6:42 AM | In my solution it's still putting the tooltip nowhere near the actual txtUserName textbox. In answer to your previous post -- yes, it's displaying the tooltip balloon in random places in the lower big textbox.
When I do the tt.SetToolTip method, it ALWAYS puts the tip with the correct textbox.
I have my code as follows. Did you use the two code files I supplied above to see if the issue is happening to you as well?
Thanks!
private void txtUserName_Enter(object sender, EventArgs e)
{
ToolTip tt = new ToolTip();
if (Control.IsKeyLocked(Keys.CapsLock))
{
int topDiff = 50;
tt.IsBalloon = true;
tt.ToolTipIcon = ToolTipIcon.Warning;
tt.UseAnimation = false;
tt.ToolTipTitle = "Caps lock is On";
tt.SetToolTip(this.txtUserName, "Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.");
//Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.
tt.Show("Yo!", this, txtUserName.Left, txtUserName.Top - topDiff, 1000);
}
}
| | FireMyst Friday, September 11, 2009 7:04 AM |
In my solution it's still putting the tooltip nowhere near the actual txtUserName textbox .
If it is displayed near to your textbox then adjust this variable according to that. add or minus some value to/from it,
topDiff = 50;
| | AUmidh Friday, September 11, 2009 7:17 AM | And yet another solution: use the EM_SHOWBALLOONTIP to do the same thing:
enum TTI_ICON
{
TTI_NONE = 0,
TTI_INFO = 1,
TTI_WARNING = 2,
TTI_ERROR = 3
}
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
struct EDITBALLOONTIP
{
public int cbStruct;
public string pszTitle;
public string pszText;
public TTI_ICON ttiIcon;
}
const UInt32 EM_SHOWBALLOONTIP = 0x1503;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, EDITBALLOONTIP lParam);
private void ShowBalloon(TextBox txtTarget, string title, string text, TTI_ICON icon)
{
EDITBALLOONTIP ebt = new EDITBALLOONTIP();
ebt.pszTitle = title;
ebt.pszText = text;
ebt.ttiIcon = icon;
ebt.cbStruct = Marshal.SizeOf(ebt);
SendMessage(txtTarget.Handle, EM_SHOWBALLOONTIP, IntPtr.Zero, ebt);
}
private void textBox2_Enter(object sender, EventArgs e)
{
if (Control.IsKeyLocked(Keys.CapsLock))
{
ShowBalloon((TextBox)sender, "Warning",
"Having the Caps Lock on may cause you to enter your password incorrectly.\n\nYou should press Caps Lock to turn it off before entering your password.",
TTI_ICON.TTI_WARNING);
}
}
The ShowBallon method takes a target TextBox control and will do the same job for you without having to worry about the positioning problem. You can make a helper class or wrap the method into a new class derived from the TextBox class.
One thing to note, this message is supported starting from Windows XP. If you're running on Windows 2000, or earlier systems, it won't work.
Just my two cents.
Regards, Jie MSDN Subscriber Support in Forum If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
The CodeFx Project
My Blog (in Simplified Chinese) | | Wang, Jie Friday, September 11, 2009 12:00 PM | Thanks for your suggestion Jie!
I've been trying to implement it, but everytime I run the application, the following line:
SendMessage(txtTarget.Handle, EM_SHOWBALLOONTIP, IntPtr.Zero, ebt);
in the ShowBalloon method throws an exception. The exception is:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I've tried a few things to get around it (including rebooting my computer), but no luck. Any ideas/suggestions? | | FireMyst Monday, September 14, 2009 1:06 AM | I just made some adjustments and wrapped the code into a BalloonTipTextBox.
public class BalloonTipTextBox : System.Windows.Forms.TextBox
{
private const int ECM_FIRST = 0x1500;
private const int EM_SHOWBALLOONTIP = (ECM_FIRST + 3); // Show a balloon tip associated to the edit control
private const int EM_HIDEBALLOONTIP = (ECM_FIRST + 4); // Hide any balloon tip associated with the edit control
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint MSG, IntPtr wParam, ref EDITBALLOONTIP lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint MSG, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
private struct EDITBALLOONTIP
{
public int cbStruct;
public string pszTitle;
public string pszText;
public int ttiIcon;
}
public enum BalloonToolTipIcon
{
None = 0, // TTI_NONE
Info = 1, // TTI_INFO
Warning = 2, // TTI_WARNING
Error = 3, // TTI_ERROR
}
/// <summary>
/// Display a balloon tip on the textbox.
/// This method only works with Windows Common Controls 6 or higher version.
/// </summary>
/// <param name="title">Tip title.</param>
/// <param name="text">Tip text.</param>
/// <param name="icon">Tip icon.</param>
public void ShowBalloonTip(string title, string text, BalloonToolTipIcon icon)
{
if (this.IsHandleCreated)
{
EDITBALLOONTIP bt = new EDITBALLOONTIP();
bt.cbStruct = Marshal.SizeOf(bt);
bt.pszTitle = title;
bt.pszText = text;
bt.ttiIcon = (int)icon;
SendMessage(this.Handle, EM_SHOWBALLOONTIP, IntPtr.Zero, ref bt);
}
}
/// <summary>
/// Hide the balloon tip on the textbox.
/// This method only works with Windows Common Controls 6 or higher version.
/// </summary>
public void HideBalloonTip()
{
if (this.IsHandleCreated)
{
SendMessage(this.Handle, EM_HIDEBALLOONTIP, IntPtr.Zero, IntPtr.Zero);
}
}
}
So now on the form the BalloonTipTextBox shall be used.
private void balloonTipTextBox1_Enter(object sender, EventArgs e)
{
if (Control.IsKeyLocked(Keys.CapsLock))
{
BalloonTipTextBox bt = (BalloonTipTextBox)sender;
bt.ShowBalloonTip("Warning",
"Having the Caps Lock on may cause you to enter your password incorrectly.\n\n" +
"You should press Caps Lock to turn it off before entering your password.",
BalloonTipTextBox.BalloonToolTipIcon.Warning);
}
}
I've tested the code on Windows XP / Windows XP x64 Edition / Windows 7 Enterprise x64with Any CPU/x86/x64 build configurations.  If you're still having problem making it work, please let me know. Regards, Jie MSDN Subscriber Support in Forum If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
The CodeFx Project
My Blog (in Simplified Chinese) | | Wang, Jie Monday, September 14, 2009 2:39 AM | Thanks Jie!
Would love to know how you got the screen captures in here. :-)
Anyway, still causing me problems. On the line: BalloonTipTextBox bt = (BalloonTipTextBox)sender;
it throws the exception: Unable to cast object of type 'System.Windows.Forms.TextBox' to type 'BalloonTipTextBox'
which I don't understand why because BalloonTipTextBox, as per your code, extends the TextBox class.
I must be missing something obvious. Code to all my files is below. Would appreciate your advice.
I am developing under WinXP / SP3with VS 2005. When I run WinVer it tells me "Version 5.1 (Build 2600.xpsp_sp3_gdr.090206-1234: Service Pack 3)" and VS 2005 Professional Edition Version 8.0.50727.42 with .NET version 2.0.50727 SP2.
Also, do you know why the ToolTip.SetToolTip method doesn't get activated in the "_Enter" event method when tabbing through the controls (only does when clicking on it) whereas the ToolTip.Show method does? Assuming the methods aren't commented out of course. :-)
/***********************************************
* Code to Form1.cs
***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
namespace TestADSecurity
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
// Reset
this.Reset();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void btnReset_Click(object sender, EventArgs e)
{
this.Reset();
}
private void Reset()
{
}
private void txtUserName_Enter(object sender, EventArgs e)
{
//ToolTip tt = new ToolTip();
if (Control.IsKeyLocked(Keys.CapsLock))
{
//tt.IsBalloon = true;
//tt.ToolTipIcon = ToolTipIcon.Warning;
//tt.UseAnimation = false;
//tt.ToolTipTitle = "Caps lock is On";
////tt.SetToolTip(this.txtUserName, "Having the Caps Lock on may cause you to enter your password incorrectly.\r\nYou should press Caps Lock to turn it off before entering your password.");
////http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/7fe50cfa-885e-4e2f-bbba-55c0b42d852b
//tt.Show("Having the Caps Lock on may cause you to enter your password incorrectly.\r\n\r\nYou should press Caps Lock to turn it off before entering your password.", this.txtUserName, 0, -90, 3000);
BalloonTipTextBox bt = (BalloonTipTextBox)sender;
bt.ShowBalloonTip("Warning",
"Having the Caps Lock on may cause you to enter your password incorrectly.\r\n" +
"You should press Caps Lock to turn it off before entering your password.",
BalloonTipTextBox.BalloonToolTipIcon.Warning);
}
}
}
}
/***********************************************
* Code to Form1.Designer.cs
***********************************************/
namespace TestADSecurity
{
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.btnTest = new System.Windows.Forms.Button();
this.txtLogonServer = new System.Windows.Forms.TextBox();
this.lblLogonServer = new System.Windows.Forms.Label();
this.txtUserName = new System.Windows.Forms.TextBox();
this.txtResult = new System.Windows.Forms.TextBox();
this.lblUserName = new System.Windows.Forms.Label();
this.btnReset = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// btnTest
//
this.btnTest.Location = new System.Drawing.Point(90, 89);
this.btnTest.Name = "btnTest";
this.btnTest.Size = new System.Drawing.Size(75, 23);
this.btnTest.TabIndex = 2;
this.btnTest.Text = "Test";
this.btnTest.UseVisualStyleBackColor = true;
this.btnTest.Click += new System.EventHandler(this.button1_Click);
//
// txtLogonServer
//
this.txtLogonServer.Location = new System.Drawing.Point(90, 12);
this.txtLogonServer.Name = "txtLogonServer";
this.txtLogonServer.Size = new System.Drawing.Size(100, 20);
this.txtLogonServer.TabIndex = 1;
//
// lblLogonServer
//
this.lblLogonServer.AutoSize = true;
this.lblLogonServer.Location = new System.Drawing.Point(13, 15);
this.lblLogonServer.Name = "lblLogonServer";
this.lblLogonServer.Size = new System.Drawing.Size(71, 13);
this.lblLogonServer.TabIndex = 3;
this.lblLogonServer.Text = "Logon Server";
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(90, 49);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(100, 20);
this.txtUserName.TabIndex = 4;
this.txtUserName.Enter += new System.EventHandler(this.txtUserName_Enter);
//
// txtResult
//
this.txtResult.Location = new System.Drawing.Point(90, 131);
this.txtResult.Multiline = true;
this.txtResult.Name = "txtResult";
this.txtResult.Size = new System.Drawing.Size(406, 53);
this.txtResult.TabIndex = 5;
//
// lblUserName
//
this.lblUserName.AutoSize = true;
this.lblUserName.Location = new System.Drawing.Point(13, 52);
this.lblUserName.Name = "lblUserName";
this.lblUserName.Size = new System.Drawing.Size(60, 13);
this.lblUserName.TabIndex = 6;
this.lblUserName.Text = "User Name";
//
// btnReset
//
this.btnReset.Location = new System.Drawing.Point(180, 89);
this.btnReset.Name = "btnReset";
this.btnReset.Size = new System.Drawing.Size(75, 23);
this.btnReset.TabIndex = 7;
this.btnReset.Text = "Reset";
this.btnReset.UseVisualStyleBackColor = true;
this.btnReset.Click += new System.EventHandler(this.btnReset_Click);
//
// Form1
//
this.AcceptButton = this.btnTest;
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(505, 195);
this.Controls.Add(this.btnReset);
this.Controls.Add(this.lblUserName);
this.Controls.Add(this.txtResult);
this.Controls.Add(this.txtUserName);
this.Controls.Add(this.lblLogonServer);
this.Controls.Add(this.txtLogonServer);
this.Controls.Add(this.btnTest);
this.Name = "Form1";
this.Text = "Test AD Security";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button btnTest;
private System.Windows.Forms.TextBox txtLogonServer;
private System.Windows.Forms.Label lblLogonServer;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.TextBox txtResult;
private System.Windows.Forms.Label lblUserName;
private System.Windows.Forms.Button btnReset;
}
}
/***********************************************
* Code to BalloonTipTextBox.cs
***********************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.Windows.Forms;
public class BalloonTipTextBox : System.Windows.Forms.TextBox
{
private const int ECM_FIRST = 0x1500;
private const int EM_SHOWBALLOONTIP = (ECM_FIRST + 3); // Show a balloon tip associated to the edit control
private const int EM_HIDEBALLOONTIP = (ECM_FIRST + 4); // Hide any balloon tip associated with the edit control
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint MSG, IntPtr wParam, ref EDITBALLOONTIP lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint MSG, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct EDITBALLOONTIP
{
public int cbStruct;
public string pszTitle;
public string pszText;
public int ttiIcon;
}
public enum BalloonToolTipIcon
{
None = 0, // TTI_NONE
Info = 1, // TTI_INFO
Warning = 2, // TTI_WARNING
Error = 3, // TTI_ERROR
}
/// <summary>
/// Display a balloon tip on the textbox.
/// This method only works with Windows Common Controls 6 or higher version.
/// </summary>
/// <param name="title">Tip title.</param>
/// <param name="text">Tip text.</param>
/// <param name="icon">Tip icon.</param>
public void ShowBalloonTip(string title, string text, BalloonToolTipIcon icon)
{
if (this.IsHandleCreated)
{
EDITBALLOONTIP bt = new EDITBALLOONTIP();
bt.cbStruct = Marshal.SizeOf(bt);
bt.pszTitle = title;
bt.pszText = text;
bt.ttiIcon = (int)icon;
SendMessage(this.Handle, EM_SHOWBALLOONTIP, IntPtr.Zero, ref bt);
}
}
/// <summary>
/// Hide the balloon tip on the textbox.
/// This method only works with Windows Common Controls 6 or higher version.
/// </summary>
public void HideBalloonTip()
{
if (this.IsHandleCreated)
{
SendMessage(this.Handle, EM_HIDEBALLOONTIP, IntPtr.Zero, IntPtr.Zero);
}
}
}
- Edited byFireMyst Monday, September 14, 2009 7:14 AMUpdated with Version numbers I'm developing with.
-
| | FireMyst Monday, September 14, 2009 5:35 AM | First, add a new Class to your project, paste the " public class BalloonTipTextBox : System.Windows.Forms.TextBox {...}" codefrom my previous postinto the new class file. You may need to added one more using line toget it compile: " using System.Runtime.InteropServices;". Rebuild the project, and now you'll see BalloonTipTextBox control in the Toolbox when designing your form. Drag & drop the BalloonTipTextBox to the form and use it for your password textbox instead of normal TextBox. I haveuploaded a Visual Studio 2005 sample project to my SkyDrive storage and you can download it for reference. Regards, Jie MSDN Subscriber Support in Forum If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
The CodeFx Project
My Blog (in Simplified Chinese) | | Wang, Jie Monday, September 14, 2009 7:46 AM | Hi Jie,
Thanks. I've done all that already. :-) Sorry I didn't mention it. I did it and took that code out in my last post. Your text box works fine that way when I paste it on the form, but the other regular textbox on the form doesn't.
For our purposes we already have another custom textbox control that's used. As a proof of concept. I need to get this code working for a regular text box (there's no way they want a new custom control made). This way, it will proove we can do it just by editing our current custom textbox control.
I thought this would be a simple exercise... :-)
So still stuck at a loss as to why it won't work with the regular textbox...
| | FireMyst Monday, September 14, 2009 8:01 AM | So how about making a helper class? The code is almost the same:
public static class BalloonHelper
{
private const int ECM_FIRST = 0x1500;
private const int EM_SHOWBALLOONTIP = (ECM_FIRST + 3); // Show a balloon tip associated to the edit control
private const int EM_HIDEBALLOONTIP = (ECM_FIRST + 4); // Hide any balloon tip associated with the edit control
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint MSG, IntPtr wParam, ref EDITBALLOONTIP lParam);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint MSG, IntPtr wParam, IntPtr lParam);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct EDITBALLOONTIP
{
public int cbStruct;
public string pszTitle;
public string pszText;
public int ttiIcon;
}
public enum BalloonToolTipIcon
{
None = 0, // TTI_NONE
Info = 1, // TTI_INFO
Warning = 2, // TTI_WARNING
Error = 3, // TTI_ERROR
}
/// <summary>
/// Display a balloon tip on the textbox.
/// This method only works with Windows Common Controls 6 or higher version.
/// </summary>
/// <param name="title">Tip title.</param>
/// <param name="text">Tip text.</param>
/// <param name="icon">Tip icon.</param>
public static void ShowBalloonTip(TextBox textBox, string title, string text, BalloonToolTipIcon icon)
{
if (textBox.IsHandleCreated)
{
EDITBALLOONTIP bt = new EDITBALLOONTIP();
bt.cbStruct = Marshal.SizeOf(bt);
bt.pszTitle = title;
bt.pszText = text;
bt.ttiIcon = (int)icon;
SendMessage(textBox.Handle, EM_SHOWBALLOONTIP, IntPtr.Zero, ref bt);
}
}
/// <summary>
/// Hide the balloon tip on the textbox.
/// This method only works with Windows Common Controls 6 or higher version.
/// </summary>
public static void HideBalloonTip(TextBox textBox)
{
if (textBox.IsHandleCreated)
{
SendMessage(textBox.Handle, EM_HIDEBALLOONTIP, IntPtr.Zero, IntPtr.Zero);
}
}
}
And now you can call the BalloonHelper.ShowBalloonTip method, passing in a reference to a TextBox and other required parameters.
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
If you have any feedback, please tell us.
The CodeFx Project
My Blog (in Simplified Chinese) | | Wang, Jie Tuesday, September 15, 2009 8:11 AM |
|