Windows 窗体取消按钮不起作用

发布于 2025-01-01 00:42:09 字数 738 浏览 0 评论 0原文

我有一个 Visual Studio、Visual Basic 窗体,其中包含“确定”按钮和“取消”按钮。

我想要做的是让“确定”按钮保存用户选择的选项,当然“取消”按钮丢弃它们并将它们返回到之前的值。

但我注意到,当我调试表单时,无论我选择哪个按钮,这些值都会被保存。在表单的属性上,我已经声明 CancelBtn 确实是 CancelBtn,“确定”按钮是“确定”按钮,但无论如何,这些值仍然会被保存。

有没有更好的方法来完成我希望此表格执行的操作?

编辑:

这是到目前为止两个按钮的代码,两个按钮都被设置为关闭窗口。 AcceptOption 应保存值,CancelOption 应仅关闭表单。如果这做得不好,我很抱歉,但我发现的常见问题解答只提到更改每个按钮的属性,而没有提到代码:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles AcceptOptionBtn.Click
    ' Save the Options
    Me.Close()
    ' Close the form
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles CancelOptionBtn.Click
    ' Close the form
    Me.Close()
End Sub

I have a Visual Studio, Visual Basic form that includes an OK button and a Cancel button.

What I want to do is have the OK button save the options that the user chooses and of course the Cancel button discarding them and returning them to their previous values.

But what I'm noticing is that as I'm debugging the form, the values are being saved regardless of whichever button I'm choosing. On the form's properties, I have declared that indeed the CancelBtn is the CancelBtn and that the OK button is the OK button, but the values are still being saved regardless.

Is there a better way to do what I would like this form to do?

EDIT:

Here's the code so far for the two buttons, both are being set to close the window. AcceptOption should save the values and CancelOption should just close the form. I'm sorry if this isn't done well but the FAQ's that I found only mention changing the properties of each button and nothing about the code.:

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles AcceptOptionBtn.Click
    ' Save the Options
    Me.Close()
    ' Close the form
End Sub

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles CancelOptionBtn.Click
    ' Close the form
    Me.Close()
End Sub

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

追星践月 2025-01-08 00:42:09

在用户单击“保存”按钮之前不要更改“值”。

该表单应预加载您想要更新的值的副本。

取消按钮应该只是关闭表单。

“保存”按钮应该更新“值”,而不是表格副本。

编辑:-

关于这个问题,您发布的代码没有任何问题。是否为正确的按钮单击调用了正确的处理程序?表单的 AcceptButtonCancelButton 属性是否设置为正确的按钮?

如果有的话,您的编辑控件绑定到哪些数据?

Don't change "the values" until the user clicks the Save button.

The form should be preloaded with a copy of the values you would like to update.

The Cancel button should just close the form.

The Save button should cause "the values", not the forms copy, to be updated.

EDIT:-

In regard to this question, there is nothing wrong with the code you have posted. Are the right handlers being called for the right button clicks? Are the form's AcceptButton and CancelButton properties set to the right buttons?

What data are your editing controls bound to, if at all?

悲歌长辞 2025-01-08 00:42:09

“确定”和“取消”按钮没有什么神奇之处。它们只是...按钮。如果您每次进行更改时都保存数据,则“取消”按钮不会神奇地“取消保存”它们。但是,如果您在“确定”按钮的 Click 事件处理程序中保存更改,则单击“取消”按钮显然不会保存您的更改。为了进一步帮助您,我们需要知道您如何保存数据。

编辑:

通过查看您的代码,我认为您将数据直接传递到表单,而不执行对象的副本。因此,如果您修改此数据,父表单中的数据也会发生更改。通过使用此表单中的数据副本,任何未保存的更改都将被正确丢弃。

There's nothing magical about OK and Cancel buttons. They're just... buttons. If you save your data every time a change is made, the Cancel button won't magically "unsave" them. Though if you save changes in the OK button's Click event handler, then clicking the Cancel button obviously won't save your changes. To help you further we'd need to know how you save your data.

Edit:

From looking at your code, I think you're passing data directly to your form, without performing a copy of your objects. Therefore if you modify this data, it will also be changed in the parent form. By working with a copy of your data in this form, any changes which aren't saved will be correctly discarded.

梅窗月明清似水 2025-01-08 00:42:09

取消按钮的事件处理程序应如下所示:

Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
    Me.Close()
End Sub

确定按钮的事件处理程序应如下所示:

Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
    SaveSettings 'call a routine to save the settings the user has entered
    Me.Close()
End Sub

就这么简单!

Your event handler for the cancel button should look like this:

Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
    Me.Close()
End Sub

Your event handler for the OK button should look like this:

Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
    SaveSettings 'call a routine to save the settings the user has entered
    Me.Close()
End Sub

It is as simple as that!

ゝ偶尔ゞ 2025-01-08 00:42:09

如果您打开表单,就像

myForm.showdialog()

不必为关闭按钮单击事件定义处理程序一样,它会自动处理;只需为按钮设置 'DialogResult' 属性

btnCancel.DialogResult = DialogResult.Cancel

另外,如果您想在按下 ESC 时关闭表单,则设置 'CancelButton' 属性对于表单:

myForm.CancelButton = btnCancel

另一方面,如果您打开表单,

myForm.Show()

您确实需要指定对关闭按钮单击事件采取的操作,如下所示,即:

Private Sub BtnCancelClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
     Close()
End Sub

If you open your form like

myForm.showdialog()

you don't have to define the handler for the close button click event, it is automatically handled; just set the 'DialogResult' property for the button

btnCancel.DialogResult = DialogResult.Cancel

Also if you want to close the form when ESC is pressed then set the 'CancelButton' property for the form:

myForm.CancelButton = btnCancel

On the other hand if you open the form like

myForm.Show()

you do need to specify the action(s) to take on the close button click event as indicated here, ie:

Private Sub BtnCancelClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
     Close()
End Sub
飘然心甜 2025-01-08 00:42:09

我也遇到了同样的问题。一旦我使用 My.Settings.Blabla = Blabla.value,即使我没有使用 My.Settings.Save() ,它也会被保存,据我所知,这使得 My.Settings.Save() 完全毫无意义。

我最终接受了乔德尔的建议:在用户单击“保存”按钮之前不要更改“值”,但我不太清楚如何去做。

我最终在所有设置子项中使用临时变量而不是用户 My.Settings.UserConfigs。仅当我在 OK 子目录中时,我才调用

My.Settings.UserConfigSetting = tempor_UserCofigValue

以下是我正在处理的代码中的一个示例:

Private Sub btnOptionsThemeLB_Back_Update_Click(sender As System.Object, e As System.EventArgs) Handles btnOptionsThemeLB_Back_Update.Click
    If (tempOptionsThemeLB_Back = Nothing) Then
        tempOptionsThemeLB_Back = Me.btnOptionsThemeLB_Back.BackColor
    End If
    tempOptionsThemeLB_Back = RGBToColor(txtbOptionsThemeLB_Back_Red.Text, txtbOptionsThemeLB_Back_Green.Text, txtbOptionsThemeLB_Back_Blue.Text, tempOptionsThemeLB_Back)
    Me.btnOptionsThemeLB_Back.BackColor = tempOptionsThemeLB_Back
End Sub

并且只有在 Ok 子目录中,我才调用 My.Settings。

'Theme Section
My.Settings.colorBtnBack = tempOptionsThemeLB_Back

I was having the same issues. As soon as I use My.Settings.Blabla = Blabla.value, it gets saved even if I haven't used My.Settings.Save() which makes My.Settings.Save() completely pointless as far as I can tell.

I ended up taking up Jordell's advice: Don't change "the values" until the user clicks the Save button but it wasn't too clear for me how to go about it.

I ended up using temporary variables in all my settings subs instead of the user My.Settings.UserConfigs. Only when I was in the OK sub did I call

My.Settings.UserConfigSetting = temporary_UserCofigValue

Here is an example from the code I was working on:

Private Sub btnOptionsThemeLB_Back_Update_Click(sender As System.Object, e As System.EventArgs) Handles btnOptionsThemeLB_Back_Update.Click
    If (tempOptionsThemeLB_Back = Nothing) Then
        tempOptionsThemeLB_Back = Me.btnOptionsThemeLB_Back.BackColor
    End If
    tempOptionsThemeLB_Back = RGBToColor(txtbOptionsThemeLB_Back_Red.Text, txtbOptionsThemeLB_Back_Green.Text, txtbOptionsThemeLB_Back_Blue.Text, tempOptionsThemeLB_Back)
    Me.btnOptionsThemeLB_Back.BackColor = tempOptionsThemeLB_Back
End Sub

And only withing the Ok sub did I call My.Settings.

'Theme Section
My.Settings.colorBtnBack = tempOptionsThemeLB_Back
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文