简体   繁体   English

Picturebox.click 的行为与 button.click 不同?

[英]Picturebox.click acting different than button.click?

So I want to loop through Datagridview and search for checkboxes with True value.所以我想遍历 Datagridview 并搜索具有 True 值的复选框。 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.如果我单击 PictureBox,它不会检测到最后选中的行或第一行,但是当我首先单击按钮时,它会正常工作。 (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

It's not the Click event handlers acting differently at all.根本不是Click事件处理程序的行为不同。 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.不同之处在于Button可以接收焦点,并且当您单击它时可以接收焦点,而PictureBox不能也不会。 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.这意味着网格失去焦点时执行的操作是在执行Button控件的Click处理程序时发生的,而不是在执行PictureBoxClick事件处理程序时发生的。 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 .例如,如果您要先单击TextBox然后单击PictureBox ,您会发现结果与单击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.未经测试,我认为调用网格的EndEdit方法应该足够了。 You may need to call the form's Validate method as well or instead but I'd go with EndEdit as a first option.您可能还需要调用表单的Validate方法,或者改为使用 EndEdit 作为第一个选项的EndEdit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM