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; }
}
}
Related
I'm currently making a simple C# game for a college assignment, wondering if it's possible to have radio or checkbox (or something similar) inputs upon loading, in an external window.
I'm currently using this to get a username:
public Form1()
{
InitializeComponent();
Initialize();
GlobalVar.Username = Microsoft.VisualBasic.Interaction.InputBox("Welcome to EasiGame, Please enter your username.", "Welcome", "Player1", -1, -1);
label4.Text = GlobalVar.Username;
}
This references VB, and that works great, however could I include radio buttons or something in this box, or a separate box, to grab user input for a difficulty setting.
Thanks in advance!
You would have implement your own form. Assuming you're using a recent version of Visual Studio:
Click Project > Add Windows Form.
Set the FormBorderStyle property of the Form control to FixedDialog to prevent
the user from resizing the form.
Set the MinimizeBox and MaximizeBox properties to false.
Set the StartPosition property to CenterScreen or CenterParent.
Add a button to the form with the text OK and set the DialogResult
property to OK.
Add a button to the form with the text Cancel and set the DialogResult
property to Cancel.
Change the AcceptButton property of the form to reference the OK button.
Change the CancelButton property of the form to reference the Cancel button.
Add a TextBox and a RadioButton or CheckBox control to the form.
Press the F7 key to open the code view for the form.
Implement a property of type string that returns the Text property of
the TextBox control.
You'll also need to come up with a way of getting the difficulty from the
form; I'll let you figure this part out for yourself :)
To show the form, create an instance and call the ShowDialog() method.
This will block the calling method until the user has clicked a button.
The ShowDialog method will return a DialogResult, which can be used to
determine which button was clicked.
I have a problem .. I have an error list form (works as validation summary screen) that displays validation of controls that require to save data but have no values.
This form opened when validation occurs on controls in another form that has tab control contains all controls that have validation.
The problem is when I double click on Error List form, I need cursor focus on tab control that have this control and focus on the control itself
The result : focus happened on tab control only .. but I need to focus on the control also
Use Control.Focus() in your tab selected event handler.
Call Focus() to focus on the next control.
Step 1 : You need to handle the Enter event of the TabPage Control to perform the operations when TabPage gains the focus.
Step 2: You can call Select() function on Required control to gain the Focus.
Try This: if you want to gain the Focus of TextBox control in TabPage2 use this code
tabPage2.Enter += new System.EventHandler(this.tabPage2_Enter);
private void tabPage2_Enter(object sender, EventArgs e)
{
textBox1.Select();
}
I think the trick is to set socus on the tab page first, then set focus on the actual control you want to focus on.
What I was seeing is if the tab page was already selected setting focus to the control works fine. However, if the tab was programmatically activated then setting focus on the control alone does not work.
So this works for me reliably:
// first select and focus the tab
TabsResult.SelectedTab = tabRequest;
TabsResult.SelectedTab.Focus();
// then focus the control
txtRequestUrl.Focus();
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}");
}
TextBoxes and NumericUpDowns have the odd property of not allowing you to deselect them once they are selected. When my user selects a NumericUpDown and clicks else-where on the form, the NumericUpDown should be deselected.
Unfortunately, this is not the case. Currently I am just handling the MouseDown event of all other controls on the form (like the panels and actual form itself) and just calling the Focus method of a random label to remove the focus from the NumericUpDown. However, this cannot be applied to menu items or scrollbars.
There must be a better way to do this. The user may want to scroll the panel instead of the NumericUpDown and intuitively click the Panel and then use the scroll-wheel, but currently that would scroll the NumericUpDown instead, since it still has focus.
Thanks for reading.
Edit: Problem still unsolved.
Normally Panel Control is a Non-Focusable control. Therefore clicking on Panel will NOT remove focus from TextBox or NumericUpDown Countrol.
The workaround can be, place a button on panel and move it away from view for example setting its x = -100 and y = -100. Do NOT set visible = false.
Now whenever user clicks on Panel (Panel_Click event) set focus (Button.Focus()) to that button. In this way panel will be scrollable through scroll-wheel.
Enclose the numeric box within a panel of some sort and then do
panel1.MouseHover += new EventHandler(panel1_MouseHover);
private void panel1_MouseHover(object sender, EventArgs e)
{
if (numericUpDown1.Focused)
{
panel1.Focus();
}
}
I tested it and it works.!
How to change dropdown button in a ComboBox control (C#, Windows Forms)? I have a custom button, and I want to use it in the ComboBox instead of the default dropdown button.
I think Hans Passant solution is the way...
From here:
http://social.msdn.microsoft.com/forums/en-US/winformsdesigner/thread/5d65f987-834c-465f-a944-622831d4cfb0
You can create a UserControl, drag a
ComboBox and a Button onto it, make
the Button right over the ComboBox's
arrow button to make the arrow button
invisible, handle the Button's Paint
event to draw an arrow on it, this can
be done by calling
ComboBoxRenderer.DrawDropDownButton()
method (Notice: this method has a
limit, it needs the visual style being
enabled on the OS) or by drawing an
icon on it, or just drawing a small
triangle on it.
Then handle the Click event of the button to show the ComboBox's
DropDown, this can be done by
something like this
private void button1_Click(object sender, EventArgs e)
{
this.comboBox1.DroppedDown = true;
}