我仍然是一个新手程序员(来自 C++),我正在创建一个 GIU,它显示来自正在运行的软件的标签。目的是显示标签属性Description
和Eng Units
. 我得到了 2 个 dll InTouchDataAccess 和 NDde。我读过异常处理,我见过的最好的想法是这个:
创建一个验证函数
我做了。但是程序没有进入函数,我直接进入我的SelectButton_Click
fct 的 catch 块。
标签浏览器.cs
using System;
using System.Windows.Forms;
using IOM.InTouchDataAccess;
namespace InTouchTagBrowser
{
public partial class InTouchTagBrowser : Form
{
public string tagName;
public string description;
public string engUnits;
public InTouchTagBrowser()
{
InitializeComponent();
}
private void TagBrowser_Load(object sender, EventArgs e)
{
}
private void SelectButton_Click(object sender, EventArgs e)
{
try
{
tagName = tagNameBox.Text;
InTouchDdeWrapper inTouchWrapper = new InTouchDdeWrapper();
inTouchWrapper.Initialize();
TagDotField tagDotField = new TagDotField(tagName);
string value = inTouchWrapper.Read(tagName);
if (EngValidate(inTouchWrapper.Read(tagDotField.EngUnits)) != 0)
{
engUnits = inTouchWrapper.Read(tagDotField.EngUnits);
}
else
{
engUnits = "N/A";
}
if (inTouchWrapper.Read(tagDotField.Description) != "")
{
description = inTouchWrapper.Read(tagDotField.Description);
}
else
{
description = "N/A";
}
descriptionlbl.Text = description;
englbl.Text = engUnits;
valuelbl.Text = value;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
MessageBox.Show(ex.Source);
MessageBox.Show(ex.HelpLink);
MessageBox.Show(ex.StackTrace);
}
}
private void WriteButton_Click(object sender, EventArgs e)
{
try
{
if (tagName == "")
{
MessageBox.Show("Please enter a tag!");
}
else
{
string inputValue = ValueBox.Text;
InTouchDdeWrapper inTouchWrapperWriter = new InTouchDdeWrapper();
inTouchWrapperWriter.Initialize();
TagDotField tagWriter = new TagDotField(inputValue);
inTouchWrapperWriter.Write(tagName, inputValue);
valuelbl.Text = inputValue;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Tag change successfull");
}
}
public int EngValidate(string engString)
{
string exception;
int x;
try
{
InTouchDdeWrapper inTouchWrapper = new InTouchDdeWrapper();
inTouchWrapper.Initialize();
TagDotField tagDotField = new TagDotField(tagName);
engString = inTouchWrapper.Read(tagDotField.EngUnits);
x = 1;
}
catch (Exception msg)
{
exception = msg.ToString();
if (exception == "")
x = 1;
else
x = 0;
}
return x;
}
}
}