c#数据窗口中就一个保存按钮,修改完数据后按保存按钮保存数据,如何编写?多谢!修改

???还是没明白

是直接输入表达式进行计算的?
第一种简单,无非就是做一些按钮并执行制定操作而已。
第二种我写了两份,分别使用正则表达式和语法分析技术
使用正则表达式的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using System.Drawing;

namespace Eval
{
public class EvalEventArgs : EventArgs
{
public EvalEventArgs(string Expressions)
{
this.Expressions = Expressions;
}
public string Expressions
public string Message
}

public class Function
{
static Regex Num = new Regex(@"(\-?\d+\.?\d*)");
static Regex Power = new Regex(Num.ToString() + @"\^" + Num.ToString());
static Regex AddSub = new Regex(Num.ToString() + "([+-])" + Num.ToString());
static Regex MulDiv = new Regex(Num.ToString() + "([*/])" + Num.ToString());
static Regex AndXor = new Regex(Num.ToString() + "(and|xor)" + Num.ToString());
static Regex InnerRegex = new Regex(@"\([^\(\)]+\)");
static Regex FunctionRegex = new Regex(@"(ln|lg|sin|cos|tan|ctg|sh|ch|th|arcsin|arccos|arctan|not)" +Num.ToString());
static Regex LBrackets = new Regex(@"\{|\[");
static Regex RBrackets = new Regex(@"\}|\]");
const double PI = Math.PI;
const double e = Math.E;

public event EventHandler<EvalEventArgs> Evaling;
public event EventHandler<EvalEventArgs> EvalComplent;
public event EventHandler<EvalEventArgs> Error;

private void OnError(string Expressions, string Message)
{
if (Error != null) Error(this, new EvalEventArgs(Expressions) );
IsAbort = true;
}

private bool IsAbort = false;

/// <summary>计算表达式</summary>
/// <param name="Expressions">表达式</param>
public void Eval(string Expressions)
{
IsAbort = false;
Expressions = Expressions.ToLower();
Expressions = Expressions.Replace("pi", Math.PI.ToString()).Replace("e",Math.E.ToString());
Expressions = LBrackets.Replace(Expressions, (Match match) => );
Expressions = RBrackets.Replace(Expressions, (Match match) => );
if (Check(Expressions))
{
try
{
do
{
Expressions = InnerRegex.Replace(Expressions, (Match match) =>
{
return EvalInner(match.Value.Substring(1, match.Value.Length - 2));
});
if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions));
}
while (InnerRegex.IsMatch(Expressions) && !IsAbort);
if(!IsAbort) Expressions = EvalInner(Expressions);
if (!IsAbort && EvalComplent != null) EvalComplent(this, new EvalEventArgs(Expressions));
}
catch(Exception ex)
{
OnError(Expressions, ex.Message);
}
}
}

/// <summary>
/// 用于计算不含括号的表达式的值
/// </summary>
/// <param name="Expressions">要计算的表达式</param>
/// <returns>计算结果</returns>
public string EvalInner(string Expressions)
{
while (FunctionRegex.IsMatch(Expressions) && !IsAbort)
{
Expressions = FunctionRegex.Replace(Expressions, (Match match) => );
if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions));
}

while (AndXor.IsMatch(Expressions) && !IsAbort)
{
Expressions = AndXor.Replace(Expressions, (Match match) =>
{
double P1=double.Parse(match.Groups[1].Value);
double P2=double.Parse(match.Groups[3].Value);
if((uint)P1!=P1 || (uint)P1!=P1 )
OnError(Expressions,"");
if (match.Value.Contains("and"))
return ((uint)P1 & (uint)P2).ToString();
else
return ((uint)P1 ^ (uint)P2).ToString();
});
if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions));
}

while (Power.IsMatch(Expressions) && !IsAbort)
{
Expressions = Power.Replace(Expressions, (Match match) =>
{
return System.Math.Pow(double.Parse(match.Groups[0].Value), double.Parse(match.Groups[2].Value)).ToString();
});
if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions));
}

while (MulDiv.IsMatch(Expressions) && !IsAbort)
{
Expressions = MulDiv.Replace(Expressions, (Match match) =>
{
if (match.Value.Contains("*"))
return (double.Parse(match.Groups[1].Value) * double.Parse(match.Groups[3].Value)).ToString();
else
return (double.Parse(match.Groups[1].Value) / double.Parse(match.Groups[3].Value)).ToString();
});
if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions));
}

while (AddSub.IsMatch(Expressions) && !IsAbort)
{
Expressions = AddSub.Replace(Expressions, (Match match) =>
{
if (match.Value.Contains("+"))
return (double.Parse(match.Groups[1].Value) + double.Parse(match.Groups[3].Value)).ToString();
else
return (double.Parse(match.Groups[1].Value) - double.Parse(match.Groups[3].Value)).ToString();
});
if (Evaling != null) Evaling(this, new EvalEventArgs(Expressions));
}

return IsAbort ? null : Expressions;
}

/// <summary>
/// 用于计算单变量函数表达式的值
/// </summary>
/// <param name="Expressions">要计算的表达式</param>
/// <returns>计算结果</returns>
public string EvalSingle(string Expressions)
{
while (FunctionRegex.IsMatch(Expressions) && !IsAbort)
{
Expressions = FunctionRegex.Replace(Expressions, (Match match) => );
}

return IsAbort ? null : Expressions;
}

private string EvalSingle(Match match)
{
//ln|lg|sin|cos|tan|ctg|sh|ch|th|arcsin|arccos|arctan
double Param = double.Parse(match.Groups[2].Value.Trim(char.Parse("("), char.Parse(")")));
double Result = double.NaN;
if (!double.IsNaN(Param))
{
if (match.Value.StartsWith("ln"))
{
if (Param > 0)
Result = System.Math.Log(Param);
else
OnError(match.Value, "表达式中有错误:对数的真数不能小于零!");
}
else if (match.Value.StartsWith("lg"))
{
if (Param > 0)
Result = System.Math.Log10(Param);
else
OnError(match.Value, "表达式中有错误:对数的真数不能小于零!");
}
else if (match.Value.StartsWith("sin"))
Result = System.Math.Sin(Param * PI / 180);
else if (match.Value.StartsWith("sqrt"))
Result = System.Math.Sqrt(Param);
else if (match.Value.StartsWith("cos"))
Result = System.Math.Cos(Param * PI / 180);
else if (match.Value.StartsWith("tan"))
Result = System.Math.Tan(Param * PI / 180);
else if (match.Value.StartsWith("ctg"))
Result = 1 / System.Math.Tan(Param * PI / 180);
else if (match.Value.StartsWith("sh"))
Result = System.Math.Sinh(Param);
else if (match.Value.StartsWith("ch"))
Result = System.Math.Cosh(Param);
else if (match.Value.StartsWith("th"))
Result = System.Math.Tanh(Param);
else if (match.Value.StartsWith("arcsin"))
Result = System.Math.Asin(Param);
else if (match.Value.StartsWith("arccos"))
Result = System.Math.Acos(Param);
else if (match.Value.StartsWith("arctan"))
Result = System.Math.Atan(Param);
else if (match.Value.StartsWith("not"))
{
if (((uint)Param) != Param)
OnError(match.Value, "逻辑运算只能针对整数!");
else
Result = ~(uint)Param;
}
else
OnError(match.Value,"表达式含有不被支持的函数!");
}
else
OnError(match.Value, "表达式中有错误!");
if (double.IsInfinity(Result)) OnError(match.Value, "结果超出了范围!");
return Result == double.NaN ? null : Result.ToString();
}

/// <summary>
/// 对表达式进行检查,确保其格式符合要求
/// </summary>
/// <param name="Expressions">表达式</param>
/// <returns>是否符合要求</returns>
public bool Check(string Expressions)
{
if (!CheckBrackets(Expressions))
{
OnError(Expressions, "括号不匹配!");
return false;
}
else
return true;
}

/// <summary>
/// 检查括号是否匹配
/// </summary>
/// <param name="Expressions">表达式</param>
/// <returns>是否匹配</returns>
private bool CheckBrackets(string Expressions)
{
int Num = 0;
foreach (char c in Expressions)
{
if (c == char.Parse("("))
if (c == char.Parse(")"))
}
if (Num != 0) else
}
}
}

Evaling事件可以获取中间结果
EvalComplent事件表明计算完成
Error事件表明发生错误
调用方法:
Function f = new Function();
f.Eval(要计算的表达式)

语法分析的比较长,想要的话留下邮箱我发过去
温馨提示:内容为网友见解,仅供参考
第1个回答  2011-03-05
C#可视化编程,将数据源对象拖到窗口就行,导航条中自带保存按钮和查询更新的各种按钮,都已配好代码,不须再手工添加。如果数据库表有图像列,需要添加照片等图形,倒是应该加一个图形搜索按钮,并配以事件处理程序,具体做法参见《C#编程指南》,清华大学出版社,2011年1月出版,Google或百度输入书名出版社,可网购。追问

我不想用导航条中自带保存按钮和查询更新的各种按钮!!!多谢!

追答

不好意思,这两天有点事,未能及时回答。
保存按钮的事件处理程序如下:
private void saveButton_Click(object sender, EventArgs e)
{
//验证窗体中的各控件的值是否合法,若不合法将抛出异常
this.Validate();
//将所有挂起的更改应用于DataSet数据源
this.bindingSource1.EndEdit();
//将更新解析回数据库
this.tableAdapterManager.UpdateAll(this.northwndDataSet1);
}
此段代码是《C#编程指南》第15章例15-4中的一部分,此例用手工编写类型化DataSet绑定数据到控件,手工添加添加保存按钮,若需要此例的代码和说明,清添加追问。

本回答被网友采纳
第2个回答  2011-03-05
用update更新数据库
用insert语句插入数据库追问

是的,也能插入,也能修改,但按下保存按钮,需要重新连接数据源,如上面编码,数据是修改前的?多谢!最好在我的编码基础上修改,多谢!!!