I've been looking around everywhere but couldn't find any solutions.
My problem is that when pressing TAB (to move around the form focusing on different controls), the buttons get an ugly inset border that I do not want, there's no option to disable it either.
Note that these inset borders only show on the recently selected/clicked button, not on all at once, it displays on whatever button is focused/selected, here are a few examples:
How I want the button to look (and how it looks before pressing TAB)
How the button looks after pressing TAB
And that's what all buttons in the form look like when focused until the form is reopened
Any tips? Thanks in advance for help!
Have the ShowFocusCues return false, although that can be annoying for users who use tabs to navigate.
public class MyButton : Button {
protected override bool ShowFocusCues {
get {
return false;
}
}
}
Related
I have created an Outlook add in which at some point displays a windows form with four buttons present on it. I am trying to default the focus to the first button, however the visual "selected" border will not appear around the button whenever I default this button as the focused one on start.
Any ideas how I could achieve this?
You can use either of these options to set the focus on a control in Load event of the form:
this.ActiveControl = this.button1;
this.button1.Select();
this.Show(); this.button1.Focus();.
You can use the Control.Focus method in the Load event of the form to set the focus on a control only after the Visible property of the form is set to true.
After selection the button, the border of the button will be drawn in a way that shows it's the active control, but the focus cues will not be drawn.
As a quick and dirty fix, you can send a Tab, and a Shift + Tab to your form:
SendKeys.SendWait("{TAB}");
SendKeys.SendWait("+{TAB}");
If you are interested to change the standard behavior of Button to see focus cues when you select button in code or using mouse, you can create your own button inheriting Button and override its ShowFocusCues to return Focused value. You can read more about it here:
public class MyCustomButton : Button
{
protected override bool ShowFocusCues
{
get { return this.Focused; }
}
}
This question already has answers here:
How can I disable a tab inside a TabControl?
(26 answers)
Closed 7 years ago.
I have a tab control with couple of tabs on it, I want to disable one of the tabs while checking a radio button on the other tab page. I can disable the controls on it by using tab.Enabled = False; But I want to know how I am able to disable the whole page from even clicking on the tab. The way I did I can select the tab still, just the controls on it are disable.
There's no native way to disable just one tab and leave the rest working. You'll need to make your own TabControl and use the Selecting event to detect when someone is about to enter your tab and disable it.
Here's a really basic example that stops you from entering tabindex 1.
// Make your own control deriving from TabControl
class CustomTabControl : TabControl
{
// Subscribe to the Selecting event
public CustomTabControl()
{
Selecting += tabSelecting;
}
private void tabSelecting(object sender, TabControlCancelEventArgs e)
{
// You would put your own logic here to detect which tabs to disable
// For this example I'm disabling access to tab in index 1
if (e.TabPageIndex == 1)
{
e.Cancel = true;
}
}
}
If you want the tab to grey out you'll need to override the DrawItem even and paint the tab yourself. If I have the code for that I'll update my answer in a bit.
Edit: It seems the example for custom drawing is pretty long, but here is the tutorial I used to create my own tabs.
http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition
I am making a Windows Form Application.
I have two buttons (Accept and Cancel) and I want to call the click event for one when Enter is pressed, and the click event for the other when Escape is pressed.
This is a code I've tried (I found it here on a similar question) that didn't work.
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode.Equals(Keys.Enter)) {
this.btnOrder.PerformClick();
}
if (e.KeyCode.Equals(Keys.Escape))
{
this.btnCancel.PerformClick();
}
}
I have looked through all the properties and events of the buttons but I cannot find anything Accept or Cancel related. I know that this is probably way too easy but I've learnt of these buttons tonight and I just can't do them.
Can someone tell me what am I doing wrong?
It's a property of the Form:
AcceptButton
CancelButton
In the Form Designer: You add the buttons to the form, select the form, and select the buttons you want as accept and/or cancel button.
Developing in Monogame I have created some simple way to detect that a sprite has been clicked. This is how I check that the sprite was pressed. mouseOver means the mouse is intersecting the sprite.
var lastMouseState = mouseState;
mouseState = Mouse.GetState();
if (mouseOver && lastMouseState.LeftButton == ButtonState.Pressed && mouseState.LeftButton == ButtonState.Released)
onClick(this);
onclick is managed in another class. This class is called Menu
private void onClick(Button btn)
{
//Toggles the button.
if (btn.HasCheckBox)//Whether the button has a checkbox
{
btn.Checked = !btn.Checked;
}
if (btn.Destination == "inGame")
unPause();
else if (btn.Destination == "Quit")
Exit();
else
openMenu(btn.Destination);
}
A little explanation here: Each button has a property called destination, meaning where the button leads to, such as: inGame, Options, Quit, etc.
Now here is my problem:
When the game is paused, the menu is shown containing 3 buttons (continue, options, and quit).
When the menu is opened and a submenu is entered, then a button there is pressed, after that, the menu is closed and the mouse has not been moved, then when I reopen the menu and the mouse is on top of the Quit button, the game will close. Check the gif below for a much clearer explanation.
This only happens with Quit. When I try the same procedure with the other buttons, it does not work. {SEE EDIT 2}
A small demonstration:
http://media.giphy.com/media/AxVvk9FgO6TOd5roEo/giphy.gif
I try it first with the music button. close and reopen menu -- nothing happens.
I try it with the fullscreen button, click > close > reopen menu -- game quits.
Why is a button click registered at quit, but not at options?
Thanks for the help. Comment if additional information is needed.
EDIT
Apparently, it does not matter where the mouse is when entering the options menu. As long as a click is done (anywhere in the screen) and after the menuscreen is reopened the mouse is hovering over the button, the game will quit.
Strangely enough, I added a new button, under Quit which also leads to the options menu. After testing the same behaviors with the 2nd Option button. I could not reproduce the error. However, If I do the same with the 1st Option button while hovering the second one, it works same as with Quit -- it will instantly open the option screen as if it was clicked.
Updated Gif: http://media.giphy.com/media/AxVvk60pK8NBjmghDa/giphy.gif
EDIT 2
Narrowed the bug even further. When using foreach, the bug only appears when the index of the second button is greater than the first. Explanation: I was pressing 2nd button, button[1] and it was only working for button[2] and higher.
This is my update method:
if (isPaused)
{
foreach (var button in ButtonList)
{
if (MenuScreen == button.Parent)//Menuscreen is the current screen being displayed.
//button.parent is the parent screen that holds current button. such as: ingame, pause, etc.
{
button.Update();
}
}
}
Now I know the logic behind it, but not where it happens.
Sounds like you could have some unseen characters mucking up the works, maybe from a copy paste or something. If you added another similar button and it worked, that could be a possibility.
You could re-type it by hand manually or run it through something like jsbin to see if anything shows. (In jsbin it shows red periods for unseen characters.) Just an idea to try if all else fails you.
Apparently, the bug was happening because the buttons that were not visible were not being updated, which meant that their respective mouse positions were not updated -- which still doesn't explain why the index had to be greater.
Anyway, I added a conditional in the Update method of the button. In case this bool variable is true, update the clicks too. And if the buttons are not visible, or the variable is false, only the mouse states are being updated.
Solution:
if (isPaused)
{
foreach (var button in ButtonList)
{
if (MenuScreen == button.Parent)
{
button.Update(true);
}
else
button.Update(false);
}
}
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}");
}