Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
446 views
in Technique[技术] by (71.8m points)

vb.net - Picturebox.click acting different than button.click?

So I want to loop through Datagridview and search for checkboxes with True value. Both Controls (Button and Picturebox) have the exact same code, but they act differently. If I click on the PictureBox, it won't detect either the last checked row or the first, but when I click on the button first, it works then as it should. (Button however works perfectly every time)

What could be the issue with this?

Code:

Private Sub PictureBoxVorgangAnlegen_Click(sender As Object, e As EventArgs) Handles PictureBoxVorgangAnlegen.Click
    For k As Integer = 0 To DataGridView1.Rows.Count - 1
        If DataGridView1.Rows(k).Cells("Auswahl").Value = True Then
            MsgBox("true " & DataGridView1.Rows(k).Cells("Position").Value)
        End If
    Next
End Sub
question from:https://stackoverflow.com/questions/65932853/picturebox-click-acting-different-than-button-click

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

It's not the Click event handlers acting differently at all. They are both doing exactly the same thing. The difference is that a Button can receive focus and does when you click it while a PictureBox cannot and does not. That means that the actions that the grid performs when it loses focus have occurred when your Button control's Click handler is executed but not when the Click event handler for your PictureBox is executed. If you were to, for instance, click into a TextBox first and then click the PictureBox, you'd find the result would be the same as if you had clicked the Button.

You need to do something to force those actions to occur. Without having tested, I think that calling the EndEdit method of the grid should be enough. You may need to call the form's Validate method as well or instead but I'd go with EndEdit as a first option.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...