Can I remove the dotted focus rectangle over tabs on a TabControl? - c#

I have a tab control and need to remove the dotted focus rectangle around the selected tab.
I have set the TabStop property of the TabControl to false. However if I click on a tab and press the Tab key, the dotted rectangle appears around the tabname.
I have tried creating my own TabControl and tried this
class MyTabControl : TabControl
{
public MyTabControl()
{
TabStop = false;
DrawMode = TabDrawMode.OwnerDrawFixed;
DrawItem += new DrawItemEventHandler(DoMoreTabControl_DrawItem);
Invalidate();
}
}
However, the dotted rectangle still appears.
I also tried overriding the MyTabControl.OnPaint() method but it doesn't help.
Is there any way to achieve this?

Set the focus to tab instead of header (like this)
private void tabControl1_Click(object sender, EventArgs e)
{
(sender as TabControl).SelectedTab.Focus();
}
You will see dotted rectangle for a millisecond, as soon as the above event gets executed it will disappear.
Also, to remove dotted rectangle for default selected tab on load
private void tabControl1_Enter(object sender, EventArgs e)
{
(sender as TabControl).SelectedTab.Focus();
}
Both this changes worked for me!
hope it helps somebody.

Yes, DrawItem event. You didn't post it, impossible to guess what's wrong with it. Just make sure that you don't call e.DrawFocusRectangle(), likely to present when you copied the MSDN sample code. Simply deleting the statement is sufficient. Consider using a different background color or text font style as an alternative so the focus hint isn't entirely lost.

Related

Remove button border on tab c# winforms

I have a button on my form that has flat style applied and uses a background image, I have removed all borders from the button, but when I tab onto the button from another control a black border appears around the button.
This can be seen in the image below. On the left is the button with black border on the right is a different button but shows how the cancel button should look.
I do not get this border, if I set the BoderSize to 0 in the FlatAppearance section.
Further investigation shows that this border appears only when the button is the default button. You can create your own button, which does never show this border like this
public class NoNotifyButton: System.Windows.Forms.Button
{
public override void NotifyDefault(bool value)
{
}
}
Note: NotifyDefault remains intentionally empty.
You have to make a new button class using IButtonControl and change NotifyDefault to false:
base.NotifyDefault(false);
You don't have to create a derived class.
You can set the ForeColor to be the same as parent control BackColor.
As follows :
btn1.ForeColor = btn1.Parent.BackColor;
You can do it setting the button property "ForeColor" to transparent
I managed to get around this by setting the button TabStop property to False and then using this code on the button click event
private void sendBackTab()
{
System.Windows.Forms.SendKeys.SendWait("+{TAB}");
}

Why the shape of cursor is wrong during my drag-drop operation

I have some code to do drag-drop between items of two list boxes. which I got it to work finally. so it is doing the drag-drop But the shape of the cursor icon is Wrong. for example when I am doing the "drop" on the second list box the icon is still that Stop Circle or whatever its name is. not sure. but yea the icon of the cursor is wrong. Any thoughts how can I fix this?
In the DragEnter event of the ListBox you can set the DragEventArgs.Effect to a DragDropEffects value:
private void ListBox1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
Not sure what control you are using, but aside from setting the correct DragDropEffect, if you are using the RichTextBox and have EnableAutoDragDrop = true, I noticed some issues where it wants to do a Move from some sources that don't allow it. Holding down [ctrl] switches the dragdrop mode to Copy and allows you to drop it in the target control.

ToolStripButton "Reset Appearance" / "Fire Leave Event"

I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)

WPF ScrollViewer

I was wondering if there is anyway to check if the up arrow or the bottom arrow of a wpf scroll viewer is clicked.
I am trying to do it within a wpf textbox but, I want it to snap to the next line of a text instead of displaying partial text.
So, the way for me to do this is when up/ or down is clicked.
i would say
textBox.lineup/linedown.
but I also need to know which component is clicked in order to do so.
Thanks in advance!
-Kevin
You can use ScrollChanged event in ScrollViewer as below
<ScrollViewer ScrollChanged="ScrollViewer_ScrollChanged">
In code you can get the verticalOffSet value.
private void ScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e)
{
double verticalOffSet = e.VerticalOffset;
...
}

Quirky Winforms Font Behavior On MouseHover

So I'm working on a basic subclass of Label that supports editing. The editing part works fine--I insert a text box with no background color or border on click, commit changes on enter or loss of focus.
The little thing that's giving me trouble is related to some basic font styling. The label is to underline with the MouseHover event (like a hyperlink) and then lose the underline afterwards. Most of the time, it works, but occasionally, the MouseHover will cause the font to revert to the Winforms default--8pt sans-serif--instead of performing the operation.
Here's the event handler:
void BWEditableLabel_MouseHover(object sender, EventArgs e)
{
_fontBeforeHover = Font;
Font hoverFont = new Font(
_fontBeforeHover.FontFamily.Name,
_fontBeforeHover.Size,
_fontBeforeHover.Style | FontStyle.Underline
);
Font = hoverFont;
}
Some of you may observe that the last line doesn't simply say:
Font = new Font(Font, Font.Style | FontStyle.Underline)
I tried that, and the problem came about. The current version before you was an attempt that I made to resolve the issue.
What if you use the MouseEnter and MouseLeave events? MouseEnter sets it to underlined and MouseLeave reverts it.
I think I solved it, though it feels like a bit of a patch instead of the cleanest solution. I did away with _fontBeforeHover and created _originalFont. I then overrode the Font property of the label and in the setter, set _originalFont to whatever the label is being set to. Then, in my MouseHover and MouseLeave events, I used a new method, SetFont() to change the font. Within SetFont(), I assign base.Font instead of using the overridden property. If I used the overridden property, I'd always be reassigning _originalFont to whatever I change the label's font to during the events.
Sure would be nice if I didn't need all that extra code, though :-)
I'm definitely open to more suggestions.
Sorry for the double answer, but maybe you might want to consider drawing the text yourself via DrawString in the paint event, that way you're not setting the Font property. The Font property gets it's value from the Parent's font unless explicitly set.
On your user control create a mousehover Event for your control like this, (or other event type) like this
private void picBoxThumb_MouseHover(object sender, EventArgs e)
{
// Call Parent OnMouseHover Event
OnMouseHover(EventArgs.Empty);
}
On your WinFrom which hosts the UserControl have this for the UserControl to Handle the MouseOver
this.thumbImage1.MouseHover += new System.EventHandler(this.ThumbnailMouseHover);
Which calls this method on your WinForm
private void ThumbnailMouseHover(object sender, EventArgs e)
{
ThumbImage thumb = (ThumbImage) sender;
}
Where ThumbUmage is the type of usercontrol

Categories

Resources