点击Form窗体的AcceptButton按钮之后,如果发生错误,阻止窗体的关闭的办法

C# WinForm控件开发指南:从基础控件到实战应用 在C#桌面开发中,控件是构建用户界面的核心组件,它们作为程序与用户交互的桥梁,负责数据的输入、展示和操作响应。控件的原理基于事件驱动模型,通过属性、事件和布局三大要素协同工作,实现丰富的交互逻辑。掌握控件的使用不仅能提升开发效率,还能构建出专业、易用的应用程序界面,在企业管理软件、数据录入系统、工具软件等场景中广泛应用。本文聚焦WinForm控件的核心概念,深入解析常用控件的特性与实战技巧,涵盖按钮、文本框、数据网格等关键组件,帮助开发者从基础入门到灵活运用,快速构建功能完善的桌面应用。 阅读详情

在网上找了很多的解决办法,都无法解决碰到的问题, 比如this.AcceptButton.DialogResult = DialogResult.None;

最后只好自己想办法, 现把解决方案共享出来,供大家参考.

问题描述: 

为Form窗体设置了AcceptButton, 当在界面中回车, 将触发按钮的单击事件, 同时会关闭当前的窗体,

当出现错误的时候,我们并不需要立即关闭窗体, 例如一个修改密码的Form, 当我们把旧密码和新密码输入

之后, 然而旧密码出错, 这时应该保持Form的打开状态,而不是立即关闭窗体.

 

解决办法:

在按钮事件中如果发生错误, 则把Form的属性DialogResult设置为DialogResult.None, 同时在Form的关闭事件中判断如果窗体的返回值为DialogResult.None的时候,则取消窗体的关闭.

 

代码如下:

private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.OnChangePasswd();
            }
            catch (Exception ex)
            {
                EoMessageBox.ShowError(ex.Message);
                this.DialogResult = DialogResult.None;
            }
        }

protected override void OnClosing(CancelEventArgs e)
        {
            if (this.DialogResult == DialogResult.None)
                e.Cancel = true;
            base.OnClosing(e);
        }

C#窗口的Form.AcceptButton 属性和Form.CancelButton 属性 Form.AcceptButton 属性主要作用就是用来指定用户按下Enter键时响应的按钮,因为一个窗口上有很多按钮,如果不指定的话,就不知道选择一个作为回车键响应的按钮。这两个属性的作用大多数都是不需要鼠标操作的时候使用,大家也许感觉到奇怪,为什么会不需要鼠标的呢,其实这个就是在特定的场合使用,比如鼠标突然坏了,又想把软件关闭,或者做一些简单的处理,就需要使用键盘操作。初入门时,看到这两个属性一般不太关注它,因为这两个属性在窗口设计上也不需要设置就可以工作,因为默认了按下回车键的动作。 阅读详情

相关推荐

编译CycloneDDS和CycloneDDS-CXX踩过的坑——CMake Error at CMakeLists.txt:227 (find_package):

最近编译CycloneDDS和CycloneDDS-CXX踩了两个坑,花了不少时间。做个简单的记录,免得以后又踩到。注意:CycloneDDS-CXX编译依赖CycloneDDS,因此必须先编译CycloneDDS需要严格按照步骤进行,否则要入坑步骤如下,1先编译CycloneDDS以上步骤完成CycloneDDS的编译和安装,生成的bin include lib等位于install目录下面。

usstmiracle的博客 1903

this.AcceptButton = button1的用法:

1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Text; 7 using System.Windows.Forms; 8 ...

weixin_30781107的博客 364

无人驾驶汽车系统入门(三十二)——FEC算法:点云快速欧几里得聚类实战解析

本文深入解析了FEC算法在无人驾驶点云聚类中的高效应用,通过逐点处理策略和体素栅格预分割技术,实现了比传统欧几里得聚类快百倍的速度。文章详细介绍了算法原理、Python实现及参数调优指南,并对比了FEC与传统算法的性能差异,为无人驾驶系统开发提供了实用解决方案。

weixin_29065015的博客 491

c#控件

1、程序添加控件                      TextBox myText = new TextBox();             myText.Location = new Point(25,25);             myText.Text = "textbox ";             Label label = new Label();

snn1410的专栏 795

电影票购票小程序搭建(源码部署)

答:为用户提供便捷的购票方式,包括授权登录、个性化推荐、在线选座、订单管理等功能,同时在界面设计上注重用户友好性,简化用户的购票流程。答:通过展示电影院的优惠活动和票价信息,同时提供电影推荐功能,根据用户的历史购票记录和电影评分推荐个性化的电影列表,吸引用户购票。编写页面逻辑代码:使用JavaScript编写小程序页面的逻辑代码,实现电影列表的获取、电影详情的展示、座位选择的逻辑等。设计小程序界面:使用WXML和WXSS编写小程序的界面代码,实现电影列表的展示、电影详情的样式、座位选择页面的布局等。

2301_79072569的博客 2074

《物联网技术》课程笔记——第二章 物联网技术架构

文章目录第二章:物联网技术架构1 物联网技术体系(C3SD)2 系统体系结构的基本概念3 物联网体系结构——信息传递视角3.1物联网感知层3.1.1 感知层要解决的问题3.1.2 感知层的形态3.1.3 节点的感知能力与控制能力3.1.4 感知层实例3.2 物联网网络层3.2.1 网络相关概念3.2.2 IPV6与IPV4相比互联网协议的升级3.2.3 计算机网络的逻辑层次3.2.4 传输网的拓扑结构3.2.4.1 举例:智能小区的三表采集系统3.2.4.2 接入层3.2.4.2 汇聚层3.2.4.2 核心

南京大学软件工程硕士在读 2510

winform 获取光标、回车(键盘)事件

winform中获取光标和设置默认回车事件,都是在页面初始化时候进行绑定设置。 1、获取光标 #region Form2 -- 激活事件 private void Form2_Activated(object sender, EventArgs e) { //this.ActiveControl = textBox1;//获取光标 textBox1.Focus(); } ...

qingkaqingka的博客 6653

开源数显表

开源数显表,广泛应用于车床、铣床、磨床、镗床、火花机、线切割等。市面上此类液晶数显表多数都是基于此项目。 一、基本配置 硬件 主控:STM32F407VGT6 LCD:群创AT070TN92 显示驱动:RA8875 光栅尺:3 x 输入(X/Y/Z) 串口:1 x MAX3232 继电器:2 x 输出 存储:AT24C64 模拟输入:LM324C 电源:AC 220V 软件 工程版本:Keil 5 STM32库:V1.4.0 图形界面:uCGUI 多语言支持:中文、英文 二、项目文

m0_70934529的博客 443

Winform快捷键设置以及系解决统热键冲突技巧

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; nam

随笔 2116

1.5 RAG技术栈全景图:LangChain vs LlamaIndex深度对比,选错框架后悔3年,一文帮你选对

摘要:LangChain与LlamaIndex框架深度对比 本文全面对比了RAG领域两大主流框架LangChain和LlamaIndex。LangChain是通用AI应用框架,具有模块化设计和丰富生态;LlamaIndex专为RAG优化,提供开箱即用解决方案。功能对比显示,LangChain在文档加载和Agent支持方面更强,而LlamaIndex在检索策略、向量存储和多模态处理上表现更优。代码示例展示了二者实现RAG的不同方式:LangChain需要更多配置但灵活性高,LlamaIndex则提供更简洁的A

yonggeit的博客 3257

【信息系统项目管理师】第18章:项目绩效域详解

(3)度量绩效域与规划绩效域、项目工作绩效域和交付绩效域的相互作用:1规划构成了 交付和规划比较的基础;项目绩效域共同构成了一个统一的整体,作为一个完整系统,在项目生命周期过程中运行,系统内的每个绩效域相互作用、相互关联和相互依赖,并协调一致、共同运作,支撑项目目标和价值 的实现。(5)开发方法和生命周期绩效域与其他绩效域的相互作用:开发方法和生命周期绩效域与干系人绩效域、规划绩效域、不确定性绩效域、交付绩效域、项目工作绩效域和团队绩效域相互作用。(2)项目工作绩效域的绩效要点包括:项目过程;

数据知道的博客 7823

Winform Accept与文本框多行的冲突解决方法

如果设置Accept的情况下:多行的TextBox会在消息被AcceptButton拦截之前进行一次判断,如果同时TextBox多行,且允许回车键,则会相应TextBox的回车,而不会响应AcceptButtonToolstripTextBox 或者其他输入类控件若想开启自己定义的回车事件则需要开启允许回车符或者取消accepbutton或者更改accept事件 转载于:https://www.c...

weixin_30292745的博客 109

MES通信从入门到精通(12)——HTTP报文组成

HTTP 协议由起始行、头部、空行、消息体组成。

qq_34059233的博客 100

winform关于AcceptButton和CanceleButton的问题

1.Form.AcceptButton 属性:取得或设定使用者按下 ENTER 键時所按下的表单上按鈕,指派至这个属性的按鈕必须是在目前表单上或位於目前表單上容器內的IButtonControl(也就是只有按鈕能用它) private void Form1_Load(object sender, EventArgs e) { this.AcceptButton = this.butt

我的梦 6303

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

Statistics and Machine Learning Toolbox

Statistics and Machine Learning Toolbox(带书签),MATLAB统计学习工具

C#阻止窗体关闭的方法

有时候 我们往往需要在用户点击关闭窗体(或窗体的关闭事件产生)时,对一些变量进行判断,如果存在问题则使用弹出对话框的形式来警告和询问用户,让使用确定是否关闭。用以下几个方法可以实现:   1、窗体关闭事件的重截法 //重截窗体的OnClosing方法 protected override void OnClosing(CancelEventArgs e)

iloli的专栏 1万+

jre9下载 jdk9

(Java SE Runtime Environment 9.0) 9.0u175 官方最新版 【x86|x64】

C#控制台程序启动调试结束后让窗口不自动关闭

解决调试时自动关闭窗口的问题

ToabyKing的博客 7358

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

Winform窗体下,AcceptButton已经设置了Button,TextBox仍能接收回车键

Winform窗体下,AcceptButton已经设置了Button,TextBox仍能接收回车键

yuankaiwsl的博客 2535

Form的AcceptButton属性,相当于按下Enter建

Form的AcceptButton属性,相当于按下Enter建

异道的专栏 1858

winform按钮学习之AcceptButton

使用AcceptButton优化用户体验

weixin_42528820的博客 1321

WinForm default button 窗体的默认按钮 [AcceptButton]

有时候需要在一个窗体上设置一个默认按钮,就是当我们按下Enter键是需要出发的按钮。 Form对象都有一个AcceptButton的属性,设置这个属性选择你希望的默认按钮即可。 设置完之后,对应的按钮就会有个蓝色的外框,代表已经获得了默认的焦点。 ...

遥远的心 2788

设定了窗口的AcceptButton及CancelButton点击时无动作的原因

设定如下 Codethis.AcceptButton=this.btnOK;this.CancelButton=this.btnCancel; 接下来设定 Codethis.btnOK.DialogResult=System.Windows.Forms.DialogResult.OK;...

weixin_30920853的博客 447

van-dialog:“确认”验证失败时阻止对话框的关闭

这里写自定义目录标题欢迎使用Markdown编辑器新的改变功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片生成一个适合你的列表创建一个表格设定内容居中、居左、居右SmartyPants创建一个自定义列表如何创建一个注脚注释也是必不可少的KaTeX数学公式新的甘特图功能,丰富你的文章UML 图表FLowchart流程图导出与导入导出导入 欢迎使用Markdown编辑器 你好! 这是你第一次使用 Markdown编辑器 所展示的欢迎页。如果你想学习如何使用Mar

qq_37800258的博客 6627
上一篇: EODA For Oracle Web V1.3.1102
下一篇: ASP.NET: 为自定义控件设置主题(Theme)样式
eooda
博客等级 码龄20年 2粉丝 14原创
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值