this.AcceptButton = button1的用法:

本文介绍了一个简单的C# Windows窗体应用程序示例,展示了如何设置窗体的默认按钮并处理点击事件,同时提供了完整的代码实现。
 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 
 9 namespace Test02
10 {
11     public partial class Form1 : Form
12     {
13         public Form1()
14         {
15             InitializeComponent();
16         }
17 
18         private void Form1_Load(object sender, EventArgs e)
19         {
20             this.AcceptButton = button1;
21         }
22 
23         private void button1_Click(object sender, EventArgs e)
24         {
25             MessageBox.Show("引发了接受按钮1");
26         }
27 
28         
29     }
30 }

this.AcceptButton = button1;只是能对选中的按钮进行操作,并不能对未选中的进行操作。

转载于:https://www.cnblogs.com/liuyaozhi/p/4973280.html

// 程序集特性必须放在最前面 using System; using System.Collections.Generic; using System.Reflection; using System.Windows.Forms; // 显式引用System.Drawing中的类型 using DrawingPoint = System.Drawing.Point; using DrawingSize = System.Drawing.Size; // AutoCAD相关命名空间 using Autodesk.AutoCAD.ApplicationServices; using Autodesk.AutoCAD.DatabaseServices; using Autodesk.AutoCAD.EditorInput; using Autodesk.AutoCAD.Geometry; using Autodesk.AutoCAD.Runtime; // 为AutoCAD应用程序创建别名以避免冲突 using AcApplication = Autodesk.AutoCAD.ApplicationServices.Application; // 确保插件有唯一标识 [assembly: Guid("B6D4F4A2-7A8E-4F3A-9B7D-8EAC1F7C8D9B")] [assembly: ExtensionApplication(typeof(CADPlugin.Initialization))] [assembly: CommandClass(typeof(CADPlugin.Commands))] namespace CADPlugin { #region 插件初始化和命令类 public class Initialization : IExtensionApplication { public void Initialize() { Editor ed = AcApplication.DocumentManager.MdiActiveDocument.Editor; ed.WriteMessage("\nCAD插件已加载。输入STEXT创建单行文字,MTEXT创建多行文字..."); } public void Terminate() { } } public class Commands { [CommandMethod("STEXT")] public void SingleText() { SingleTextForm form = new SingleTextForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("MTEXT")] public void MultiText() { MultiTextForm form = new MultiTextForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("TEXTMERGE")] public void TextMerge() { Document doc = AcApplication.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { PromptSelectionOptions pso = new PromptSelectionOptions(); pso.MessageForAdding = "选择要合并的文字: "; PromptSelectionResult psr = ed.GetSelection(pso); if (psr.Status != PromptStatus.OK) return; TextMerger.MergeSelectedTexts(psr.Value); } catch (System.Exception ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } } [CommandMethod("ELEVATION")] public void Elevation() { ElevationForm form = new ElevationForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("LEADER")] public void Leader() { LeaderForm form = new LeaderForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("NOTATION")] public void Notation() { NotationForm form = new NotationForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("SECTION")] public void Section() { SectionForm form = new SectionForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("REVISIONCLOUD")] public void RevisionCloud() { RevisionCloudForm form = new RevisionCloudForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("BREAKLINE")] public void BreakLine() { BreakLineForm form = new BreakLineForm(); AcApplication.ShowModalDialog(form); } [CommandMethod("TITLE")] public void Title() { TitleForm form = new TitleForm(); AcApplication.ShowModalDialog(form); } } #endregion #region 窗体类实现 public class SingleTextForm : Form { public TextBox ContentTextBox; public ComboBox StyleComboBox; public TextBox HeightTextBox; public TextBox AngleTextBox; public SingleTextForm() { InitializeComponent(); } private void InitializeComponent() { this.Text = "单行文字"; this.Size = new DrawingSize(300, 250); this.StartPosition = FormStartPosition.CenterScreen; // 文字内容标签和文本框 Label contentLabel = new Label(); contentLabel.Text = "文字内容:"; contentLabel.Location = new DrawingPoint(20, 20); contentLabel.Size = new DrawingSize(80, 20); this.Controls.Add(contentLabel); ContentTextBox = new TextBox(); ContentTextBox.Location = new DrawingPoint(100, 20); ContentTextBox.Size = new DrawingSize(160, 20); this.Controls.Add(ContentTextBox); // 文字样式标签和下拉框 Label styleLabel = new Label(); styleLabel.Text = "文字样式:"; styleLabel.Location = new DrawingPoint(20, 50); styleLabel.Size = new DrawingSize(80, 20); this.Controls.Add(styleLabel); StyleComboBox = new ComboBox(); StyleComboBox.Location = new DrawingPoint(100, 50); StyleComboBox.Size = new DrawingSize(160, 20); StyleComboBox.Items.AddRange(new string[] { "Standard", "仿宋", "黑体", "宋体" }); StyleComboBox.SelectedIndex = 0; this.Controls.Add(StyleComboBox); // 字高标签和文本框 Label heightLabel = new Label(); heightLabel.Text = "字高:"; heightLabel.Location = new DrawingPoint(20, 80); heightLabel.Size = new DrawingSize(80, 20); this.Controls.Add(heightLabel); HeightTextBox = new TextBox(); HeightTextBox.Location = new DrawingPoint(100, 80); HeightTextBox.Size = new DrawingSize(160, 20); HeightTextBox.Text = "2.5"; this.Controls.Add(HeightTextBox); // 角度标签和文本框 Label angleLabel = new Label(); angleLabel.Text = "角度:"; angleLabel.Location = new DrawingPoint(20, 110); angleLabel.Size = new DrawingSize(80, 20); this.Controls.Add(angleLabel); AngleTextBox = new TextBox(); AngleTextBox.Location = new DrawingPoint(100, 110); AngleTextBox.Size = new DrawingSize(160, 20); AngleTextBox.Text = "0"; this.Controls.Add(AngleTextBox); // 确认按钮 Button okButton = new Button(); okButton.Text = "确认"; okButton.Location = new DrawingPoint(80, 150); okButton.Size = new DrawingSize(75, 25); okButton.DialogResult = DialogResult.OK; okButton.Click += (sender, e) => { CreateSingleText(); this.DialogResult = DialogResult.OK; this.Close(); }; this.Controls.Add(okButton); // 取消按钮 Button cancelButton = new Button(); cancelButton.Text = "取消"; cancelButton.Location = new DrawingPoint(165, 150); cancelButton.Size = new DrawingSize(75, 25); cancelButton.DialogResult = DialogResult.Cancel; cancelButton.Click += (sender, e) => { this.DialogResult = DialogResult.Cancel; this.Close(); }; this.Controls.Add(cancelButton); this.AcceptButton = okButton; this.CancelButton = cancelButton; } private void CreateSingleText() { Document doc = AcApplication.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { // 获取插入点 PromptPointResult ppr = ed.GetPoint("\n指定插入点: "); if (ppr.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; // 创建单行文字 DBText text = new DBText(); text.Position = ppr.Value; text.TextString = ContentTextBox.Text; text.Height = double.Parse(HeightTextBox.Text); text.Rotation = double.Parse(AngleTextBox.Text) * Math.PI / 180.0; btr.AppendEntity(text); tr.AddNewlyCreatedDBObject(text, true); tr.Commit(); } ed.WriteMessage("\n单行文字创建成功。"); } catch (System.Exception ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } } } public class MultiTextForm : Form { public TextBox ContentTextBox; public ComboBox StyleComboBox; public TextBox HeightTextBox; public MultiTextForm() { InitializeComponent(); } private void InitializeComponent() { this.Text = "多行文字"; this.Size = new DrawingSize(400, 350); this.StartPosition = FormStartPosition.CenterScreen; // 文字内容标签和文本框 Label contentLabel = new Label(); contentLabel.Text = "文字内容:"; contentLabel.Location = new DrawingPoint(20, 20); contentLabel.Size = new DrawingSize(80, 20); this.Controls.Add(contentLabel); ContentTextBox = new TextBox(); ContentTextBox.Multiline = true; ContentTextBox.Location = new DrawingPoint(20, 50); ContentTextBox.Size = new DrawingSize(350, 150); ContentTextBox.ScrollBars = ScrollBars.Vertical; this.Controls.Add(ContentTextBox); // 文字样式标签和下拉框 Label styleLabel = new Label(); styleLabel.Text = "文字样式:"; styleLabel.Location = new DrawingPoint(20, 210); styleLabel.Size = new DrawingSize(80, 20); this.Controls.Add(styleLabel); StyleComboBox = new ComboBox(); StyleComboBox.Location = new DrawingPoint(100, 210); StyleComboBox.Size = new DrawingSize(160, 20); StyleComboBox.Items.AddRange(new string[] { "Standard", "仿宋", "黑体", "宋体" }); StyleComboBox.SelectedIndex = 0; this.Controls.Add(StyleComboBox); // 字高标签和文本框 Label heightLabel = new Label(); heightLabel.Text = "字高:"; heightLabel.Location = new DrawingPoint(20, 240); heightLabel.Size = new DrawingSize(80, 20); this.Controls.Add(heightLabel); HeightTextBox = new TextBox(); HeightTextBox.Location = new DrawingPoint(100, 240); HeightTextBox.Size = new DrawingSize(160, 20); HeightTextBox.Text = "2.5"; this.Controls.Add(HeightTextBox); // 确认按钮 Button okButton = new Button(); okButton.Text = "确认"; okButton.Location = new DrawingPoint(120, 270); okButton.Size = new DrawingSize(75, 25); okButton.Click += (sender, e) => { CreateMultiText(); this.DialogResult = DialogResult.OK; this.Close(); }; this.Controls.Add(okButton); // 取消按钮 Button cancelButton = new Button(); cancelButton.Text = "取消"; cancelButton.Location = new DrawingPoint(205, 270); cancelButton.Size = new DrawingSize(75, 25); cancelButton.DialogResult = DialogResult.Cancel; this.Controls.Add(cancelButton); } private void CreateMultiText() { Document doc = AcApplication.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; try { PromptPointResult ppr = ed.GetPoint("\n指定插入点: "); if (ppr.Status != PromptStatus.OK) return; using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; MText mtext = new MText(); mtext.Location = ppr.Value; mtext.Contents = ContentTextBox.Text; mtext.Height = double.Parse(HeightTextBox.Text); mtext.Width = 300; btr.AppendEntity(mtext); tr.AddNewlyCreatedDBObject(mtext, true); tr.Commit(); } ed.WriteMessage("\n多行文字创建成功。"); } catch (System.Exception ex) { ed.WriteMessage($"\n错误: {ex.Message}"); } } } public class ElevationForm : Form { public ElevationForm() { InitializeComponent(); } private void InitializeComponent() { this.Text = "标高标注"; this.Size = new DrawingSize(300, 200); this.StartPosition = FormStartPosition.CenterScreen; Label elevationLabel = new Label(); elevationLabel.Text = "楼层标高:"; elevationLabel.Location = new DrawingPoint(20, 20); elevationLabel.Size = new DrawingSize(80, 20); this.Controls.Add(elevationLabel); TextBox elevationTextBox = new TextBox(); elevationTextBox.Location = new DrawingPoint(100, 20); elevationTextBox.Size = new DrawingSize(160, 20); elevationTextBox.Text = "±0.000"; this.Controls.Add(elevationTextBox); Button okButton = new Button(); okButton.Text = "确认"; okButton.Location = new DrawingPoint(80, 120); okButton.Size = new DrawingSize(75, 25); okButton.DialogResult = DialogResult.OK; this.Controls.Add(okButton); Button cancelButton = new Button(); cancelButton.Text = "取消"; cancelButton.Location = new DrawingPoint(165, 120); cancelButton.Size = new DrawingSize(75, 25); cancelButton.DialogResult = DialogResult.Cancel; this.Controls.Add(cancelButton); } } // 其他窗体类 public class LeaderForm : Form { } public class NotationForm : Form { } public class SectionForm : Form { } public class RevisionCloudForm : Form { } public class BreakLineForm : Form { } public class TitleForm : Form { } #endregion #region 工具类实现 public static class TextMerger { public static void MergeSelectedTexts(SelectionSet selection) { if (selection == null || selection.Count == 0) return; Document doc = AcApplication.DocumentManager.MdiActiveDocument; Database db = doc.Database; Editor ed = doc.Editor; List<DBText> dbTexts = new List<DBText>(); List<MText> mTexts = new List<MText>(); using (Transaction tr = db.TransactionManager.StartTransaction()) { foreach (SelectedObject so in selection) { if (so != null && so.ObjectId != ObjectId.Null) { Entity ent = tr.GetObject(so.ObjectId, OpenMode.ForRead) as Entity; if (ent is DBText) { dbTexts.Add(ent as DBText); } else if (ent is MText) { mTexts.Add(ent as MText); } } } tr.Commit(); } if (dbTexts.Count == 0 && mTexts.Count == 0) { ed.WriteMessage("\n未选中任何文字对象!"); return; } string mergedText = ""; var allTextObjects = new List<ITextObject>(); foreach (var text in dbTexts) { allTextObjects.Add(new TextObjectAdapter(text)); } foreach (var text in mTexts) { allTextObjects.Add(new TextObjectAdapter(text)); } allTextObjects.Sort(new TextObjectComparer()); foreach (var textObj in allTextObjects) { if (!string.IsNullOrEmpty(mergedText)) mergedText += Environment.NewLine; mergedText += textObj.Text; } Point3d insertionPoint = Point3d.Origin; if (allTextObjects.Count > 0) { insertionPoint = allTextObjects[0].Position; } using (Transaction tr = db.TransactionManager.StartTransaction()) { BlockTable bt = tr.GetObject(db.BlockTableId, OpenMode.ForRead) as BlockTable; BlockTableRecord btr = tr.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite) as BlockTableRecord; MText mergedMText = new MText(); mergedMText.Location = insertionPoint; mergedMText.Contents = mergedText; if (allTextObjects.Count > 0) { mergedMText.TextStyleId = allTextObjects[0].TextStyleId; mergedMText.Height = allTextObjects[0].Height; } mergedMText.Width = 300; btr.AppendEntity(mergedMText); tr.AddNewlyCreatedDBObject(mergedMText, true); foreach (var text in dbTexts) { Entity ent = tr.GetObject(text.ObjectId, OpenMode.ForWrite) as Entity; ent.Erase(); } foreach (var text in mTexts) { Entity ent = tr.GetObject(text.ObjectId, OpenMode.ForWrite) as Entity; ent.Erase(); } tr.Commit(); } ed.WriteMessage("\n文字合并完成。"); } private interface ITextObject { string Text { get; } Point3d Position { get; } ObjectId TextStyleId { get; } double Height { get; } } private class TextObjectAdapter : ITextObject { private DBText _dbText; private MText _mText; private bool _isDBText; public TextObjectAdapter(DBText dbText) { _dbText = dbText; _isDBText = true; } public TextObjectAdapter(MText mText) { _mText = mText; _isDBText = false; } public string Text { get { return _isDBText ? _dbText.TextString : _mText.Contents; } } public Point3d Position { get { return _isDBText ? _dbText.Position : _mText.Location; } } public ObjectId TextStyleId { get { return _isDBText ? _dbText.TextStyleId : _mText.TextStyleId; } } public double Height { get { return _isDBText ? _dbText.Height : _mText.Height; } } } private class TextObjectComparer : IComparer<ITextObject> { public int Compare(ITextObject x, ITextObject y) { if (x.Position.Y > y.Position.Y) return -1; if (x.Position.Y < y.Position.Y) return 1; if (x.Position.X < y.Position.X) return -1; if (x.Position.X > y.Position.X) return 1; return 0; } } } #endregion } CS0012类型“Point”在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point”在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b035f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型”Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point”在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point”在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型”Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f1d50a3a"的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a"的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集"System.Drawing,Version=4.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"的引用。 CS0012类型“Point"在未引用的程序集中定义。必须添加对程序集”System.Drawing,Version=4.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"的引用。 CS0234命名空间“System.Drawing”中不存在类型或命名空间名“Size”(是否缺少程序集引用?) CS0234命名空间“System.Drawing”中不存在类型或命名空间名“Point(是否缺少程序集引用?) CS0579“Guid”特性重复 CS0591“Guid”特性的参数值无效
最新发布
08-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值