C# 应用vs关闭,清洁方式?
在Close/Ok按钮事件中,我写了很多不同的变量。与将其复制/粘贴到Apply button事件相比,处理这两者的“最干净”解决方案是什么 唯一的实际区别是Close/Ok不执行Environment.exit。不过可能有将近50行代码。只是想用正确的方法 提前谢谢 充分披露:我一个月前才开始学习编程。我肯定我的代码有很多错误C# 应用vs关闭,清洁方式?,c#,C#,在Close/Ok按钮事件中,我写了很多不同的变量。与将其复制/粘贴到Apply button事件相比,处理这两者的“最干净”解决方案是什么 唯一的实际区别是Close/Ok不执行Environment.exit。不过可能有将近50行代码。只是想用正确的方法 提前谢谢 充分披露:我一个月前才开始学习编程。我肯定我的代码有很多错误 private void doneButton_Click(object sender, EventArgs e) { // actual end resul
private void doneButton_Click(object sender, EventArgs e)
{
// actual end result osnap and right click values
int findOvalue = 0;
int findMvalue = 0;
// OSNAP values
int endpoint = 1;
int midpoint = 2;
int center = 4;
int node = 8;
int quadrant = 16;
int intersection = 32;
int insertion = 64;
int perpendicular = 128;
int tangent = 256;
int nearest = 512;
int apparentIntersection = 2048;
int extension = 4096;
int parallel = 8192;
//int none = 0;
//int clearAll = 1024;
// find the OSNAP value
if (cbxEndpoint.Checked) { findOvalue += endpoint; }
if (cbxMidpoint.Checked) { findOvalue += midpoint; }
if (cbxCenter.Checked) { findOvalue += center; }
if (cbxNode.Checked) { findOvalue += node; }
if (cbxQuadrant.Checked) { findOvalue += quadrant; }
if (cbxIntersection.Checked) { findOvalue += intersection; }
if (cbxInsertion.Checked) { findOvalue += insertion; }
if (cbxPerpendicular.Checked) { findOvalue += perpendicular; }
if (cbxTangent.Checked) { findOvalue += tangent; }
if (cbxNearest.Checked) { findOvalue += nearest; }
if (cbxApparent.Checked) { findOvalue += apparentIntersection; }
if (cbxExtension.Checked) { findOvalue += extension; }
if (cbxParallel.Checked) { findOvalue += parallel; }
if (cbxNone.Checked) { findOvalue = 0; }
// Right Click values
int defaultmode = 1;
int editmode = 2;
int commandactive = 4;
int commandmode = 8;
int menumode = 16;
// find the right click value
if (cbxRcDefault.Checked) { findMvalue += defaultmode; }
if (cbxRcEdit.Checked) { findMvalue += editmode; }
if (cbxRcCommandActive.Checked) { findMvalue += commandactive; }
if (cbxRcCommand.Checked) { findMvalue += commandmode; }
if (cbxRcMenu.Checked) { findMvalue += menumode; }
// the value of the slider location. 3 to 100
int zfValue = tbZoomFactor.Value;
// file dialog
int fdval = 0;
if (cbxFileDialog.Checked) fdval = 1;
// pick first
int pfval = 0;
if (cbxPickFirst.Checked) pfval = 1;
// middle button pan
int mbval = 0;
if (cbxMbuttonPan.Checked) mbval = 1;
// attribute required
int attval = 0;
if (cbxAttRequired.Checked) attval = 1;
// paperspace background color
int psval = 0;
if (cbxPsBackground.Checked) psval = 256;
string nL = Environment.NewLine;
string ppValue = tbPromptPrefix.Text;
System.IO.StreamWriter file = new System.IO.StreamWriter(@"N:\C3D Support\MySettings.txt");
file.WriteLine("OSMODE," + findOvalue + nL + "SHORTCUTMENU," + findMvalue + nL + "ZOOMFACTOR," + zfValue + nL + "FILEDIA," + fdval + nL + "PICKFIRST," + pfval + nL + "MBUTTONPAN," + mbval + nL + "ATTREQ," + attval + nL + "BKGCOLORPS," + psval + nL + "CMDLNTEXT," + ppValue);
file.Close();
Environment.Exit(0);
}
重构变量写入两个处理程序中调用的另一个方法。重构变量写入两个处理程序中调用的另一个方法
private void doneButton_Click(object sender, EventArgs e) {
// call your new method here:
NameYourMethodSomething();
Environment.Exit(0);
}
private void otherButton_Click(object sender, EventArgs e) {
// call your method again:
NameYourMethodSomething();
Do_Something_Else_Whatever_You_Want_Here();
}
// put your stuffs here:
private void NameYourMethodSomething() {
// actual end result osnap and right click values
int findOvalue = 0;
int findMvalue = 0;
// OSNAP values
int endpoint = 1;
int midpoint = 2;
int center = 4;
int node = 8;
int quadrant = 16;
int intersection = 32;
int insertion = 64;
int perpendicular = 128;
int tangent = 256;
int nearest = 512;
int apparentIntersection = 2048;
int extension = 4096;
int parallel = 8192;
//int none = 0;
//int clearAll = 1024;
// find the OSNAP value
if (cbxEndpoint.Checked) { findOvalue += endpoint; }
if (cbxMidpoint.Checked) { findOvalue += midpoint; }
if (cbxCenter.Checked) { findOvalue += center; }
if (cbxNode.Checked) { findOvalue += node; }
if (cbxQuadrant.Checked) { findOvalue += quadrant; }
if (cbxIntersection.Checked) { findOvalue += intersection; }
if (cbxInsertion.Checked) { findOvalue += insertion; }
if (cbxPerpendicular.Checked) { findOvalue += perpendicular; }
if (cbxTangent.Checked) { findOvalue += tangent; }
if (cbxNearest.Checked) { findOvalue += nearest; }
if (cbxApparent.Checked) { findOvalue += apparentIntersection; }
if (cbxExtension.Checked) { findOvalue += extension; }
if (cbxParallel.Checked) { findOvalue += parallel; }
if (cbxNone.Checked) { findOvalue = 0; }
// Right Click values
int defaultmode = 1;
int editmode = 2;
int commandactive = 4;
int commandmode = 8;
int menumode = 16;
// find the right click value
if (cbxRcDefault.Checked) { findMvalue += defaultmode; }
if (cbxRcEdit.Checked) { findMvalue += editmode; }
if (cbxRcCommandActive.Checked) { findMvalue += commandactive; }
if (cbxRcCommand.Checked) { findMvalue += commandmode; }
if (cbxRcMenu.Checked) { findMvalue += menumode; }
// the value of the slider location. 3 to 100
int zfValue = tbZoomFactor.Value;
// file dialog
int fdval = 0;
if (cbxFileDialog.Checked) fdval = 1;
// pick first
int pfval = 0;
if (cbxPickFirst.Checked) pfval = 1;
// middle button pan
int mbval = 0;
if (cbxMbuttonPan.Checked) mbval = 1;
// attribute required
int attval = 0;
if (cbxAttRequired.Checked) attval = 1;
// paperspace background color
int psval = 0;
if (cbxPsBackground.Checked) psval = 256;
string nL = Environment.NewLine;
string ppValue = tbPromptPrefix.Text;
System.IO.StreamWriter file = new System.IO.StreamWriter(@"N:\C3D Support\MySettings.txt");
file.WriteLine("OSMODE," + findOvalue + nL + "SHORTCUTMENU," + findMvalue + nL + "ZOOMFACTOR," + zfValue + nL + "FILEDIA," + fdval + nL + "PICKFIRST," + pfval + nL + "MBUTTONPAN," + mbval + nL + "ATTREQ," + attval + nL + "BKGCOLORPS," + psval + nL + "CMDLNTEXT," + ppValue);
file.Close();
}
你为什么要否决这个问题?他/她是一名新程序员,有权向我们寻求帮助
你为什么要否决这个问题?他/她是一名新程序员,有权向我们寻求帮助发布您的代码。否则,这将很难提供帮助。为什么不将“实际代码”放在一个函数中,并从两个位置调用它呢?(我建议直接在事件处理程序中使用很少的代码。)为click event code编辑您得到了我的+1。不要因为-1而悲伤,在这里尝试更多,问你的问题。干杯,说出你的密码。否则,这将很难提供帮助。为什么不将“实际代码”放在一个函数中,并从两个位置调用它呢?(我建议直接在事件处理程序中使用很少的代码。)为click event code编辑您得到了我的+1。不要因为-1而悲伤,在这里尝试更多,问你的问题。干杯也许你可以包括这样一个小的代码示例:@CompuChip我觉得没有必要用一个如此简单的示例。是的,没错。很抱歉浪费了任何人的时间。也许你可以包括一个像这样的小代码示例:@CompuChip我觉得没有必要用一个如此简单的示例。是的,duh。很抱歉浪费了任何人的时间。谢谢你的时间,非常感谢。这应该更加明显。我仍然被缺少的分号/语法所困扰(@smakfactor1 u'r wellcomeThanks for your time Javad,非常感谢。这应该更明显。我仍然被缺少的分号/语法所困扰。:(@smakfactor1 u'r wellcome