change the backcolor of a cell in tablelayoutpanel by clicking the button inside it - vb.net

I have a tableLayoutPanel in a form. There are 10 rows and 10 columns in tableLayoutPanel. Each cell contains a button. So there are 100 buttons. When a user clicks on any of the button I want to change the backcolor of that. Particular cell. How can I achieve this?

TableLayoutPanel's cell does not have a property to control background color. Instead, you can put a panel on it, which would in turn host your button. Assuming your buttons are dynamically created and a generic handler is attached to every one of them, the following code will change back color of the panel when the button it hosts is clicked:
Private Sub Button_Click(sender As System.Object, e As System.EventArgs)
CType(sender, Control).Parent.BackColor = Color.Black
End Sub

Related

ListBox past edge of Panel

Here's photo of the form showing the listbox stopping at the edge of the panel when it should extend up past it. I have a project with several panels and a datagridview. I also have some textboxes and buttons. when I click on one of the textboxes, I have a listbox set to be shown and and rezise and locate to just above the textbox but the size I want it to expand to goes beyond the panel and datagrid and that part is not visible.
I've set it to "bring to front" but that doesn't solve the problem
Private Sub txtPayee_Click(sender As Object, e As EventArgs) Handles txtPayee.Click
ListPayee.Show()
ListPayee.BringToFront()
ListPayee.Left = txtPayee.Left
ListPayee.Height = 200
ListPayee.Width = txtPayee.Width
ListPayee.Top = txtPayee.Top - ListPayee.Height
End Sub

Get panel from button inside and change TableLayoutPanel cell color

I have a TableLayoutPanel, panel in each cell, 2 RadioButton in each panel. When I checked the RadioButton, the cell color will change. I know I can do it with assign CheckedChanged event to each RadioButton and hardcode the cell row and column to change the color. I have 15 panels, so will have 30 different CheckedChanged event.
Is there any way that I can use the sender(RadioButton) to get its panel? So that I can use GetCellPosition(panel) to get the cell and use the panel to get which RadioButton in it is checked. Then I can just assign this event to all RadioButtons.
That can be achieved by using the Control.Parent property.
Your code should look something like this:
Private Sub RBs_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged,
RadioButton2.CheckedChanged,
' ...etc.
Dim rb = DirectCast(sender, RadioButton)
Dim pnl = DirectCast(rb.Parent, Panel)
' TODO: Do something with pnl and/or rb.
Console.WriteLine(pnl.Name)
End Sub
Do note, however, that selecting a certain RadioButton will trigger the CheckedChanged event for two RBs; the one that got checked and the one the got unchecked. So, you might want to wrap your code inside If rb.Checked Then ... End If or whatever is appropriate.

Set cursor position on TextBox when Label is clicked

I have this custom control which acts as TextBox with background image. This control consists of UserControl with a background image, a TextBox placed on top of it and a Label as a child control for TextBox.
Labels text is always equal to TextBoxes text. So when you click on that label it focuses the TextbBox.
Below is a MouseClick event for this control:
Label_FolderNameBox.Text = TextBox_Name.Text
Private Sub Label_FolderNameBox_MouseClick(ByVal sender As Object, ByVal e As EventArgs) Handles Label_FolderNameBox.MouseClick
BackgroundImage = My.Resources.box_large_focus
Label_FolderNameBox.Hide()
TextBox_Name.Focus()
TextBox_Name.Select(TextBox_Name.Text.Length, 0)
End Sub
What I want to achieve:
I want to focus the IBeam cursor in the TextBox_Name in the same position where it was clicked on the Label_FolderNameBox.
As you can see with this line of code TextBox_Name.Select(TextBox_Name.Text.Length, 0) it always puts the cursor at the very end of the text.

How to stop User resizing DatagridView

I have a DataGridView control docked in Panel1 of a SplitContainer (docked into the form) which has Panel2 Fixed.
I'm trying to preview User resizing of the DataGridView Control. The two Events raised when the DataGridView border is dragged are Resize and ClientSizeChanged.
There isn't an e.Handled property or similar that I can cancel out the action, there doesn't seem to be any Property associated with the Control EventArgs that stops User resizing the DataGridView Control.
I only want to allow resizing of both controls when the Form is resized.
Any ideas?
If I understand correctly the answer is very simple: don't use a SplitContainer.
You can use:
a Panel docked to Right or Left
a DataGridView to Fill the empty space
If you want more 'control' over Panel.Size you can use Resize event of your Form.
Private Sub yourForm_Resize(sender As Object, e As System.EventArgs) Handles Me.Resize
Me.Panel.Width = Me.Width * 0.2
End Sub

How to change selected menustrip item color?

I have changed the menu strip item to from the default color to a new color. However, when selected, it goes to the default color. What property do I have to change so that I can seem a DimGray background when I click the tool strip item?
I've tried the click instance of the item to change the BackColor to Color.Dimgray, but that didn't work. Any suggestions?
I had the same problem - when the user selects the ToolStripMenuItem, the dropdown menu is then shown, the MenuItem has changed back to the original colour.
This can be solved by creating a sub which handles the dropdown opened event for the MenuItem by changing the item's forecolor and/or backcolor, for example:
Private Sub ToolStripMenuItem2_DropDownOpened(sender As Object, e As EventArgs) Handles ToolStripMenuItem2.DropDownOpened
ToolStripMenuItem2.ForeColor = Color.Black
End Sub