none
FromlArgb gives an error "Not A Member Of" RRS feed

  • Question

  • Project: VB with multiple Standard Forms where one contains a DataGridView object.

    I use FromArgb on several forms to change a textbox color with no problem.  But, when I try to use it in the form that contains the DataGridView, I get an error that says FromArgb is not part of the DataGridView.

    To get around this error, I use a common module with a function that returns Color.

    So, I can set a cell color with Color but I can not use Color.FromArgb to do it.

    I have tried the FromArgb on my three other forms and it defines the Color with no error.

    I have deleted the form with DataGridView and recreated it but I still get the "Not A Member Of" error.

    Exact error: 'FromArgb' is not a member of 'DataGridView TextboxColumn'

    This (to me) indicates that the Color Class is being overloaded by the DataGridView class.

    Thanks for any suggestions on how to fix this (because I hate to use module code like this)

    Lance

    Friday, June 8, 2018 6:47 AM

Answers

All replies

  • Does it work if you write :

    System.Drawing.Color.FromArgb
    ?

    • Marked as answer by LanceSummers Saturday, June 9, 2018 8:01 AM
    Friday, June 8, 2018 6:57 AM
  • Hi Lance

    Be careful to not show any of the actual code. After all, it would spoil our guessing.

    Seriously, how can you expect help for code you do not show?

    Show at least, the line causing the exception, and maybe some more before that line.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, June 8, 2018 11:43 AM
    Friday, June 8, 2018 11:43 AM
  • Here is the code that is called after the DataGridView is defined:

        Private Sub PopulateGrid()
            Dim Index As Long
            Dim CellColor As Color
    
            RecGrid.Rows.Clear()
            Index = 0
            While (Index < UBound(WordRecs))
                RecGrid.Rows.Add(WordRecs(Index).Title, WordRecs(Index).Note, WordRecs(Index).ColorCode,
                                 WordRecs(Index).Release, WordRecs(Index).Resolution,
                                 WordRecs(Index).Code, WordRecs(Index).Rating, WordRecs(Index).Genre,
                                 WordRecs(Index).Indexed, WordRecs(Index).Tested)
                Index = Index + 1
                Application.DoEvents()
            End While
    
            'We do the loop again so that the 'Filling the grid will not be slowed to a crawl
            'For some reason, changing a cell color makes a noticable difference in performance
            MenuStatus.Text = "Coloring Title Cells"
            Index = 0
            While (Index < UBound(WordRecs))
                If (Len(WordRecs(Index).ColorCode) > 0) Then
                    'CellColor = Color.FromArgb(255, 160, 160) gives an Error so we have To use a common Module For this
                    'Created CellColor variable so that we can debug this step by itself
                    CellColor = CommonCode.SetBackColor(WordRecs(Index).ColorCode)
                    RecGrid.Rows(Index).Cells(0).Style.BackColor = CellColor
                    If (WordRecs(Index).ColorCode = "D") Then
                        RecGrid.Rows(Index).Cells(0).Style.ForeColor = CommonCode.SetBackColor("W")
                    Else
                        'Leave the Text color as is
                    End If
                Else
                    'We do not set white
                End If
                Index = Index + 1
                Application.DoEvents()
            End While
            Me.Text = "Grid Form For Word Data (" + Trim(Str(RecGrid.Rows.Count)) + ")"
        End Sub 'PopulateGrid
    Module CommonCode
        'Reason for this commoncode module:
        'GridForm throws an error when FromArgb is used, therefore I have
        'to use a function outside of the GridForm to do the work
        'What is strange, IndexCardForm uses FromArgb with no problem
        Public Function SetBackColor(ColorCode As String) As Color
            Dim BackColor As Color
            'Dim ColorAscii As Long
    
            Select Case (ColorCode)
                Case ("R")
                    BackColor = Color.FromArgb(255, 160, 160) 'Red
                Case ("P")
                    BackColor = Color.FromArgb(252, 186, 250) 'Pink
                Case ("B")
                    BackColor = Color.FromArgb(170, 170, 253) 'Blue
                Case ("G")
                    BackColor = Color.FromArgb(176, 255, 176) 'Green
                Case ("Y")
                    BackColor = Color.FromArgb(255, 255, 176) 'Yellow
                Case ("T")
                    BackColor = Color.FromArgb(176, 226, 250) 'Turqoise
                Case ("F")
                    BackColor = Color.LightGray
                Case ("D")
                    BackColor = Color.Blue
                Case Else
                    BackColor = Color.White
            End Select
            Return BackColor
        End Function
    End Module



    Friday, June 8, 2018 3:52 PM
  • Lance,

    Try:

           FromARGB(255, 255, 160, 160)

    where A is alpha the opacity and the other three are Red, Green, Blue.

    You could also do this:

          dim MyColor as color = Color.Magenta

          dim MyColor as color = Color.fromargb( 255, Color.Magenta)


    Friday, June 8, 2018 4:36 PM
  • Hi

    Here is some code that uses .ARGB colors and works normally on a DGV.  Also adjusts color when ColorCode cell is changed.

    ' Form1 with blank DataGridView
    ' named DGV
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim WordRecs As New DataTable("Freddy")
      Dim r As New Random
      Dim defCol As Color
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With WordRecs
          .Columns.Add("Title")
          .Columns.Add("Note")
          .Columns.Add("ColorCode")
          .Columns.Add("Release")
          .Columns.Add("Resolution")
          .Columns.Add("Code")
          .Columns.Add("Rating")
          .Columns.Add("Genre")
          .Columns.Add("Indexed")
          .Columns.Add("Tested")
    
          Dim c As String = "rgboyv"
          For i As Integer = 1 To 20
            .Rows.Add("Title" & i.ToString, "Note" & i.ToString, c.Substring(r.Next(0, c.Length), 1), Now, i.ToString, "ABC - 22", "12+", "Action" & i.ToString, "Yes")
          Next
        End With
        DGV.DataSource = WordRecs
    
        For Each r As DataGridViewRow In DGV.Rows
          If Not r.Index = DGV.NewRowIndex Then
            r.Cells("Title").Style.BackColor = SetBackColor(r.Cells("ColorCode").Value.ToString)
          End If
        Next
        DGV.DefaultCellStyle.BackColor = Color.Beige
        defCol = DGV.DefaultCellStyle.BackColor
      End Sub
      Function SetBackColor(s As String) As Color
        If s.Length < 1 Then Return defCol
        Select Case s.ToUpper
          Case "R"
            Return Color.FromArgb(255, 255, 99, 71)
          Case "G"
            Return Color.FromArgb(255, 0, 200, 0)
          Case "B"
            Return Color.FromArgb(255, 135, 206, 250)
          Case "O"
            Return Color.FromArgb(255, 255, 140, 0)
          Case "Y"
            Return Color.FromArgb(255, 245, 245, 0)
          Case "V"
            Return Color.FromArgb(255, 186, 85, 211)
          Case Else
            Return defCol
        End Select
      End Function
    
      Private Sub DGV_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DGV.CellEndEdit
        If e.ColumnIndex = DGV.Columns("ColorCode").Index Then
          DGV(0, e.RowIndex).Style.BackColor = SetBackColor(DGV("ColorCode", e.RowIndex).Value.ToString)
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland



    • Edited by leshay Friday, June 8, 2018 5:02 PM
    Friday, June 8, 2018 4:59 PM
  • I totally missed this comment.

    I tried it and it works by explicitly defining the Color Class to use.

    Moved the Select from the module to the function and changed the Color.FromArgb reference to use the explicit System.Drawing.Color.FromArgb and tested it.  Works just fine.

    Thanks, Castorix31

    Saturday, June 9, 2018 8:01 AM