Display Ballon Tip on Button Click - c#

I want to display a ballon tip when an error occures instead of showing MessageBox.
[NOTE] i did not want it to be shown on mouse Hover.
I tried both but they actually show the tip on mouse hover
toolTip1.SetToolTip();
toolTip1.Show();

You can use the ToolTip Popup event to check if there is a Tooltip present and cancel it if there isn't. You can then set the tooltip during your validation then show it. In this example I set a timer to reset the tooltip text after a 2 second timeout.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.IsBalloon = true;
toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);
toolTip1.SetToolTip(textBox1, "");
}
void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (toolTip1.GetToolTip(e.AssociatedControl) == "")
e.Cancel = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
toolTip1.RemoveAll();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
int temp;
if (!int.TryParse(textBox1.Text, out temp))
showTip("Validation Error", (Control)sender);
}
private void showTip(string message, Control destination)
{
toolTip1.Show(message, destination);
timer1.Start();
}
}

Much to my surprise it appears that toolTip1.IsOpen = true will show a tooltip and allow it to stay open. Note that you will need to provide code to close it because it wouldn't go away on its own on my machine no matter what I did.

Related

Detect keyboard input and mouse activity on a single form

I have this below form and I want to detect key-strokes and any mouse clicks on the button.
For example, if I press S on my keyboard, message will show START and keypress will display keyboard press. The same thing happen when mouse-click on START button, except that keypress will display START BTN.
Here is my button code. If I only do for button or only IsKeyDown it works fine, but when I combine both in one form, they go haywire.
private void btnStart_Click(object sender, EventArgs e)
{
lblKeypress.Text = "START BTN";
lblmessage.Text = "START";
}
Here is my Keyboard.IsKeyDown code:
private void Form1_KeyPress(object sender, KeyPressEventArgs e)
{
if (Keyboard.IsKeyDown(Key.S))
{
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Please help, thanks.
try this code
private void Form1_KeyDown(object sender,
EventArgs e){
if (e.KeyData == Keys.S){
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Make sure that in your Form you set KeyPreviewProperty to TRUE
In Form1.Designer.cs
this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.form_keydown);
private void form_keydown(object sender, KeyEventArgs e)
{
int keyVal = (int)e.KeyValue;
keyValue = -1;
if ((keyVal >= (int)Keys.S))
{
keyValue = (int)e.KeyValue - (int)Keys.S;
lblKeypress.Text = "Keyboard Press";
lblmessage.Text = "START";
}
}
Note:
Ensure you do not have any unrelated WPF namespaces`
(Keyboard.IsKeyDown(Key.S)) is a WPF approach, in WinForms you are better off using combination of e.KeyValue & Keys

C# If messagebox is displayed, then stop timer

When I write something wrong on a textbox and click a button, a messagebox pops up, and keeps popping up since I have a timer.
So I want to make an if statement that if the messagebox is displayed, then stop the timer, until the button is clicked once again.
I tried using this:
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
if (errormsg)
{
timer1.Stop();
}
data();
}
private void data()
{
//code
Now here's what's in my timer1 code:
private void timer1_Tick(object sender, EventArgs e)
{
int value;
if (int.TryParse(textBox1.Text, out value))
{
if (value > 0)
{
timer1.Interval = value;
}
}
button1.PerformClick();
}
here's the error message:
private void errormsg()
{
MessageBox.Show("Sorry, there was an error. Please, try again.");
}
I will also note that I'm using errormsg in an else statement on my //code
//code
else
{
errormsg();
}
So my question is:
How can I make the timer stop, if a wrong value is displayed on my textbox (//code) causing a messagebox to appear. Then, when a correct value is displayed on a textbox, and I click the button, the timer would start again?
Stop the timer in your errormsg() function. When you clicked the button1 it starts again.
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
data();
}
private void errormsg()
{
timer1.stop();
MessageBox.Show("Sorry, there was an error. Please, try again.");
}

Moving a picture box gradually, not instantly

I have an PictureBox that I want to move up on the y axis after a button click. The problem is that the PictureBox simply appears there after the button click. I want it to move to the new position, not teleport. What do I do?
public partial class Form1 : Form
{
Point stageplus1 = new Point(164, 325);
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
pictureBox5.Location = stageplus1;
}
private void pictureBox5_Click(object sender, EventArgs e)
{
}
}
Expanding on BJ Myers comment this is how you can implement that:
private void button2_Click(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
// calculate new position
this.pictureBox1.Top++;
this.pictureBox1.Left++;
// when to stop
if (this.pictureBox1.Top > (this.Height - (2 * this.pictureBox1.Height)))
{
this.timer1.Enabled = false;
}
}
I used the default Timer control that raises a Tick at an interval. The Tick event gets executed on the UI thread so you don't have to be bothered by cross thread errors.
If added to your form this is what you'll get:
If you need to animate more stuff you might want to look into using a Background worker and helper classes like I show in this answer of mine

Show button only when focus is on textbox

Is this possible to display button on Windows Form only when focus is on specific textbox?
Tried that with this approach:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
button3.Visible = false;
}
No luck, because button click does not work then, because button is hidden immediately after textbox lost focus, preventing it from firing button3_Click(/*...*/) { /*...*/ } event.
Now I'm doing it like that:
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
//button3.Visible = false;
DoAfter(() => button3.Visible = false);
}
private async void DoAfter(Action action, int seconds = 1)
{
await Task.Delay(seconds*1000);
action();
}
Form now waits for a second and only then hides button3.
Is there any better approach?
I think you want to display the button only when focus is on specific textbox or the focus is on the button.
To do this you can check the Focused property of button3 in the Leave event of textBox2 and only hide the button if the button doesn't have focus. Note that the button will get focus before the Leave event of textBox2 fires.
You will then need to hide the button in the scenario where button3 loses focus and the focus moves to somewhere other than textBox2. You can use exactly the same technique here by handling the Leave event of button3 and only hiding button3 if textBox2 does not have focus.
The following code should fit your requirements:
private void textBox2_Leave(object sender, EventArgs e)
{
if (!button3.Focused)
{
button3.Visible = false;
}
}
private void button3_Leave(object sender, EventArgs e)
{
if (!textBox2.Focused)
{
button3.Visible = false;
}
}
private void textBox2_Enter(object sender, EventArgs e)
{
button3.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked");
}
Why not work with the GotFocus and LostFocus event of the TextBox?
private void textBox2_GotFocus(object sender, EventArgs e)
{
button3.Visible = true;
}
Then hide the button on the click event.
private void button3_Click(object sender, EventArgs e)
{
MessageBox.Show("OK");
button3.Visible = false;
}
How about you add a Panel and place the button and text boxes in that panel and when user MouseHovers that Panel then display the button...
This way user would be able to click on the button...
This is the event you are looking for, I think...
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousehover(v=vs.110).aspx
UPDATE:
var textboxFocussed = false;
private void textBox2_Enter(object sender, EventArgs e)
{
textboxFocussed = true;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textboxFocussed = false;
}
UPDATE 2
private void Panel_GotFocus(object sender, EventArgs e)
{
button3.Visible = textboxFocussed;
}
private void Panel_LostFocus(object sender, EventArgs e)
{
button3.Visible = false;
}
Here are the details of the Panel Events
you can add Enter event handler for all controls on form at Load. Just make sure to skip the controls on which you want to show the button.
List<string> strControlException = new List<string>();
public Form1()
{
InitializeComponent();
strControlException.Add("btnMain");
strControlException.Add("txtMain");
}
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < this.Controls.Count;i++ )
{
if (!strControlException.Contains(Controls[i].Name))
{
Controls[i].Enter += new EventHandler(hideButton);
}
}
}
private void txtMain_Enter(object sender, EventArgs e)
{
btnMain.Visible = true;
}
private void hideButton(object sender, EventArgs e)
{
btnMain.Visible = false;
}
btnMain (Button you want to Manipulate) and txtMain (Which controls the vibility of the button) are the controls in contention here
Add more controls on the form to test.
Explanation for the above code :
First initialize a list with the names of controls that should show the Button
On Form Load add an Event handler to all controls (except the one in our list)
In the handler function hide the button. (You might want to perform more logic here based on the control that called this function)
Button is hidden by default and only on textbox Enter event we show the button.

CheckBox button and ContextMenuStrip for dropdown menu

I'm tring to implement a button which have a dropdown menu when checked and this menu is gone when unchecked. My problem is I cannot uncheck the checkbox when it or its menu lost focus.
The checkbox's appearance mode is button.
My code:
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
I've tried to uncheck the checkBox on contextMenuStrip's VisibleChanged / Closed event but this caused menu not to hide (or hide and show immediately).
The example below does not, of course, include the code you would need for swapping BackGroundImage of the CheckBox to indicate CheckState. The events to "wire-up" should be obvious. Hope this is helpful.
// tested in VS 2010 Pro, .NET 4.0 FrameWork Client Profile
// uses:
// CheckBox named 'checkBox1
// ContextMenuStrip named 'contextMenuStrip1
// TextBox named 'cMenuSelectionInfo for run-time checking of results
// used to position the ContextMenuStrip
private Point cPoint;
// context click ? dubious assumption that 'right' = context click
private bool cmOpenedRight;
// the clicked ToolStripMenuItem
private ToolStripMenuItem tsMIClicked;
private void checkBox1_MouseDown(object sender, MouseEventArgs e)
{
cmOpenedRight = e.Button == MouseButtons.Right;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
// positioning the CheckBox like this
// is something in a 'real-world' example
// you'd want to do in the Form.Load event !
// unless, of course, you'd made the CheckBox movable
if(checkBox1.Checked)
{
contextMenuStrip1.Show();
cPoint = PointToScreen(new Point(checkBox1.Left, checkBox1.Top + checkBox1.Height));
contextMenuStrip1.Location = cPoint;
}
else
{
contextMenuStrip1.Hide();
}
}
private void contextMenuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
// assume you do not have to check for null here ?
tsMIClicked = e.ClickedItem as ToolStripMenuItem;
tbCbMenuSelectionInfo.Text = tsMIClicked + " : " + ! (tsMIClicked.Checked);
}
private void contextMenuStrip1_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = checkBox1.Checked;
}
private void contextMenuStrip1_Closed(object sender, ToolStripDropDownClosedEventArgs e)
{
if (cmOpenedRight)
{
tbCbMenuSelectionInfo.Text += " : closed because : " + e.CloseReason.ToString();
}
}
I think your approach of unchecking the check box on the context menu's closed event is a good one, what you need is a bit of "event cancelling logic"(c), like this:
private void OnContextClosing(object sender, EventArgs e)
{
_cancel = true;
cbSettings.Checked = false;
_cancel = false;
}
private void cbSettings_CheckedChanged(object sender, EventArgs e)
{
if(_cancel)
return;
if (cbSettings.Checked) {cmsSettings.Show(cbSettings, 0, cbSettings.Height);}
else {cmsSettings.Hide();}
}
This will keep your CheckChanged event from re-checking your checkbox.

Categories