Datagridviewcell Tooltip doesn't display after modifying image of any cell - winforms

In my winforms application, I loop through the cells of a datagridview and add a tooltip for specific cells based on values in other columns. I do this in the cellformatting event handler, and it worked perfectly. Here is the code:
Private Sub dgvResults_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvResults.CellFormatting
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "TradeInValue"
DirectCast(dgvResults.Rows(e.RowIndex).Cells("TradeInValue"), DataGridViewTextBoxCell).ToolTipText = "Min = " & CDec(dgvResults.Item("BB_Min", e.RowIndex).FormattedValue).ToString("$#####.##") & ", Max = " & CDec(dgvResults.Item("BB_Max", e.RowIndex).FormattedValue).ToString("$#####.##")
If Not IsNothing(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue) AndAlso dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString.Trim.Length > 0 AndAlso CInt(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue.ToString) <> -1 Then
If dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Length > 0 Then
Dim ValueParts() As String = dgvResults.Item("ValueList", e.RowIndex).FormattedValue.ToString.Split("|")
'Dim selectedTrim As String = ValueParts(dgvResults.Item("SelectedTrimIndex", e.RowIndex).FormattedValue)
End If
End If
End Select
End Sub
Then, I added code in the cellpainting event handler to hide specific images, again based on values in the datagridview. Here is that code.
Private Sub dgvResults_CellPainting(sender As Object, e As DataGridViewCellPaintingEventArgs) Handles dgvResults.CellPainting
If e.ColumnIndex >= 0 AndAlso e.RowIndex >= 0 Then
Select Case dgvResults.Columns(e.ColumnIndex).Name
Case "VIN_Pic"
If dgvResults.Rows(e.RowIndex).Cells("VIN_Value").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
Case "EmailDisplayImage"
If dgvResults.Rows(e.RowIndex).Cells("ListingContactEmail").FormattedValue = "" Then
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
End If
End Select
End If
End Sub
When this code as added, the tooltips no longer display. The CellToolTipTextNeeded event fires, and it shows the correct text in the argument, but it never displays. Comment out the lines that assign a new image to the datagridviewimagecells, and the tooltips start displaying again.
I hope this explanation was sufficient. Any ideas?

With the below code, you will run into memory and GDI object leaks very easily as a new Bitmap instance is created every time the cell is painted. Also this is causing your ToolTip to be not displayed.
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = New Bitmap(1, 1)
Define the empty bitmap in class level or add as embedded resource and use it.
Dim emptyBitmap As New Bitmap(1, 1);
DirectCast(dgvResults.Item(e.ColumnIndex, e.RowIndex), DataGridViewImageCell).Value = emptyBitmap

Related

how to group rows in datagridview on Query in vb.net

Still has my problem on how i group rows in datagridview after executing SELECT Statement. I just want to make my datagridview to display data like the image i uploaded, if you would mind to take a look at the image below;
enter image description here
I've also tried this code but it does not do the same like the image;
Private Sub dgvCurriculum_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvCurriculum.CellFormatting
If e.RowIndex > 0 And e.ColumnIndex = 0 Then
If dgvCurriculum.Item(0, e.RowIndex - 1).Value = e.Value Then
e.Value = ""
ElseIf e.RowIndex < dgvCurriculum.Rows.Count - 1 Then
dgvCurriculum.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.White
End If
End If
End Sub
I know this sounds tricky but, i couldn't find another way on how to do it. I'm just new in vb.net.

Use CheckBoxList in DevExpress vb.net Assign & Evaluate?

Hello my code example is :
/*
bdEmpresa (BindingSource)
Contains:
selet idEmpresa,Nombre,Acceso from Empresa
Result
1, Empresa1,true
2, Empresa2,false
3, Empresa2,true
*/
clEmpresas.DataSource = bdEmpresa
clEmpresas.DisplayMember = ? (Nombre)
clEmpresas.ValueMember = ? (Acceso)
This is the code we would use
With clEmpresas.Properties
If .DataSource IsNot Nothing Then .DataSource = Nothing
.DataSource = bdEmpresa
If .DataSource.rows.count = 0 Then Return Nothing 'no data to load
'get the names from the dataset, don't expect them to be specific names
.ValueMember = .DataSource.rows(0).Table.Columns(0).ColumnName
.DisplayMember = .DataSource.rows(0).Table.Columns(1).ColumnName
'Set the selected items, comma separated list of ids
If selectedIds <> "" Then
For i As Integer = 0 To .ItemCount - 1
If Array.IndexOf(selectedIds.Split(","), .GetItemValue(i).ToString) >= 0 Then
.SetItemChecked(i, True)
End If
Next
End If
End With
You can use the CheckMember property to let the control automatically check the selection values from your assigned data source:
e.g.
clEmpresas.DataSource = bdEmpresa
clEmpresas.DisplayMember = "Nombre"
clEmpresas.ValueMember = "idEmpresa"
clEmpresas.CheckMember= "Acceso"
After that you can use the CheckedItems property to get the selected items if user changes selection in the control. see below example:
Private Sub SimpleButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SimpleButton1.Click
For Each item As DevExpress.XtraEditors.Controls.CheckedListBoxItem In CheckedListBoxControl1.CheckedItems
MessageBox.Show(item.Value.ToString())
Next
End Sub
Refer these:
get the selected items from a checkedlistbox
How to get checked rows of a data-bound CheckedListBoxControl
Get item index from databound DevExpress CheckedListBoxControl

create array of existing buttons vb 2013

I used to program in VB6 and am trying to write the same program in VB 2013. In this program I use an array of 49 buttons that all do the same thing when you click on them. I have figured out have to do the click function to a point:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button9.Click, Button10.Click, Button11.Click, Button12.Click, Button13.Click, Button16.Click, Button17.Click, Button18.Click, Button19.Click, Button20.Click
...
End Sub
What I am trying to do is simplify the code down to using an array so I can just pass on the index. One other person asked the same question in 2010 and the best answer was:
Button[] array = { firstButton, secondButton };
That would work but I want something with less typing. I also tried the following with failure:
One
Button[] buttons = this.Controls.OfType<Button>().ToArray();
Two
For i = 1 To 100
Dim btns() As Button = Controls.Find("Button" & i, True)
Dim btn As Button
If btns IsNot Nothing Then
btn = btns(0)
'If buttons Is Nothing Then
' ReDim buttons(0)
'Else
' ReDim Preserve buttons(buttons.Length)
'End If
'buttons(UBound(buttons)) = btn
btn.Text = i - 1 'here you can change whatever you want
End If
Next
Three
Dim buttons() As Button
buttons = Nothing
For Each b As Button In Me.Controls
If buttons Is Nothing Then
ReDim buttons(0)
Else
ReDim Preserve buttons(buttons.Length)
End If
buttons(UBound(buttons)) = b
Next
I just can't get it to accept the existing buttons into an array. I hope someone can help.
If your Buttons are nested inside container controls (e.g. a GroupBox) then you will need to perform a recursive search for all buttons. Maybe something like this (totally unoptimized)...
Private Function FindAllButtons(root As Control) As List(Of Button)
Dim result As List(Of Button) = New List(Of Button)()
For Each c As Control In root.Controls
If TypeOf c Is Button Then
result.Add(DirectCast(c, Button))
ElseIf c.HasChildren Then
result.AddRange(FindAllButtons(c))
End If
Next
Return result
End Function
Then just call that in your Form:
Dim allButtons as List(Of Button) = FindAllButtons(Me)
' Add common Click handler
For Each b As Button In allButtons
AddHandler b.Click, AddressOf Button_Click
Next
Update Just for fun, here's a generic version to find other types of control.
Private Function FindAllControls(Of T As Control)(root As Control) As List(Of T)
Dim result As List(Of T) = New List(Of T)()
For Each c As Control In root.Controls
If TypeOf c Is T Then
result.Add(DirectCast(c, T))
ElseIf c.HasChildren Then
result.AddRange(FindAllControls(Of T)(c))
End If
Next
Return result
End Function
You can use that like:
Dim allButtons As List(Of Button) = FindAllControls(Of Button)(Me)
Dim allTextBoxes As List(Of TextBox) = FindAllControls(Of TextBox)(Me)
Option two will work, you just need to add the button found into a list. I suggest you add your buttons into a List(Of ) instead of an array. You can always convert the list into an array if you really need to.
Dim buttonList As New List(Of Button)
For i As Integer = 1 To 100
Dim btns() As Control = Controls.Find("Button" & i, True)
If btns IsNot Nothing AndAlso btns.Length > 0 Then
buttonList.Add(CType(btns(0), Button))
End If
Next

Visual Basic Click Event Adding Scores together

Not 100% sure how to do this, mainly because I am new to programming here!
I have created a table which is populated with random numbers from an array, when the user clicks on one of the buttons the number needs to be added onto their score. eg, when a user clicks 10 and 15, their score will be 25. Once the user has clicked on the button, the button needs to change colour To AliceBlue (just a random colour). Any advice / examples?
This code below makes the table which is used within the game,
Let me know what you think!
Dim RandomNumbers = Enumerable.Range(0, 100).ToList()
Dim RandomNumber As New Random()
For Me.TableColunm = 0 To 4 Step 1
For Me.TableRow = 0 To 4 Step 1
Dim TemporaryNumber = RandomNumbers(RandomNumber.Next(0, RandomNumbers.Count))
RandomNumbers.Remove(CInt(TemporaryNumber))
TableButtons = New Button()
With TableButtons
.Name = "TextBox" & TableColunm.ToString & TableRow.ToString
.Text = TemporaryNumber.ToString
.Width = CInt(Me.Width / 4)
.Left = CInt(TableColunm * (Me.Width / 4))
.Top = TableRow * .Height
.Tag = TemporaryNumber
AddHandler TableButtons.Click, AddressOf TableClickEvent
End With
GameScreen.Controls.Add(TableButtons)
Next TableRow
Next TableColunm
Catch ex As Exception
MsgBox(ex.StackTrace & vbCrLf & "index1= " & TableColunm & vbCrLf & "index2= " & TableRow)
End Try
and
Public Sub TableClickEvent(sender As Object, e As EventArgs)
CType(sender, Button).BackColor = Color.BlueViolet
OverAllScoreInteger += CInt(CType(sender, Button).Tag)
End Sub
I also have to parse the score into a text box called 'UserScoreBox' the form is on 'GameScreen'
You add an eventhandler to a control by using the AddHandler statement and providing the sub that handles the event.
Private Sub Clickhandler(sender As Object, e As EventArgs)
CType(sender, Button).BackColor = Color.AliceBlue
End Sub
Sender is basically the source that initiated the event. In this case the button you clicked.
To make the button raise the event, add this to the code where you create the Buttons:
Addhandler TableButtons.Click, AddressOf Clickhandler
To add the scores you can for example place the index of the table row the button represents to the .Tag property of the button. That way you can, in the event handler, retrieve the row of the sender and add the values.
Edit: Adding the number:
On button creation, save the number in the tag (You could also just use the .Text, but doesn't really matter, it's one conversion less this way):
With TableButtons
.Tag = Temporarynumber
And in the handler unpack the object again:
Overallscore += CInt(CType(sender, button).Tag)

FindControl not finding dynamcily added user control in wizard control

I have a wizard control in wich I am adding a user control containing a simple table with
some input fields based on users entry of how many children they have. ex: how many kids do you have so I add the user control ascx based on that loop
that goes into step 5 of my wizard wich is also in a masterpage.
I then use findcontrol to atttempt the get to those input boxes so i can save the data into my db, findcontrol allway comes up null, even though the user control in visable and recreated on page load after post back.
any help greatly appreciated.
find control button:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim numbchildren As Integer = CInt(Howmanychildren.Text)
For i As Integer = 1 To numbchildren - 1
Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1_Child_Name"), TextBox)
'Dim textbox2 As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Howmanychildren"), TextBox)
If textbox IsNot Nothing Then
Response.Write("Found TextBox1 <br>")
Dim val As String = textbox.Text
Response.Write(val & "<br>")
Else
Response.Write("not found" & "<br>")
End If
' Insert into DB
'SaveValueToDatabase(val)
Next
End Sub
user control added function on dropdown :
Protected Sub Doyouhavechildren_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Doyouhavechildren.SelectedIndexChanged
Dim numbchildren As Integer = CInt(Howmanychildren.Text)
Dim cnt As Integer = 1
'Panel1.Controls.Clear()
Select Case Doyouhavechildren.SelectedIndex
Case 0
ViewState.Add("Doyouhavechildren", numbchildren)
Do While cnt <= numbchildren
Dim uc As Web.UI.UserControl = DirectCast(Page.LoadControl("MinorChild.ascx"), Web.UI.UserControl)
uc.ID = "Minor_" + cnt.ToString()
Wizard1.ActiveStep.Controls.Add(uc)
cnt = cnt + 1
Loop
Exit Select
Case 1
Exit Select
End Select
End Sub
user control:
<%# Control Language="VB" AutoEventWireup="false" CodeFile="MinorChild.ascx.vb" Inherits="MinorChild" %>
Name
Age
SS#
DOB
the find control works in the howmanychildren field that is static
I figured it out myself
basicly, you have to referance the container, thats what everyone everywhere else where saying but I kept ignoring the answer
the correct code is
Dim textbox As TextBox = TryCast(Me.Wizard1.FindControl("WizardStep5").FindControl("Minor_1").FindControl("Child_Name"), TextBox)
you have to referance the user control name first then search within it , even though the client source is disceptive.

Resources