How does groupbox.Controls[i] or panel.Controls[i] work? - c#

I am new to C# and was wondering how groupbox.Controls[i] and/oror panel.Controls[i] works?
I have a panel with a bunch of buttons in it to represent a keyboard. I change the color of the keyboard button when the key is pressed.
However, when I do keyboardPanel.Controls[2].BackColor = Color.Red;, my spacebar becomes red.
My spacebar is button55 and at TabIndex 54. Why is it my spacebar that turns red?

The reason that yoor space-bar gets red is simple: keyboardPanel.Controls[2] is the space-bar. please pay attention that the buttons are not necessarily added to the keyboardPanel.Controls list in the order they are named. Meaning: keyboardPanel.Controls[1] is not necessarily button1 and also button55 is not necessarily keyboardPanel.Controls[55].
Now, If you want to extract buttons by name you should use this:
keyboardPanel.Controls.Find("Button55" , true);
where "button55" is the name of that control and true goes for the option to search all the children.
But I think there is a simpler way to change the color. Using the Sender:
private void button_Click(object sender, EventArgs e)
{
Control btn = sender as Control;
btn.BackColor = Color.Red;
}
and make this method as the event handler of all the buttons' click event.
EDIT:
If you really want to re-arrange the controls in the GroupBox you should visit the the designer. The simple way to get to the designer is to right-click on InitializeComponent() in the constructor method of your form and choose Go To Definition.
There you will find the order that controls are added. something like below:
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button3);
this.groupBox1.Controls.Add(this.button1);
I my case Controls[0] is button2, Controls[1] is button3 and so on. You can re-arrange them like below:
this.groupBox1.Controls.Add(this.button1);
this.groupBox1.Controls.Add(this.button2);
this.groupBox1.Controls.Add(this.button3);

Related

How do I highlight the selected item (tab index location) in my windows form application?

I want to be able to highlight the selected item on my page because it's busy and there is a lot going on. The user will not have access to their mouse, only keyboard so currently they tab through buttons quickly and enter in to what they need to do (it's a fast data entry sort of app if you must know).
I want to be able to highlight the selected button (so when you tab through currently it will select a button but it isn't very noticeable, it just has a slight border around it when selected).
I know that you can use a focusEnter and focusLeave event, but I would like to avoid that if at all possible just because there are so many buttons on the page that I would have to have a ton of repetitive events with almost the same code.
You can and should use just two common event handlers for Enter and Leave events for all your buttons!
Use the sender param to access the buttons:
private void buttons_Leaveobject sender, EventArgs e)
{
((Button)sender).BackColor = SystemColors.Control;
((Button)sender).ForeColor = SystemColors.ControlText;
}
private void buttons_Enter((object sender, EventArgs e)
{
((Button)sender).ForeColor = SystemColors.Control;
((Button)sender).BackColor = SystemColors.ControlText;
}
Use your own ideas about how to highlight the focussed button; this is a bit excessive imo..:
Of course Button with FlatAppearance can do the highlighting all by themselves as they have separate Colors for their states.
I would suggest creating your own class derived from Button, and then handling the background painting yourself. That would allow you to play with the background look/color and/or the border effects.

How to hide control when click outside it?

I have a WindowForm and some controls on it.
My point is that when I click button "?" on top-right of the datagridview, it will show a picture box and when I click outside the pictureBox, it must invisible.
My MainForm
MyPictureBox
I have searched some topics on this site, but some dont work, some work partly. Like
this.
I also tried:
void pictureBox1_LostFocus(object sender, EventArgs e)
{
if (pictureBox1.Visible)
pictureBox1.Visible = false;
}
But when I click on button2, button3, ... The pictureBox wasn't invisible.
Any solution will be highly appreciated.
I think your pictureBox1 isn't losing focus, cause it never actually GOT focused. Set it to be focused after making it visible.
Oh, I have encountered this before...
I was making a Label that you could double click and it would allow you to edit the Label.Text, like a TextBox. However, I was having problems hooking into the events to know when the user had clicked off the Control and wished to stop editing. I tried Control.LostFocus, and Control.Leave, but nothing. I even got frustrated/desperate and tried some silly ones like Control.Invalidated.
What I ended up having to do was subscribe to the Click event of the Form/Container/Control behind it.
However, putting the responsibility of wiring up this event into the Form that wants to use it is poor design. What you can do, however is to make the constructor to Control class require a reference to the owner/parent/container as a parameter. That way, the requirements are not hidden, they must be satisfied before you can get a object instance, and the control can wired up to the Form.Click within itself, where that logic belongs.
private Form owner;
public EditLabel(Form Owner)
{
this.owner = Owner;
owner.Click += EndEditing;
}
Add this method in designer.cs:
pictureBoxEvent this.MouseLeave += new EventHandler(pictureBox_MouseLeave);
Add this code in cs file:
private void pictureBox_MouseLeave(object sender, EventArgs e)
{
pictureBox1.Visible = false;
}

Tab index is not working on radio buttons

This is the part of my form that I am asking about
This is the tab index:
The problem that the tab goes from Farmer Audi Status to Yes, then to Ownder Bank Name instead of going to No
please notice that the yes and no already have 0.1.6.0 and 0.1.6.1 respectively.
could you help me please?
Notice
both radio buttons has TabStop property to True
From How to: Set the Tab Order on Windows Forms (MSDN):
A radio button group has a single tab stop at run time. The selected button (that is, the button with its Checked property set to true) has its TabStop property automatically set to true, while the other buttons have their TabStop property set to false.
In other words, what you're seeing is normal. Those "Yes/No" radio buttons are in the same group, and you can't tab between radio buttons in the same group. As you tab, you'll only focus on the currently selected one, then move to the next control on the form (in your case, a TextBox).
To work around this, you could place each radio button in its own container (such as a Panel), which means you'd have two "groups" each with one radio button. But then you lose the built-in functionality that automatically deselects one radio button when you select the other. Your user will be able to select both radio buttons, so you'd need to add some logic that disables the other. If you decide to try that, experiment with the radio buttons' CheckedChanged or Click / MouseClick events.
As Steve said, and as stated in the answer he linked to, the way it works out-of-the-box is expected behavior for Windows, so think twice before overriding it unless you have a good reason for doing so.
It worked for me!
first you have to create a method like this:
private void TabStopChanged(object sender, EventArgs e)
{
((RadioButton)sender).TabStop = true;
}
and then, put this in your Form_Load event:
private void Form_Load(object sender, EventArgs e)
{
foreach (var item in this.Controls)
{
if (item.GetType() == typeof(RadioButton))
((RadioButton)item).TabStopChanged += new System.EventHandler(TabStopChanged);
}
}
For radio buttons, you don't have to use Tab to navigate. Just use right and left keys to traverse radio buttons.
Check out this link to read more - https://www.csun.edu/universal-design-center/web-accessibility-criteria-tab-order

How to change drop down button in combobox control?

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;
}

How do I programmatically wire up ToolStripButton events in C#?

I'm programmatically adding ToolStripButton items to a context menu.
That part is easy.
this.tsmiDelete.DropDownItems.Add("The text on the item.");
However, I also need to wire up the events so that when the user clicks the item something actually happens!
How do I do this? The method that handles the click also needs to receive some sort of id or object that relates to the particular ToolStripButton that the user clicked.
Couldn't you just subscribe to the Click event? Something like this:
ToolStripButton btn = new ToolStripButton("The text on the item.");
this.tsmiDelete.DropDownItems.Add(btn);
btn.Click += new EventHandler(OnBtnClicked);
And OnBtnClicked would be declared like this:
private void OnBtnClicked(object sender, EventArgs e)
{
ToolStripButton btn = sender as ToolStripButton;
// handle the button click
}
The sender should be the ToolStripButton, so you can cast it and do whatever you need to do with it.
Thanks for your help with that Andy. My only problem now is that the AutoSize is not working on the ToolStripButtons that I'm adding! They're all too narrow.
It's rather odd because it was working earlier.
Update: There's definitely something wrong with AutoSize for programmatically created ToolStripButtons. However, I found a solution:
Create the ToolStripButton.
Create a label control and set the font properties to match your button.
Set the text of the label to match your button.
Set the label to AutoSize.
Read the width of the label and use that to set the width of the ToolStripButton.
It's hacky, but it works.

Categories

Resources