Results 1 to 5 of 5

Thread: make more beautiful

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    70

    make more beautiful

    The code works as it should, but I think you can make it shorter and cleaner. Can someone please help me?

    Code:
     For i = 0 To dgvplugins.Rows.Count - 1
                        Try
    
                            If dgvplugins.Rows(i).Cells(1).Value = dgvplugins.Rows(i).Cells(2).Value Then
                                dgvplugins.Rows(i).Cells(1).Style.ForeColor = System.Drawing.Color.LightGreen
                                dgvplugins.Rows(i).Cells(4) = New DataGridViewImageCell With {.Value = My.Resources.ok_16green}
    
                            Else
    
                                dgvplugins.Rows(i).Cells(1).Style.ForeColor = System.Drawing.Color.DeepPink
    
                                dgvplugins.Rows(i).Cells(4) = New DataGridViewImageCell With {.Value = My.Resources.warningrund_16}
    
    
    
                                If dgvplugins.Rows(i).Cells(3).Value = "cf" Then
                                    dgvplugins.Rows(i).Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.codefling}
                                ElseIf dgvplugins.Rows(i).Cells(3).Value = "cc" Then
                                    dgvplugins.Rows(i).Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.chaoscode}
                                ElseIf dgvplugins.Rows(i).Cells(3).Value = "ld" Then
                                    dgvplugins.Rows(i).Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.lonedesign}
                                ElseIf dgvplugins.Rows(i).Cells(3).Value = "mv" Then
                                    dgvplugins.Rows(i).Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.myvector}
                                ElseIf dgvplugins.Rows(i).Cells(3).Value = "um" Then
                                    dgvplugins.Rows(i).Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.umod}
    
    
                                    dgvplugins.Rows(i).Cells(6) = New DataGridViewImageCell With {.Value = My.Resources.arrow_158_16}
                                End If
                            End If
                        Catch ex As Exception
                        End Try
                    Next

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: make more beautiful

    How about you explain what you're trying to achieve, instead of making us all work it out from the code?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2022
    Posts
    70

    Re: make more beautiful

    Thought that was obvious because the code is so bad.

    Don't know what there is to explain.
    I add a new row to the DataGridView.

    Then in the added row I want to compare cell 1 and 2.
    If cell 1 and 2 are the same, then change the ForeColor in cell 1 to LightGreen and insert an image in cell 4

    If cell 1 and 2 are not the same, then change the ForeColor in cell 1 to DeepPink and insert an image in cell 4 and 6.

    Then in the same row check cell 3 for a text.
    If e.g. in cell 3 cf is written, then in cell 5 the image codefling should be added.

    The code works, but I think you can make it shorter/cleaner/better.

    Option Strict Off should be like this for now.

  4. #4
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    5,872

    Re: make more beautiful

    The code can be shortened using With .. End With blocks
    And by replacing the long If .. ElseIF .. block
    Code:
    ' Just a small part 
    With dgvplugins.Rows(i)
      Select Case .Cells(3).Value 
    	case "cf": .Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.codefling}
    	case "cc": .Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.chaoscode}
    	case "ld": .Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.lonedesign}
    	case "mv": .Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.myvector}
    	case "um" 
    		.Cells(5) = New DataGridViewImageCell With {.Value = My.Resources.umod}
    		.Cells(6) = New DataGridViewImageCell With {.Value = My.Resources.arrow_158_16}			
      End Select
    End With

  5. #5
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,715

    Re: make more beautiful

    Moderator Actions - Moved thread to Code It Better forum.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width