Retrieving .Text of Textbox located on Panel during DataRepeater_DrawItem event - vb.net-2010

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.

Related

How do you show a panel in VB dot net, if all the panels are created dynamically and sit in the exact same spot?

I am using Visual Studio 2022 and my language of choice is VB. When the program starts it creates Multiple Panels that are located in the same position created from data from a database.
No matter what I do, I can not get any of the panels to show. I can search for the panels and verify the panels exist, I can search the panels and verify that there are buttons in the panels. However I can not get each individual panel to show.
I am going to list all the panels that are created in the order they are created and they are all in the exact same position and size.
panel2
pnlDeptItems01
pnlDeptItems11
pnlDeptItems22
pnlDeptItems23
pnlDeptItems24
pnlDeptItems25
pnlDeptItems26
pnlDeptItems27
pnlDeptItems28
pnlDeptItems29
pnlDeptItems32
pnlDeptItems54
pnlDeptItems82
pnlDeptItems83
pnlDeptItems84
pnlDeptItems85
pnlDeptItems96
pnlDeptItems97
pnlDeptItems98
pnlDeptItems99
Visible and Enabled are set to true for all panels on creation.
Below is the code that I use to search for the panel by name and if I find it then try to make it visible which it already is and then I try to bring it to front. And it does absolutely nothing but keeps the 1st panel visible.
For Each control In Me.Controls.OfType(Of Panel)
If control.Name = btnPanelName Then
control.Controls.Add(oButton)
control.Visible = True
control.BringToFront()
Console.WriteLine(control.Name & " " & oButton.Name)
End If
Next
CODE TO CREATE PANELS
Private Sub CreateNewPanel(ByRef pnlName As String, ByRef pnltopx As Integer, ByRef pnlLefty As Integer, ByRef pnlSizex As Integer, ByRef pnlSizey As Integer)
Dim iPanel As Panel
iPanel = New Panel
iPanel.Name = pnlName
iPanel.Enabled = True
iPanel.Visible = False
iPanel.Top = pnltopx
iPanel.Left = pnlLefty
iPanel.Size = New Size(pnlSizex, pnlSizey)
iPanel.AutoScroll = True
Me.Controls.Add(iPanel)
If iPanel.Name = "pnlDepartments" Or iPanel.Name = "pnlDeptItems26" Then
iPanel.Visible = True
iPanel.BringToFront()
End If
End Sub
I then tried doing this and this does not work either:
For Each control In Me.Controls.OfType(Of Panel)
If control.Name = btnPanelName Then
control.Controls.Add(oButton)
End If
If control.Name = "pnlDeptItems26" Then
control.Visible = True
End If
Next
Code that is called to create the panels
CreateNewPanel("pnlDeptItems" & dataDeptId, 608, 5, 429, 479)
dataDeptId is pulled from a database in a loop.
Can someone explain to me why this is not working or what the work around is? I know it has something to do with all the panels that are created being created in the exact same position.
Thanks,
Shawn Mulligan
I found the solution. After reading my post I took notice that for some reason that my paneltopx and my panellefty were reversed and too large for some reason as well as my panelsizey and panelsizex.
I made the adjustments and made sure that all panels visibility was set to false, and now when I set visibility to true, the correct panel displays with the buttons that I created.
Thanks,
Shawn Mulligan

vb.net controls set property

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

Remove previous selection highlighting in RichTextBox without scrolling

I have a form with a RichTextBox (RTB) and a listBox.
When the end user selects an item in the listbox, any matched text in the RTB is highlighted (full code removed for brevity).
re = New Regex(txtToFind)
For Each m In re.Matches(rtbMain.Text)
rtbMain.[Select](m.Index, m.Length)
rtbMain.SelectionColor = Color.White
rtbMain.SelectionBackColor = System.Drawing.SystemColors.Highlight
Next
When the user left mouse clicks in the RTB I want the previously highlighted text to be cleared. This is the standard windows behaviour - If you manually select some text in an RTB with the mouse, it is highlighted, click anywhere back in the RTB and the highlighting disappears. My programatically selected text remains selected.
I have some partially working code (below). I can clear all the highlighted text, but it is by process of selecting everything, changing the colour back and then deselecting it again. I know it is not efficient, the RTB flickers and I am sure it is not the correct way to do it. Can I emulate the standard windows behaviour?
Also using my code, it scrolls to the first line when entering the RTB a second time.
I get around this the first time by returning the top visible line index before clearing the text and then selecting that line again afterwards and using ScrollToCaret(). This only works on the first pass. Subsequent MouseDown events select the top row regardless of where the user has clicked so nothing can be manually highlighted in the RTB.
Private Sub rtbMain_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles rtbMain.MouseDown
Dim topIndex As Integer = rtbMain.GetCharIndexFromPosition(New System.Drawing.Point(1, 1))
Dim topLine As Integer = rtbMain.GetLineFromCharIndex(topIndex)
If e.Button = Windows.Forms.MouseButtons.Right Then
'Do nothing (Context Menu)
Else
rtbMain.SelectAll()
rtbMain.SelectionColor = Color.Black
rtbMain.SelectionBackColor = Color.White
rtbMain.DeselectAll()
rtbMain.Select(topIndex, 0)
rtbMain.ScrollToCaret()
End If
End Sub
I need my code to emulate the standard windows behaviour - clear selected text highlighting on MouseDown and leave the mouse cursor where the user has clicked.
Any help anyone can offer is gratefully appreciated.
I think you may be overthinking this one.
In the right click event, try RtbMain.SelectionLength = 0

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

VB.NET ComboBox - Need to force a redraw when a key is pressed when it is dropped down

I am using a DrawItem and MeasureItem events to paint a combobox with a DrawMode of OwnerDrawVariable.
Basically, I'm trying to have the user highlight a selection with the mouse, and then press the space bar to toggle the Save status of a song list. Then I call the Me.Refresh() event for the form in an attempt to redraw the form and the ComboBox.
The problem that I am running into is that only the Combobox itself (not the drop-down area) that is a control on the main form is redrawing, and the text that is behind the mouse-highlighted selection of the drop-down list is not changing from Red to Black as I believe it should. If I move the mouse to another selection, then the color does in fact update.
Here is a snippet of the code.
If (e.KeyCode = Keys.Space) Then
If cmbList.SelectedItem IsNot Nothing Then
With DirectCast(cmbList.SelectedItem, SongTitle)
.bSave = Not .bSave
End With
End If
End If
e.Handled = True
Me.Refresh()
Thanks for any help you can provide.
You need to use .RefreshItem/.RefreshItems instead of .Refresh.
See this question: Dynamically changing the text of items in a Winforms ComboBox