vb.net controls set property - vb.net

can you help me to solve my problem on vb.net
i want to change background color of one or more datagridview using check all component on form using controls..
For Each ctrl In ctrlParent.Controls
If ctrl.GetType Is GetType(DataGridView) Then
....
End If
Next
if i use ctrl.BackColor = Color.Black, that no make any change. but if i use ctrl.Backgroundcolor = Color.Black shows error : 'Backgroundcolor' is not a member of 'System.Windows.Forms.Control'
any idea?
can i access control preperty to change 'Backgroundcolor' property of datagridview?
or any else?
note : ctrl.ForeColor = Color.Black -> successfully change font color on datagridview

You can filter the type of controls directly to DataGridView in your loop.
Then set BackgroundColor accordingly:
For Each ctrl In ctrlParent.Controls.OfType(Of DataGridView)
ctrl.BackgroundColor = Color.Black
Next

Related

Retrieving .Text of Textbox located on Panel during DataRepeater_DrawItem event

I'm using the DrawItem event of a DataRepeater to change the .BackColor and .ForeColor of a textbox based on the .Text contents. This works just fine UNLESS the textbox is located on a panel. If the textbox is on a panel, then I am returning this exception: "Object reference not set to an instance of an object." It indicates that "e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).Text" doesn't have a value. Stepping through the code confirms it.
BUT... if I just slide that textbox off the panel, then it works just fine. My Google-Fu has failed me. What am I missing?
Additional Info:
Visual Studio 2010 Professional, VB.Net targeting.NET 4.0
'Set Record Status Color
Select Case e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).Text
Case "Working"
e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).BackColor = Color.Green
e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).ForeColor = Color.White
Case "Sleep"
e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).BackColor = Color.Red
e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).ForeColor = Color.White
Case Else
e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).BackColor = Color.White
e.DataRepeaterItem.Controls(txt_AWQRecordStatus.Name).ForeColor = Color.Black
End Select
Answer to my own question in case this puzzles someone else in the future:
When a control is located on a panel, it is nested inside of it from a code perspective. So you have to reference the panel control, and then the control you are actually trying to change. So I had to chase my statements to read like this:
e.DataRepeaterItem.Controls(panel_RecordDetails.Name).Controls(txt_AWQRecordStatus.Name).ForeColor = Color.Black
Where panel_RecordDetails is the panel, and txt_AWQRecordStatus is the textbox on that panel. There may be an easier way to do this, but I'm going to take the win and move on.

How to terminate the last action when a new label is clicked?

I have many labels. When a label is clicked, I change the BackColor to aqua. When I click on another label, both of them are aqua, but I want the color of the first label to go back to normal. It there a way to do that?
Here is my code:
Dim clickedLabel = TryCast(sender, Label)
If clickedLabel IsNot Nothing Then
clickedLabel.BackColor = Color.Aqua
TextBox1.Text = clickedLabel.Text
Else
End If
Put them all in a collection so that you can apply the default-color on all others or - if they are all in the same container-control like a Panel - use this code:
For Each lbl In LabelPanel.Controls.OfType(Of Label)()
clickedLabel.BackColor = If(lbl Is clickedLabel, Color.Aqua, DefaultColor)
Next
TextBox1.Text = clickedLabel.Text
Instead of LabelPanel.Controls you could also use Me.Controls, but then all labels on the form are used even if it's not related. Labels that are in other container controls won't be find in this way anyway, so no recursive search.
DefaultColor is a System.Drawing.Color that you store as class/member variable(shared or as instance-variable).

Changing the text of all the buttons on a form in VB.net

Call me crazy but for the life of me I cannot make this work. I have the following code:
Dim cControl As Control
For Each cControl In Me.Controls
If (TypeOf cControl Is Button) Then
cControl.ForeColor = Color.Black
cControl.Font = New Font(cControl.Font, FontStyle.Regular)
End If
Next cControl
Me.ActiveControl.ForeColor = Color.Blue
Me.ActiveControl.Font = New Font(Me.ActiveControl.Font, FontStyle.Bold)
I am trying to make the font black and regular for all of the buttons on the form (there are a lot) and the button just clicked, bolded and blue.
The second part of the code works (making the font bold and blue), it's the first that is not simply working.
What am I missing?
I've never tried it like that before, but I have used Linq to accomplish what you want.
Dim btn() As Button
btn = Me.Controls.OfType(Of Button)().Where(Function(c) c.Name.Contains("")).ToArray()
This will basically create an array of buttons from your form, then you can just loop through each one.
Dim i As Integer = 0
While i < btn.Count
btn(i).Enabled = True
btnText(i).BackColor = Color.DarkOliveGreen
btnText(i).ForeColor = Color.White
i += 1
End While
If you have a naming convention for your buttons and want to only change certain buttons where the empty quotes are you can have "btnTest".
Then you will have an index of any button that contains the text "btnTest" in it. So that would include "btnTest1", "btnTest2", "btnTestAnything1234" etc.
Hope this helps and you can use it!
Nevermind I found my problem. The buttons are in a group box so I had to reference the group box directly

Reset background color of textbox

I'm using VS2012 with VB.NET on a winforms application. I set the BackColor property of some textboxes programmatically during my code depending on form validation. This works fine, the problem is that I'd like to "reset" the BackColor property of the textbox, so that the textbox performs as if it were in the same state before I set the BackColor. So it would do the following:
Return to the default color of white immediately after "reset"
Change to that "light gray" color when the textbox.enabled = false
The reason why I cannot simply set the BackColor to Color.White, is that this affects the textbox when textbox.enabled = false. The textbox does not return that "light gray" color after setting the backcolor and disabling the textbox. I need it to return to that color, and I'd rather not have to set the textbox's color everytime I enable or disable the textbox. Thanks!
Simply:
TextBox1.BackColor = SystemColors.Window
You reset the color by re-assigning the original value of BackColor. Or by assigning the default value, it isn't white:
textBox1.BackColor = Color.FromKnownColor(KnownColor.Window);
textBox1.BackColor = SystemColors.Control;
This is an old post, but when I used SystemColors.Window it set it to white, not the default grey.

How to change the datagridView Header color

Now the datagridView Header Background color is showing in Gray. I want to change to differenct
color.
I Changed the background color in ColumnHeaderDefaultCellStyle, but nothing changed.
How to do this.
Set the property EnableHeadersVisualStyles to False, then change the ColumnHeaderDefaultCellStyle background color to the color that you desire. You will be able to see the changes in the designer itself.
Also, if you are trying to set the color (back or fore)color or other properties of the individual column's header (not all at once) use
datagridview.Columns(e.ColumnIndex).HeaderCell.Style.BackColor = color.cyan
datagridview.Columns(e.ColumnIndex).HeaderCell.Style.(ForeColor or Font or Alignment etc) = whatever
where e.ColumnIndex was taken from the EventArgs of your Event, but you can alter accordingly.
In datagridView you can change the Header color by using DataGridViewCellStyle, see the following code
' Set the selection background color for all the cells.
dataGridView1.DefaultCellStyle.SelectionBackColor = Color.White
dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black
' Set RowHeadersDefaultCellStyle.SelectionBackColor so that its default
' value won't override DataGridView.DefaultCellStyle.SelectionBackColor.
dataGridView1.RowHeadersDefaultCellStyle.SelectionBackColor = Color.Empty
' Set the background color for all rows and for alternating rows.
' The value for alternating rows overrides the value for all rows.
dataGridView1.RowsDefaultCellStyle.BackColor = Color.LightGray
dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.DarkGray
' Set the row and column header styles.
dataGridView1.ColumnHeadersDefaultCellStyle.ForeColor = Color.White
dataGridView1.ColumnHeadersDefaultCellStyle.BackColor = Color.Black
dataGridView1.RowHeadersDefaultCellStyle.BackColor = Color.Black
EDIT:
Using the DataGridViewCellStyle, your header color will changes but a seperator for columns in the header section will not appear. So, heres a overrided event of OnPaint Event Handler have a look at this