I have a form with a split panel. In the one split are a group of buttons which I want to programmatically change the color of the last pressed button. The following loop seems to run correctly and set the colors correctly but the form doesn't represent that. Once the loop is completed and I recheck the button colors, they revert to previous state.
For Each formControl As Control In Me.FormSplitContainer.Panel1.Controls
If formControl.GetType() Is GetType(Button) Then
If CType(sender, Button) Is CType(formControl, Button) Then
CType(sender, Button).BackColor = Color.White
Else
CType(sender, Button).BackColor = System.Drawing.SystemColors.ControlDark
End If
End If
Next
I can get the desired effect by doing the below code but seems less elegant and would obviously require updates as buttons would be added or removed.
DataFeedButton.BackColor = System.Drawing.SystemColors.ControlDark
IncentiveButton.BackColor = System.Drawing.SystemColors.ControlDark
CType(sender, Button).BackColor = Color.White
Anyone see what I am missing?
Assign sender to a button variable, then assign the color.
dim b as button
And then in the loop, assign it this way:
b = sender
b.backcolor = color.white
Where are you calling this code? Each time they click a button? To me, it looks like it sets the backcolor of all the form's buttons to white. I don't see where you're testing for the 'last button pressed' condition.
Related
I am trying to make it so if you hover into a panel, it will change its color, and when you hover into a label, the panel will not change its color or go back to default, let me show pictures of what I am trying to explain:
If you didn't hover into anything, it would be at its default state:
If you hovered into it, panel color will change:
Then, if you hover into the label, panel color shouldn't change:
But instead, what it does is if you hover into the label, the panel color goes back to what its default was. This is my code:
Private Sub Panel13_MouseEnter(sender As Object, e As EventArgs) Handles Panel13.MouseEnter
Panel13.BackColor = Color.DarkGreen
End Sub
Private Sub Panel13_MouseLeave(sender As Object, e As EventArgs) Handles Panel13.MouseLeave
Panel13.BackColor = Color.LimeGreen
End Sub
How can I fix this problem? Is there any way to make the code clean or do I have to copy and paste the code and replace words?
You may use the GetChildAtPoint method to check if the mouse cursor is over a child control.
Replace the code in your MouseLeave event handler with the following:
Dim childControl = Panel13.GetChildAtPoint(Panel13.PointToClient(Cursor.Position))
If childControl Is Nothing Then
Panel13.BackColor = Color.LimeGreen
End If
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.
So I made a script for a pool/snooker business and i have a page with the buttons which represents the tables. The buttons are as default green and when I press on them I want the to turn red and when pressed again to return to the default colour. If you can help me i will be grateful. Thank you very much!
If WinForms then, assuming you have set button background to Green by default then:
Private Sub MyButton_Click(sender As Object, e As EventArgs) Handles MyButton.Click
If MyButton.BackColor = Color.Green Then
MyButton.BackColor = Color.Red
Else
MyButton.BackColor = Color.Green
End If
End Sub
If not using WinForms I suspect a similar approach would work - i.e. check colour first then toggle colour based on result of that check.
I am working on windows form application.i given my text box back color is green.then i changed the textbox read only property true.but while running the back color is not appearing,
i have tried a lot to find this,but no answer ..
if any help is very appriciable.
How can I change the backcolor of the text, when the textbox is made readonly
Thanks
Add
If textBox1.ReadOnly
Then TextBox1.BackColor = Color.Blue
End If
to the event handler of the desired event.
If you want to change the back color when the form is loaded just use
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If textBox1.ReadOnly
Then TextBox1.BackColor = Color.Blue
End If
End Sub
If you want to change the color after a button is pressed or any other event is fired just implement the event handler and change the color there.
Go to code view and on the load function type in
TextBox1.BackColor = Color.Red
Obviously where TextBox1 is you'll enter the designer name for the textbox.
I have a ListView on a Windows Form in VB 2010.
I have set the MultiSelect property of the ListView to False so that only one item can be selected at any time.
I have configured a context menu for the ListView and it shows up correctly when the ListView is right clicked.
[Added a ContextMenuStrip control in the Designer and set the ContextMenuStrip property of the ListView to this.]
Consider these 2 scenarios:
A user right clicks on an item that is already selected in the ListView. Then the context menu is displayed and there are no issues.
A user right clicks on an item other than the item that is already selected in the ListView, Then before the context menu is displayed, the item that the user right clicked is selected.
In scenario 2, I need to stop the item that the user right clicks on from being selected automatically. Need to context menu to be displayed but the item that was previously selected should remain selected.
How can I achieve this?
I noticed that on the ListView's MouseDown event, the SelectedItems.Item(0).Index property is still at the old index. However, on the MouseUp event, this property changes to the new index.
In the MouseDown event handler, or anywhere else, how can I stop the SelectedItems from changing? Or how can I change it back to the previous selected item (without the user noticing it is being changed and then changed back)?
I can catch a right click on the MouseDown or MouseUp using the code below. However, I am not sure what I need to put inside this condition to stop the SelectedItems from changing.
If e.Button = Windows.Forms.MouseButtons.Right Then
...
End If
Note: I am able to use the following code for this. However, when I use this with scenario (2), it selects the item that the user right clicked on and then changes it back to the previous item and this change back is seen by the user. Therefore this solution cannot be used.
Dim intPrevSelectedIndex As Integer = -1
Dim boolCancel As Boolean = False
Private Sub ListView1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDown
If ListView1.SelectedItems.Count > 0 AndAlso e.Button = Windows.Forms.MouseButtons.Right Then
boolCancel = True
intPrevSelectedIndex = ListView1.SelectedItems(0).Index
End If
End Sub
Private Sub ListView1_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
If boolCancel Then
lstWalkResults.Items(intPrevSelectedIndex).Selected = True
boolCancel = False
End If
End Sub
Please let me know any solutions you might have. Thanks for your time!
In the code behind you should be able to handle the right click event. In that method you would display the context menu manually and then ignore the click event preventing the item from being selected.
If e.Button = Windows.Forms.MouseButtons.Right Then
//display context menu because you're handling the click event manually.
...context menu code...
Dim ee As New System.Windows.Forms.MouseEventArgs(Forms.MouseButtons.None, e.Clicks, e.X, e.Y, e.Delta)
e = ee
End If