Results 1 to 13 of 13

Thread: [RESOLVED] In Vb.net I need to add a message box to current code with Red backcolor

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Resolved [RESOLVED] In Vb.net I need to add a message box to current code with Red backcolor

    I have a button that retrieves data and place the information in data grid, its currently working but I need to add a message box to it. If the back color in the grid is red when the button is click a message box pop up "Correct all red fields before continuing." I have tried to insert message boxes to test but in the for each loop the message box appeared for each red back color cell and that's not what I need it to do. I need it to search the entire grid for for red back color and display the message box.

    Here is the code:
    Code:
    Private Sub Retrieve()
    
    On Error GoTo errH
    
    Dim con As OdbcConnection
    
    Dim cmd As OdbcCommand
    
    Dim dr As OdbcDataReader
    Dim strItem1 As String
    Dim strItem2 As String
    
    Dim strOdbc As String = ""
    Dim hasRows As Boolean = False
    Dim strItem3 As String
    Dim strRelease As String
    
    Dim sqlStr As String
    
    
    Dim strDsn, strSystem As String
    strDsn = ComboBox1.Text
    strSystem = txtSystem.Text
    
    
    strOdbc = "{iSeries As ODBC Driver};System=" + strSystem + ";Dsn=" + strDsn + ";Uid=TEST!;Pwd=Test;"
    con = New OdbcConnection(strOdbc)
    con.Open()
    Using con
    
    For Each dgvRow As DataGridViewRow In gridUserEntries.Rows
    If (Not IsNothing(dgvRow.Cells(0))) Then
    If (Not (IsNothing(dgvRow.Cells(0).Value))) Then
    strRelease = dgvRow.Cells(0).GetEditedFormattedValue(dgvRow.Cells(0).RowIndex, DataGridViewDataErrorContexts.Display)
    sqlStr = "Select * from jobscopedb.ppusrfs where search_key_uf= '" + strRelease + "'"
    cmd = New OdbcCommand(sqlStr, con)
    'con.Open()
    dr = cmd.ExecuteReader()
    hasRows = dr.HasRows
    While (hasRows And dr.Read)
    strRelease = dr.Item(0).ToString
    strItem2 = dr.Item(2).ToString
    strItem3 = dr.Item(3).ToString
    
    If (strItem2.Contains("27 PRODWK")) Then
    dgvRow.Cells(1).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("28 SHIPMON")) Then
    dgvRow.Cells(2).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("30 %COMPL")) Then
    dgvRow.Cells(3).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("31 TGTSHIP")) Then
    dgvRow.Cells(4).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("70 SCHPROD")) Then
    dgvRow.Cells(5).Value = dr.Item(3).ToString.Trim
    End If
    If (strItem2.Contains("81 AB%")) Then
    dgvRow.Cells(6).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("82 ARM%")) Then
    dgvRow.Cells(7).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("83 SHAFT%")) Then
    dgvRow.Cells(8).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("84 FIT% ")) Then
    dgvRow.Cells(9).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("85 HDW%")) Then
    dgvRow.Cells(10).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("86 FIN% ")) Then
    dgvRow.Cells(11).Value = dr.Item(4).ToString.Trim
    End If
    If (strItem2.Contains("87 WELDCMP")) Then
    dgvRow.Cells(12).Value = dr.Item(3).ToString.Trim
    End If
    
    End While
    cmd.Dispose()
    End If
    End If
    Next
    End Using
    
    Exit Sub
    
    errH:
    
    MsgBox(Err.Description)
    
    con = Nothing
    
    End Sub

  2. #2
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    I have read the question several times now and it's not exactly clear what problem you're having. Could you take your time and write a more clear explanation. It reads like it was written in a hurry. I think I know what you're asking but I don't want to make assumptions. It just wastes everyone's time when the assumption is wrong.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  3. #3
    PowerPoster
    Join Date
    Sep 2005
    Location
    Modesto, Ca.
    Posts
    5,508

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Yeah, I don't see where you check the cell backcolor and display a messagebox anywhere in the code you post. But if you want to check all cells in a datagridview then

    Code:
            For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
                For Each c As DataGridViewCell In row.Cells
                    MessageBox.Show(c.Style.BackColor.ToString)
                Next
            Next
        End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Quote Originally Posted by wes4dbt View Post
    Yeah, I don't see where you check the cell backcolor and display a messagebox anywhere in the code you post. But if you want to check all cells in a datagridview then

    Code:
            For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
                For Each c As DataGridViewCell In row.Cells
                    MessageBox.Show(c.Style.BackColor.ToString)
                Next
            Next
        End Sub
    When I put this code in I get the message box for ever red back color field in the grid. How do I get it to display only one time if there is red back color in the grid?

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Set a boolean in the loop, and then show the messagebox after the loop if the boolean is true.

    But I would also exit the loop as soon as the first red background cell is found, since you don't care if there is one or multiple, no point in continuing to search once you find the first one.
    Last edited by passel; Dec 12th, 2020 at 10:41 PM.
    "Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Code:
    Dim found As Boolean = False
    For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
        For Each c As DataGridViewCell In row.Cells
            If c.Style.BackColor = Color.Red Then found = True
        Next
    Next
    
    If found Then
        MessageBox.Show("found red cell")
    End If

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,630

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Quote Originally Posted by .paul. View Post
    Code:
    Dim found As Boolean = False
    For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
        For Each c As DataGridViewCell In row.Cells
            If c.Style.BackColor = Color.Red Then found = True
        Next
    Next
    
    If found Then
        MessageBox.Show("found red cell")
    End If
    Another option that will terminate the loops once the first red cell is found:

    Code:
    Dim found As Boolean = False
    For Each row As DataGridViewRow In Me.LotsDataGridView.Rows
        For Each c As DataGridViewCell In row.Cells
            If c.Style.BackColor = Color.Red Then
                found = True
                MessageBox.Show("found red cell")            
                Exit For
            End If
        Next
        If found = True Then
            Exit For
        End If
    Next

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    I have created a program that have a Datagridview and buttons (Clear, Retrieve, and Update). The first column in the grid (column 0) is called Release . End users enter release numbers in this column and tab (Cell leave) , it then validates if the release is in the database. If the release is valid the back color is white, if the release is invalid the back color turns red. The code I provided is for the retrieve button. Currently when you click the retrieve button and you have valid and invalid releases it pulls the valid release data and leaves the invalid with the red back color blank. I would like if there are red back color fields to populate a message box and not return data for the valid release until the invalid is corrected .

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Quote Originally Posted by msboddie View Post
    I have created a program that have a Datagridview and buttons (Clear, Retrieve, and Update). The first column in the grid (column 0) is called Release . End users enter release numbers in this column and tab (Cell leave) , it then validates if the release is in the database. If the release is valid the back color is white, if the release is invalid the back color turns red. The code I provided is for the retrieve button. Currently when you click the retrieve button and you have valid and invalid releases it pulls the valid release data and leaves the invalid with the red back color blank. I would like if there are red back color fields to populate a message box and not return data for the valid release until the invalid is corrected .
    To help more with that more specific issue, we need to see the relevant code...

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Posts
    13

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Quote Originally Posted by .paul. View Post
    To help more with that more specific issue, we need to see the relevant code...
    This code below is the current code that retrieves invalid (red back color) and valid release in the grid. The question I have is how do I add a message box to this code to populate a message "Please update the red fields" and not populate the valid data until the red back color is white.


    Code:
    Private Sub Retrieve()
    
            On Error GoTo errH
    
            Dim con As OdbcConnection
    
            Dim cmd As OdbcCommand
    
            Dim dr As OdbcDataReader
            Dim strItem1 As String
            Dim strItem2 As String
    
            Dim strOdbc As String = ""
            Dim hasRows As Boolean = False
            Dim strItem3 As String
            Dim strRelease As String
    
            Dim sqlStr As String
            Dim strDsn, strSystem As String
            strDsn = ComboBox1.Text
            strSystem = txtSystem.Text
    
            strOdbc = "{iSeries As ODBC Driver};System=" + strSystem + ";Dsn=" + strDsn + ";Uid=TEST;Pwd=TEST1;"
            con = New OdbcConnection(strOdbc)
            con.Open()
            Using con
    
                For Each dgvRow As DataGridViewRow In gridUserEntries.Rows
                    If (Not IsNothing(dgvRow.Cells(0))) Then
                        If (Not (IsNothing(dgvRow.Cells(0).Value))) Then
                            strRelease = dgvRow.Cells(0).GetEditedFormattedValue(dgvRow.Cells(0).RowIndex, DataGridViewDataErrorContexts.Display)
                            sqlStr = "Select * from jobscopedb.ppusrfs where search_key_uf= '" + strRelease + "'"
                            cmd = New OdbcCommand(sqlStr, con)
                            'con.Open()
                            dr = cmd.ExecuteReader()
                            hasRows = dr.HasRows
                            While (hasRows And dr.Read)
                                strRelease = dr.Item(0).ToString
                                strItem2 = dr.Item(2).ToString
                                strItem3 = dr.Item(3).ToString
    
                                If (strItem2.Contains("27 PRODWK")) Then
                                    dgvRow.Cells(1).Value = dr.Item(3).ToString.Trim
                                End If
                                If (strItem2.Contains("28 SHIPMON")) Then
                                    dgvRow.Cells(2).Value = dr.Item(3).ToString.Trim
                                End If
                                If (strItem2.Contains("30 %COMPL")) Then
                                    dgvRow.Cells(3).Value = dr.Item(3).ToString.Trim
                                End If
                                If (strItem2.Contains("31 TGTSHIP")) Then
                                    dgvRow.Cells(4).Value = dr.Item(3).ToString.Trim
                                End If
                                If (strItem2.Contains("70 SCHPROD")) Then
                                    dgvRow.Cells(5).Value = dr.Item(3).ToString.Trim
                                End If
                                If (strItem2.Contains("81 AB%")) Then
                                    dgvRow.Cells(6).Value = dr.Item(4).ToString.Trim
                                End If
                                If (strItem2.Contains("82 ARM%")) Then
                                    dgvRow.Cells(7).Value = dr.Item(4).ToString.Trim
                                End If
                                If (strItem2.Contains("83 SHAFT%")) Then
                                    dgvRow.Cells(8).Value = dr.Item(4).ToString.Trim
                                End If
                                If (strItem2.Contains("84 FIT% ")) Then
                                    dgvRow.Cells(9).Value = dr.Item(4).ToString.Trim
                                End If
                                If (strItem2.Contains("85 HDW%")) Then
                                    dgvRow.Cells(10).Value = dr.Item(4).ToString.Trim
                                End If
                                If (strItem2.Contains("86 FIN% ")) Then
                                    dgvRow.Cells(11).Value = dr.Item(4).ToString.Trim
                                End If
                                If (strItem2.Contains("87 WELDCMP")) Then
                                    dgvRow.Cells(12).Value = dr.Item(3).ToString.Trim
                                End If
    
                            End While
                            cmd.Dispose()
                        End If
                    End If
                Next
            End Using
           
            Exit Sub
    
    errH:
    
            MsgBox(Err.Description)
    
            con = Nothing
    
    
        End Sub

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    So, you need to run my code that OptionBase1 improved in post #7.
    Run that code, and if found = false, do your updates, else show a msgbox and exit sub

  12. #12
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: In Vb.net I need to add a message box to current code with Red backcolor

    Your errorhandler...

    Code:
    On Error GoTo errH
    That's legacy code. In .Net, we use a Try, Catch block...

  13. #13
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: In Vb.net I need to add a message box to current code with Red backcolor


Tags for this Thread

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