Using MouseHover event and ToolTip - c#

To show the relevant information(in monopoly game, the property belongs which player, current market price etc.), I put a Label on the top of a panel, and used a ToolTip object to display the information. This is the image of my current setup.
Here are the steps I have done:
1.Added MouseHover event handler (The Label name is MEDITERANEAN)
this.MEDITERANEAN.MouseHover += new System.EventHandler(this.MEDITERANEAN_MouseHover);
2.Initialized Tooltip
private void InitializeToolTip()
{
toolTipLabel.ToolTipIcon = ToolTipIcon.Info;
toolTipLabel.IsBalloon = true;
toolTipLabel.ShowAlways = true;
}
3.Call setToolTip() in MouseHover call back function
private void MEDITERANEAN_MouseHover(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(MEDITERANEAN, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}
But when I start application and move my cursor over the label, there is no text from toolTipLabel. What part did I make mistakes?
Interestingly, i made other function and it works.
private void panelBoard_MouseOver(object sender, EventArgs e)
{
toolTipLabel.SetToolTip(panelBoard, "You put mouse over me");
rolledDice.AppendText("Mouse Over");
}

I think you just need to bring your lable control in front of image. Try something like this .
MEDITERANEAN.BringToFront();

I found the solution, first I should set Panel's property "Enable" to true, then set label's property "visible" to true as well.

Related

How do I highlight the text in a textbox at the startup of a window?

I asked a question similar to this a few days ago here. The answer works fine, with the exception of performing the same operation at the start up of the window. This means that I need to have the text in a textBox highlighted every time the window is opened.
Currently I am setting the focus to the textBox at startup with no problem in the constructor. With that being said, I am guessing that the correct area to perform this operation is in the constructor. This is what I am trying currently with no luck:
public AddDestination()
{
InitializeComponent();
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
How can I make it so that the text inside my textBox is highlighted whenever the window opens?
Instead of constructor, move it to AddDestination_Load event.
public AddDestination()
{
InitializeComponent();
}
private void AddDestination_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
textBox1.Focus();
//Highlights text **DOES NOT WORK
textBox1.SelectionStart = 0;
textBox1.SelectionLength = textBox1.Text.Length;
}
You can write this segment of code in Load EventHandler
//code
InitializeComponent();
//code
private void Form_Load(object sender, EventArgs e)
{
//Give cursor focus to the textbox
destination_textBox.Focus();
//Highlights text **DOES NOT WORK
destination_textBox.SelectionStart = 0;
destination_textBox.SelectionLength = destination_textBox.Text.Length;
}
To do so go to Designer, click on the window, go to Properties, click Events button, search for Load event then double-click on it.

Execution of RichTextBox event handler "textChanged"

I have a winform application that has a set of RichTextBoxes.
I want to change the text color into Red when the text box contents change
and I have a button, when this button is clicked, the text color is reset to its default color.
The problem is, when I use the event handler (TextChanged) to detect if change in Contents has occured, it is also triggered when the color is reset.
to be more clear I will give an example:
1- the contents of the text box change
2- the event handler is triggered and text color is changed to Red.
3- the button is clicked, then the text is black again
4- when the text color changes, the event handler is triggered again and color is changed to Red.
so, The color seems to be always Red even if the button is clicked.
how Can I handle this problem? I need to detect only the change in contents, not in color
here is a piece from the code:
private void AHReg_TextChanged(object sender, EventArgs e)
{
AHReg.ForeColor = Color.Red;
}
private void RunButton_Click(object sender, EventArgs e)
{
resetControlColor(); //this function sets the text color to Black
}
There's a few ways to skin this cat. You can track the actual text and look for mismatches, or handle the ForeColorChanged event, but the simplest way I think in your case is to just "turn off" the event subscription when you do your reset.
For example, in your RunButton_Click method:
private void RunButton_Click(object sender, EventArgs e)
{
AHReg.TextChanged -= AHReg_TextChanged;
resetControlColor(); //this function sets the text color to Black
AHReg.TextChanged += AHReg_TextChanged;
}
If you need that event to be active in your resetControlColor() function then you'll need to come at this at a different angle, but that's the simplest away to approach it.
you can add a boolean variable called NeedToBeChangedin your class.
private bool NeedToBeChanged = true;
private void RunButton_Click(object sender, EventArgs e)
{
NeedToBeChanged =false;
resetControlColor(); //this function sets the text color to Black
NeedToBeChanged =true;
}
private void AHReg_TextChanged(object sender, EventArgs e)
{
if(NeedToBeChanged)
AHReg.ForeColor = Color.Red;
}

Tooltip on form load

I have a login form. The form has 2 textboxes, and 3 buttons. one button says "Students."
What I want to do is to show a tooltip on this button when the form opens. I dont want to have to go to the button and hover on it in order for it to display. I want the form to load, show the tooltip, and then the tooltip should disappear after 5 seconds. this is what I have tried so far:
private void Form1_Load(object sender, EventArgs e)
{
toolTip.IsBalloon = true;
toolTip.ToolTipIcon = ToolTipIcon.Info;
toolTip.ShowAlways = true;
toolTip.UseFading = true;
toolTip.UseAnimation = true;
toolTip.ToolTipTitle = "Student Mode";
toolTip.Show("You don't have to log in if you are a student. Just click here to go to the questions.", btnStudent);
}
The form's Load event is mis-used far too often. It is here, the event fires before the window becomes visible. Your tooltip is therefore not visible either.
Move your code to a Shown event handler instead. Do favor overriding OnShown() btw, it doesn't make sense for a class to listen to its own events.
protected override void OnShown(EventArgs e) {
base.OnShown(e);
// Your code here
//...
}

How to expand the picturebox into another form?

Currently, my application displays 6 picture boxes, each displaying an picture which is being constantly updating.
Now, I want upon clicking any picture box that picture box extends and fill up the whole screen just showing that chosen picture box.
Is this possible? Must i create another form to do this?
Thanks In Advance,
Perumal
in the onclick event for each the picture box (they can all point to this same method)
picturebox_Click(object sender .....)
{
PictureBox pb= (PictureBox)sender;
if (pb.dock==DockStyle.None)
{
pb.dock=DockStyle.Fill;
pb.BringToFront();
}
else
pb.dock=DockStyle.None;
}
Not seeing any code, here is how you can programmatically change a picture box on click.
pictureBox1.Dock = DockStyle.Fill
So you need to create a on click event handler and call your picture box's Dock function like the above.
update in response to comments
There is a DockStyle.None to revert the picture back to original size.
If i understand you correctly, you want to have 6 pictures and then when you click one it fills, click again, shrinks, click another one, fills etc etc...
To do this, you would use the Dock and Visible properties on the picture boxes. Now it also seems as if you are asking how to actually write the code. Well if you show some code, I could give pointers, with nothing to go on the way I'd approach it is to:
Put all your picture boxes in a list and assign a state to them Big or Small.
Write a OnClick for each picture box to change the state of the picture box clicked on.
Each OnClick then calls a helper function that iterates through each picture box in the list and hides the small one and DockStyle.Fill the big one.
Does the above algorithm accomplish what you need?
try something like this. the code is not re factored but I am sure you can do that
private bool isfill = false;
private void pictureBox1_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox1.Dock = DockStyle.Fill;
pictureBox2.Visible = false;
isfill = true;
}
else
{
pictureBox1.Dock = DockStyle.None;
pictureBox2.Visible = true;
isfill = false;
}
}
private void pictureBox2_Click(object sender, EventArgs e)
{
if (!isfill)
{
pictureBox2.Dock = DockStyle.Fill;
isfill = true;
pictureBox1.Visible = false;
}
else
{
pictureBox2.Dock = DockStyle.None;
isfill = false;
pictureBox1.Visible = true;
}

How to disable cursor in textbox?

Is there any way to disable cursor in textbox without setting property Enable to false?
I was trying to use ReadOnly property but despite the fact that I can't write in textbox, the cursor appears if I click the textbox. So is there any way to get rid of this cursor permamently?
In C#, you can use the following read-only textbox:
public class ReadOnlyTextBox : TextBox
{
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
public ReadOnlyTextBox()
{
this.ReadOnly = true;
this.BackColor = Color.White;
this.GotFocus += TextBoxGotFocus;
this.Cursor = Cursors.Arrow; // mouse cursor like in other controls
}
private void TextBoxGotFocus(object sender, EventArgs args)
{
HideCaret(this.Handle);
}
}
In C# you can disable the cursor in a textbox by temporarily disabling and then re-enabling the text box whenever it receives the focus. Note there is no need to make the textbox read only if using this method. For example:
private void TextBox_Enter(object sender, EventArgs e)
{
TextBox.Enabled = false;
TextBox.Enabled = true;
}
You could use a Label instead. When in the designer, you set BorderStyle = Fixed3D, BackColor = Window and AutoSize = False, it looks a lot like a TextBox.
However, the cursor in a TextBox is provided so that the user can scroll through the text when it is longer than the box. You'll lose that functionality with a Label, unless you are sure that it will always fit. Other than that, it is not possible to remove the cursor from a TextBox.
Putting the hideCaret function inside the TextChanged event will solve the problem:
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
private void textBox1_TextChanged(object sender, EventArgs e)
{
HideCaret(textBox1.Handle);
}
Easiest solution for me was to just override the on focus event and focus back to the parent. This prevents the cursor and any editing of the textbox by the user and basically disables the text box with out having to set the Enabled = false property.
private void Form1_load(object sender, EventArgs e) {
textBox1.ReadOnly = true;
textBox1.Cursor = Cursors.Arrow;
textBox1.GotFocus += textBox1_GotFocus;
}
private void textBox1_GotFocus(object sender, EventArgs e) {
((TextBox)sender).Parent.Focus();
}
Like #Mikhail Semenov 's solution, you can also use lambda express to quickly disable the cursor if you do not have many textboxes should do that:
[DllImport("user32.dll")]
static extern bool HideCaret(IntPtr hWnd);
textBox1.ReadOnly = true;
textBox1.BackColor = Color.White;
textBox1.GotFocus += (s1, e1) => { HideCaret(textBox1.Handle); };
textBox1.Cursor = Cursors.Arrow;
You can set it programatically.
textBox1.Cursor = Cursors.Arrow;
This is not strictly an answer to the question, but perhaps it can solve some similar problem(s). I use a textbox control which can look like a label for a control which displays a scale, but can be edited, when clicked. Start enabled = false and make an activation (enabled = true) in a mousehandler of the parent of the textbox control (which, when disabled, border None and backcolor = parent backcolor, looks like a label). E.g. when enter hit or other event, disable again in KeyDown handler.
(Of course the parent mouse click routine can check whether the mouseclick really occured in the label/textbox control).
If you need the textbox control to activate by tabbing, some more work is required (than I have done).
I use the form constructor to find the textbox parent at runtime and to apply the delegate mouse control. Perhaps you can do this as wel in compile time (Form header), but that seemed a little error-prone to me.
One way of doing it is using View + TabIndex, you can do indexing of some other controls on the dialog as first, let say for buttons if there any. Then as if the control tabIndex is not the first i.e 0, cursor won't get appear there.
To disable the edit and cursor in the TEXT BOX
this.textBox.ReadOnly = true;
this.textBox.Cursor = Cursors.No;//To show a red cross icon on hover
this.textBox.Cursor = Cursors.Arrow //To disable the cursor
you can use RightToLeft Property of Text Box, set it to true, you will not get rid of the Cursor, but it will get fixed at right corner and it will not appear automatically after every text you type in your text Box. I have used this to develop an application like Windows Calculator.

Categories