C# WASD control? - c#

So I have a picturebox named picbox1 and an image inside it. I need it to move up if W is pressed , left if A is pressed and you get the idea. What's the easiest way to do this? Could you please explain the code as well.
Here is my code(just in case):
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Threading;
public class MyProgram {
public static void Main() {
Form form1 = new Form();
form1.Show();
form1.Width = 800;
form1.Height = 600;
PictureBox picbox1 = new PictureBox();
form1.Controls.Add(picbox1);
picbox1.Image = Image.FromFile("c:\\Users\\FakeUsername\\Downloads\\AnimatedTest.gif");
Application.Run();
}
}

Creating form and picture box dynamically in Program.cs code is not good idea. You should declare your Form1 from in another file. Put pictureThen use KeyPress event:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
int offset = 10;
if(e.KeyChar == 'a')
{
pictureBox1.Location = new Point(pictureBox1.Location.X - offset, pictureBox1.Location.Y);
}
else if(e.KeyChar == 'w')
{
pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y - offset);
}
else if (e.KeyChar == 's')
{
pictureBox1.Location = new Point(pictureBox1.Location.X, pictureBox1.Location.Y + offset);
}
else if (e.KeyChar == 'd')
{
pictureBox1.Location = new Point(pictureBox1.Location.X + offset, pictureBox1.Location.Y);
}
}

Related

C# drag a snapping control

I am creating a form that when a panel is dragged from a parent panel to another panel, the new panel becomes the parent. I created a version of the program, but I'm not sure what I'm missing. How would I add a timer to the dragged panel it so that it doesn't move immediately after clicking it, and properly create snapping bounds so that it snaps within a certain distance around the parent panel.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace SNAPtest
{
public partial class Form1 : Form
{
Point MousePoint = new Point();
public Form1()
{
InitializeComponent();
panel_white.MouseDown += new MouseEventHandler(White_MouseDown);
panel_white.MouseMove += new MouseEventHandler(White_MouseMove);
}
private void White_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int mx = Math.Min(Math.Max(panel_white.Location.X - MousePoint.X + e.X, 0), panel_white.Parent.Width - panel_white.Width);
int my = Math.Min(Math.Max(panel_white.Location.Y - MousePoint.Y + e.Y, 0), panel_white.Parent.Height - panel_white.Height);
panel_white.Location = new Point(mx, my);
}
if (panel_white.Location.X == Math.Max(panel_maroon.Location.X, 10) | panel_white.Location.Y == Math.Max(panel_maroon.Location.Y, 100))
{
Console.WriteLine("Maroon has focus");
panel_white.Parent = panel_maroon;
}
if (panel_white.Location.X == Math.Max(panel_blue.Location.X, 10) | panel_white.Location.Y == Math.Max(panel_blue.Location.Y, 100))
{
Console.WriteLine("Blue has focus");
panel_white.Parent = panel_blue;
}
}
private void White_MouseDown(object sender, MouseEventArgs e)
{
panel_white.Parent = this;
panel_white.BringToFront();
if (e.Button == MouseButtons.Left)
{
MousePoint = e.Location;
}
}
}
}

C# keyboard event handler rectangle

I am new to C# and try to make an keyboard event. It should display when the keys W, A, S or D are pressed. First my plan was to show some pictureBox and just change the picture if the right keys are pressed.
But then I searched the internet an found something similar in Java http://docs.oracle.com/javase/8/javafx/sample-apps/KeyboardExample.zip
and it looks like this:
As I can understand the code is drawing a rectangle with some letter in it. I looked the msdn and found a example for drawing a rectangle:
https://msdn.microsoft.com/de-de/library/sx8yykw8(v=vs.110).aspx
Unfortunately I stucking in the drawing. Normally I use the toolbox to add things to the form. Then I double-click it and write my code inside the braces. But there is no "Rectangle" in the toolbox, so I am not sure how to add it.
This is my code so far:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//Stay always on top
this.TopMost = true;
//Does not work. Removes border but you can't move the window after this
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
}
private void Form1_Load(object sender, EventArgs e)
{
//Can I delete this?
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 65 && e.KeyChar <= 122)
{
switch (e.KeyChar)
{
//If pressed w or W
case (char)119:
case (char)87:
Console.WriteLine(e.KeyChar);
break;
//If pressed a or A
case (char)97:
case (char)65:
Console.WriteLine(e.KeyChar);
break;
//If pressed s or S
case (char)83:
case (char)115:
Console.WriteLine(e.KeyChar);
break;
//If pressed d or D
case (char)100:
case (char)68:
Console.WriteLine(e.KeyChar);
break;
//Other keys
default:
lblMessage.Text = "Key not supported";
//does not work
//timer1_Tick();
break;
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lblMessage.Hide();
}
}
And here is how my Form looks now:
Other things I am stucking at the moment:
How I can call the timer from the Form1_KeyPress to hide the lblMessage after some seconds?
Remove border without losing the ability to move the window (like with this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; for example)
EDIT: I changed the code to the newest working state.
Welcome to the world of Windows desktop programming!
You have two choices here; you can either:
Add components to the WASD form using the Design View (and since you have the W, A, S, D boxes there it looks like you have already added them in) and in your Form1_KeyPress() handler, just update the properties of the boxes. This can be as simple as the following, just make sure to change it to the correct component name:
//If pressed w or W
case (char)119:
case (char)87:
Console.WriteLine(e.KeyChar);
button1.BackColor = Color.Red;//Highlight W
button2.BackColor = Color.Empty;//Ignore A
button3.BackColor = Color.Empty;//Ignore S
button3.BackColor = Color.Empty;//Ignore D
break;
Override the form's OnDraw() handler and paint the boxes directly on the screen. This is harder but gives you a lot more power.
Turning off the label is easy. In your Form1_Load() handler make sure to set the timer1's timeout property:
private void Form1_Load(object sender, EventArgs e)
{
timer1.Interval = 5000;//In ms = thousandths-of-a-second
}
Turn the timer on in the Form1_KeyPress() handler:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
...
lblMessage.Enabled = true;
timer1.Start();
}
Do your work and turn off the timer in the timer1_Tick() handler:
private void timer1_Tick(object sender, EventArgs e)
{
lblMessage.Enabled = false;
timer1.Stop();
}
Here is what I quickly put together:
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 WASD_Keyboard
{
public partial class Form1 : Form
{
private PictureBox pictureBox1 = new PictureBox();
private bool wPressed = false;
private bool aPressed = false;
private bool sPressed = false;
private bool dPressed = false;
private Timer timer = new Timer();
public Form1()
{
InitializeComponent();
//Stay always on top
this.TopMost = true;
//Does not work. Removes border but you can't move the window after this
this.FormBorderStyle = FormBorderStyle.None;
timer.Interval = 3000;
//this is an event binding
timer.Tick += timer1_Tick;
}
private void Form1_Load(object sender, EventArgs e)
{
// Dock the PictureBox to the form and set its background to white.
pictureBox1.Dock = DockStyle.Fill;
pictureBox1.BackColor = Color.White;
pictureBox1.Paint += DrawRectangleRectangle;
// Add the PictureBox control to the Form.
this.Controls.Add(pictureBox1);
}
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
// reversed logic to stop nesting
if (e.KeyChar < 65 || e.KeyChar > 122) return;
wPressed = false;
aPressed = false;
sPressed = false;
dPressed = false;
//this should really be multiple if statement so it can do more than one key
//If pressed w or W
if (e.KeyChar == (char) 119 || e.KeyChar == (char) 87) {
wPressed = true;
Console.WriteLine(e.KeyChar);
}
//If pressed a or A
if (e.KeyChar == (char) 97 || e.KeyChar == (char) 65) {
aPressed = true;
Console.WriteLine(e.KeyChar);
}
//If pressed s or S
if (e.KeyChar == (char) 83 || e.KeyChar == (char) 115) {
sPressed = true;
Console.WriteLine(e.KeyChar);
}
//If pressed d or D
if (e.KeyChar == (char) 100 || e.KeyChar == (char) 68) {
dPressed = true;
Console.WriteLine(e.KeyChar);
}
if (!wPressed && !aPressed && !sPressed && !dPressed) {
//Something goes wrong
lblMessage.Text = "Key not supported";
return;
}
pictureBox1.Refresh();
// in older .net if you didn't do both you ran into multiple issues
timer.Enabled = true;
timer.Start();
}
public void DrawRectangleRectangle(object sender, PaintEventArgs e)
{
DrawRectangle(e, new Point(40, 10), new Size(20, 20), 'W', wPressed ? Color.Red : Color.White);
DrawRectangle(e, new Point(10, 40), new Size(20, 20), 'A', aPressed ? Color.Red : Color.White);
DrawRectangle(e, new Point(40, 40), new Size(20, 20), 'S', sPressed ? Color.Red : Color.White);
DrawRectangle(e, new Point(70, 40), new Size(20, 20), 'D', dPressed ? Color.Red : Color.White);
}
public void DrawRectangle(PaintEventArgs e, Point p, Size s, char letter, Color c)
{
// Create pen.
var blackPen = new Pen(Color.Black, 3);
var brush = new SolidBrush(c);
// Create rectangle.
var rect = new Rectangle(p, s);
// Draw rectangle to screen.
e.Graphics.DrawRectangle(blackPen, rect);
e.Graphics.FillRectangle(brush, rect);
e.Graphics.DrawString(letter.ToString(), new Font(FontFamily.GenericSerif, 12), Brushes.Blue, rect);
}
private void timer1_Tick(object sender, EventArgs e)
{
wPressed = false;
aPressed = false;
sPressed = false;
dPressed = false;
timer.Enabled = false;
timer.Stop();
pictureBox1.Refresh();
}
}
}
Note: This is highly async but doesn't use any locking...

How to connect pictureboxes with connecting line

first sorry for my English. I am programming application in windows forms. It is something like Packet Tracer. I have four buttons. When I click on them, they dynamicaly create pictureboxes with picture of Router or Switch,.... Each time I click on the button, new picture box(Switch or Router,...), is created. I can also move with this pictureboxes by mouse.
I need to create a button, which connects selected pictureboxes with line(Cable). This pictureboxes should be selected by click on them. It sholud be able to move with this objects(movable line).
Here is 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.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
int a = 0;
int b = 0;
int c = 0;
int d = 0;
PictureBox[] picturebox = new PictureBox[100];
public Form1()
{
InitializeComponent();
}
private void router_Click(object sender, EventArgs e)
{
++a;
picturebox[a] = new PictureBox();
picturebox[a].Name = "picturebox" + a;
picturebox[a].Location = new Point(0 + (a-1) *100,100);
picturebox[a].Size = new Size(70, 70);
picturebox[a].BorderStyle = BorderStyle.None;
picturebox[a].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[a]);
picturebox[a].Image = Image.FromFile(#"D:\\router.jpg");
picturebox[a].Refresh();
picturebox[a].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[a].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[a].MouseUp += new MouseEventHandler(picMouseUp);
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
++b;
picturebox[b] = new PictureBox();
picturebox[b].Name = "picturebox" + b;
picturebox[b].Location = new Point(0 + (b - 1) * 100, 180);
picturebox[b].Size = new Size(70, 70);
picturebox[b].BorderStyle = BorderStyle.None;
picturebox[b].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[b]);
picturebox[b].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
picturebox[b].Refresh();
picturebox[b].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[b].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[b].MouseUp += new MouseEventHandler(picMouseUp);
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
++c;
picturebox[c] = new PictureBox();
picturebox[c].Name = "picturebox" + c;
picturebox[c].Location = new Point(0 + (c - 1) * 100, 260);
picturebox[c].Size = new Size(70, 70);
picturebox[c].BorderStyle = BorderStyle.None;
picturebox[c].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[c]);
picturebox[c].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\pc.jpg");
picturebox[c].Refresh();
picturebox[c].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[c].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[c].MouseUp += new MouseEventHandler(picMouseUp);
}
private void server_Click(object sender, EventArgs e)
{
++d;
picturebox[d] = new PictureBox();
picturebox[d].Name = "picturebox" + d;
picturebox[d].Location = new Point(0 + (d - 1) * 100, 340);
picturebox[d].Size = new Size(70, 70);
picturebox[d].BorderStyle = BorderStyle.None;
picturebox[d].SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(picturebox[d]);
picturebox[d].Image = Image.FromFile(#"D:\HP ProBook 450\Desktop\server.png");
picturebox[d].Refresh();
picturebox[d].MouseDown += new MouseEventHandler(picMouseDown);
picturebox[d].MouseMove += new MouseEventHandler(picMouseMove);
picturebox[d].MouseUp += new MouseEventHandler(picMouseUp);
}
}
}
THank you for your help.
You need to invalidate the parent when you add a picturebox or when you move a picturebox:
(picMouseMove and 4 times in the click handlers, it would be better to use 1 function)
Invalidate();
This is an example OnPaint, drawing lines between the pictureboxes as they are located in the Controls collection: (your picturebox array seems very weird, you always add controls at index 1, always overwriting the previous entry?! i'd suggest using a List if you need to keep a reference to them)
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
var pictureBoxes = Controls.OfType<PictureBox>().ToArray();
if (pictureBoxes.Length > 1)
{
for (int i = 1; i < pictureBoxes.Length; i++)
{
DrawLineBetween(e.Graphics, pictureBoxes[i - 1], pictureBoxes[i]);
}
}
}
This function can be used to draw a line between 2 of your boxes:
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
----- full sample below -----
I refactored your full example, and added the code above to start you off with a working example:
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<PictureBox> pictureboxes = new List<PictureBox>();
public Form1()
{
InitializeComponent();
}
private void AddPictureBox(string imagePath)
{
var pb = new PictureBox();
pb.Name = "picturebox" + pictureboxes.Count;
pb.Location = new Point(pictureboxes.Count * 100, 100);
pb.Size = new Size(70, 70);
pb.BorderStyle = BorderStyle.None;
pb.SizeMode = PictureBoxSizeMode.StretchImage;
this.Controls.Add(pb);
pb.Image = Image.FromFile(imagePath);
pb.Refresh();
pb.MouseDown += new MouseEventHandler(picMouseDown);
pb.MouseMove += new MouseEventHandler(picMouseMove);
pb.MouseUp += new MouseEventHandler(picMouseUp);
pictureboxes.Add(pb);
Invalidate();
}
private void router_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\\router.jpg");
}
private void Form1_Load(object sender, EventArgs e)
{
}
int x = 0;
int y = 0;
bool drag = false;
private void picMouseDown(object sender, MouseEventArgs e)
{
// Get original position of cursor on mousedown
x = e.X;
y = e.Y;
drag = true;
}
private void picMouseMove(object sender, MouseEventArgs e)
{
if (drag)
{
PictureBox pb = (PictureBox)sender;
// Get new position of picture
pb.Top += e.Y - y;
pb.Left += e.X - x;
pb.BringToFront();
Invalidate();
}
}
private void picMouseUp(object sender, MouseEventArgs e)
{
drag = false;
}
private void switch1_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\Grafika\switch1.png");
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void pc_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\pc.jpg");
}
private void server_Click(object sender, EventArgs e)
{
AddPictureBox(#"D:\HP ProBook 450\Desktop\server.png");
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (pictureboxes.Count > 1)
{
var arr = pictureboxes.ToArray();
for (int i = 1; i < arr.Length; i++)
{
DrawLineBetween(e.Graphics, arr[i - 1], arr[i]);
}
}
}
private void DrawLineBetween(Graphics g, PictureBox from, PictureBox to)
{
g.DrawLine(Pens.Black,
new Point(from.Left + from.Width / 2, from.Top + from.Height / 2),
new Point(to.Left + to.Width / 2, to.Top + to.Height / 2));
}
}
}

I still can't clear the rectangle i drawn over a control inside OnPaint event why i can'y clear it?

By clear i mean to redraw or paint or color the control back to it's original.
This is the working code:
using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
namespace FTP_ProgressBar
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
txtHost.TextChanged += anyTextBox_TextChanged;
txtUploadFile.TextChanged += anyTextBox_TextChanged;
txtDir.TextChanged += anyTextBox_TextChanged;
anyTextBox_TextChanged(null, null);
if ((txtHost.Text == "") || txtUploadFile.Text == "")
{
btnUpload.Enabled = false;
}
if (txtDir.Text == "")
{
checkBox1.Enabled = false;
}
}
private void anyTextBox_TextChanged(object sender, EventArgs e)
{
btnUpload.Enabled = txtHost.TextLength > 0 && txtUploadFile.TextLength > 0;
checkBox1.Enabled = txtDir.TextLength > 0;
this.Invalidate();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnBrowse_Click(object sender, EventArgs e)
{
if(this.openFileDialog1.ShowDialog() != DialogResult.Cancel)
this.txtUploadFile.Text = this.openFileDialog1.FileName;
}
private void btnUpload_Click(object sender, EventArgs e)
{
if(this.ftpProgress1.IsBusy)
{
this.ftpProgress1.CancelAsync();
this.btnUpload.Text = "Upload";
}
else
{
FtpSettings f = new FtpSettings();
f.Host = this.txtHost.Text;
f.Username = this.txtUsername.Text;
f.Password = this.txtPassword.Text;
f.TargetFolder = this.txtDir.Text;
f.SourceFile = this.txtUploadFile.Text;
f.Passive = this.chkPassive.Checked;
try
{
f.Port = Int32.Parse(this.txtPort.Text);
}
catch { }
this.toolStripProgressBar1.Visible = true;
this.ftpProgress1.RunWorkerAsync(f);
this.btnUpload.Text = "Cancel";
}
}
private void ftpProgress1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.toolStripStatusLabel1.Text = e.UserState.ToString(); // the message will be something like: 45 Kb / 102.12 Mb
this.toolStripProgressBar1.Value = Math.Min(this.toolStripProgressBar1.Maximum, e.ProgressPercentage);
}
private void ftpProgress1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if(e.Error != null)
MessageBox.Show(e.Error.ToString(), "FTP error");
else if(e.Cancelled)
this.toolStripStatusLabel1.Text = "Upload Cancelled";
else
this.toolStripStatusLabel1.Text = "Upload Complete";
this.btnUpload.Text = "Upload";
this.toolStripProgressBar1.Visible = false;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Pen penBorder;
if (txtHost.TextLength <= 0)
{
penBorder = new Pen(Color.Red, 3);
e.Graphics.DrawRectangle(penBorder, txtHost.Location.X, txtHost.Location.Y, txtHost.Width - 1, txtHost.Height - 1);
}
if (txtUploadFile.TextLength <= 0)
{
penBorder = new Pen(Color.Red, 3);
e.Graphics.DrawRectangle(penBorder, txtUploadFile.Location.X, txtUploadFile.Location.Y, txtUploadFile.Width - 1, txtUploadFile.Height - 1);
}
}
}
}
I saw now that without a breakpoint if i minimize form1 when the program is running after typed text in both textBoxes and then resize the form1 it does clear the rectangles.
Strange it seems that it's taking effect only when i minimize and resize back the form1.
In the TextChanged event i tried to add: txtHost.Invalidate(); but it didn't help.
The only way that the rectangle get clear is if i minmize and resize back form1.
Or adding this.Invalidate(); did the trick.
OnPaint() only gets called when the window needs to be updated. This is a basic principle about how Windows works. If you need the window to be updated now then, yes, you need to invalidate the window so that OnPaint() will be called.
But is it ok to redraw all the form?
Sure, but it's not very performant as you are redrawing areas that don't necessarily need redrawing. Invalidate() should have a version that accepts a rectangle argument. Use it to only invalidate the area you want to update.

Show tooltip of a disabled button which is inside a group box in Windows form?

I have a group box set visible true.Have 3 button inside it .btn1 visible true.btn 2 ,btn 3 visible false..want to show tooltip for this 3 button in exact position?
What i am doing now
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;
using System.Threading;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
private ToolTip toolTip = new ToolTip();
private bool isShown = false;
ToolTip toolTip1 = new ToolTip();
bool IsShown = false;
public Form1()
{
InitializeComponent();
button1.Enabled = false;
toolTip1.InitialDelay = 0;
}
private void Form1_MouseMove_1(object sender, MouseEventArgs e)
{
//if (button1 == this.GetChildAtPoint(e.Location))
//{
// if (!isShown)
// {
// toolTip.Show("MyToolTip", this, e.Location);
// isShown = true;
// }
//}
//else
//{
// toolTip.Hide(textBox1);
// isShown = false;
//}
Control ctrl = this.GetChildAtPoint(e.Location);
if (ctrl != null)
{
if (ctrl == this.button1 && !IsShown)
{
string tipstring = this.toolTip1.GetToolTip(this.button1);
this.toolTip1.Show(tipstring, this.button1, this.button1.Width / 2, this.button1.Height / 2);
IsShown = true;
}
}
else
{
this.toolTip1.Hide(this.button1);
IsShown = false;
}
}
private void Form1_Load(object sender, EventArgs e)
{
// Set up the delays for the ToolTip.
toolTip1.AutoPopDelay = 5000;
toolTip1.InitialDelay = 1000;
toolTip1.ReshowDelay = 500;
// Force the ToolTip text to be displayed whether or not the form is active.
toolTip1.ShowAlways = true;
// Set up the ToolTip text for the Button and Checkbox.
toolTip1.SetToolTip(this.button1, "My button1");
}
}
}
I have tried with 1 button it failed ...need help on the same
I even tried
http://msdn.microsoft.com/en-us/library/system.windows.forms.tooltip.aspx
But it didn't work for disabled controls
Because MouseHover event on disabled button won't fire, you could set MouseMove event on your form and check for buttons position, like this way:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X >= button.Location.X && e.X < button.Location.X + button.Width
&& e.Y >= button.Location.Y && e.Y <= button.Location.Y + button.Height)
{
if (!isShown)
{
tt.Show("MyToolTip", button, button.Width / 2, button.Height / 2);
isShown = true;
}
}
else
{
tt.Hide(button);
isShown = false;
}
}
EDIT (to work with GroupBox):
In your *.Designer.cs class you should add this delegate (with the same callback mentioned above):
this.groupBox1.MouseMove += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseMove);
Try this I think it will work.

Categories