使用C#将新价格添加到旧价格并将结果保存在文本框中 [英] Adding new prices to old one and keeping the result in a textbox using C#

查看:75
本文介绍了使用C#将新价格添加到旧价格并将结果保存在文本框中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谢谢你们.我刚刚发现了我的问题.
我宣布十进制价格= .00m;
对于要解析的txtTotalSales.Text,.00成为问题.非常感谢你们到目前为止向我展示的合作.

现在进入下一个级别,这是关于我之前提出的第二个问题.请检查下面的代码,然后告诉我该怎么做.


****************************************************** *************
****************************************************** *******************

< pre>密封的类SerialPortDataRead//用于串行端口通信
{

//使用基本设置创建串行端口

私有只读SerialPort端口=新的SerialPort("COM5",9600,Parity.None,8,StopBits.One);
私有只读Form1 form1;

公共SerialPortDataRead(Form1 form1)
{
//TODO:完整的成员初始化
this.form1 = form1;
}

公共SerialPortDataRead()
{
//TODO:完整的成员初始化

}

私有无效Serial()
{
//发出默认的系统声音以指示传入的数据
SystemSounds.Beep.Play();

//当端口的缓冲区中有数据等待时,附加一个要调用的方法

port.DataReceived + = port_DataReceived;

//开始交流

port.Open();

//输入一个应用程序循环以使该线程保持活动状态

Application.Run();

}

私有无效port_DataReceived(对象发送者,SerialDataReceivedEventArgs e)
{//将传入的数据排列在数组中

string [] PortVal = {port.ReadExisting()};

double []检查=新的double [6]
{120,120,100,100,150,100};//这是价格数组

字符串[] SecondMenuList =新字符串[6]
{螺丝驱动器",胶带",胶水",标记",卤素"}}///这是主数组

字符串FirstSelection =";
字符串值=";
使用(Form1 frm = new Form1())
{
string [] c = {"1","2","3","4"};
对于(int i = 0; i& lt; 3; i ++)
如果(PortVal [i] == SecondMenuList [i])//这是问题所在


//我只想比较
PortVal中每个元素的前三个字母
//具有
中元素的前三个字母的任何索引
//SecondMenulist数组.但我只能比较元素

//请问我该怎么办?

{
FirstSelection + = String.Format("{0} \ t:{1:c} \ r \ n",SecondMenuList [i],Check [i]);
值= Check [i] .ToString("c");
如果(PortVal.Contains(c [i]))
{


//比较之后,如果有匹配项,则添加匹配的元素

//在SecondMenuList数组中到DataGrid表(名为Table)

this.form1._names.Add("TABLE");
this.form1._dataArray.Add(new string [] {c [i]});
}
this.form1._names.Add("ITEM");
this.form1._dataArray.Add(new string [] {SecondMenuList [i]});
this.form1._names.Add("PRICE");
this.form1._dataArray.Add(new string [] {Check [i] .ToString("c")});
}
frm.txtTotalSales.Text =值;
}
DateTime日期=新的DateTime();
日期= DateTime.Now;
PrinterHelper.SendStringToPrinter(String.Format(订单\ r \ n \ n \ t {0} \ t:{1} \ r \ n \ n \ t {2}",
FirstSelection,
值,
日期));

}
}

公共Form1()
{
InitializeComponent();


dataGridView1.DataSource = GetResultsTable();

}

公共Form1(TextBox txtTotalSales)
{
//TODO:完整的成员初始化
this.txtTotalSales = txtTotalSales;
}

私有静态SerialPortDataRead port_DataReceived()
{
抛出新的NotImplementedException();
}

公共DataTable GetResultsTable()
{
DataTable Table = new DataTable();
for(int i = 0; i& lt; this._dataArray.Count; i ++)
{
字符串名称= this._names [i];
Table.Columns.Add(name);
List& lt; object& gt; objectNumbers =新的List& amp; lt; object& gt;();
foreach(this._dataArray [i]中的字符串号)
{
objectNumbers.Add((object)number);
}
while(Table.Rows.Count& lt; objectNumbers.Count)
{
Table.Rows.Add();
}
for(int a = 0; a& amp; lt; objectNumbers.Count; a ++)
{
Table.Rows [a] [i] = objectNumbers [a];
}
}
返回表;
}
</pre>
首先,您需要保持TotalPrice不是字符串,而是一些形式的整数:int,float,decimal-其中之一可能是你要.然后,您可以将新订购的商品价格添加到其中,并适当地设置TextBox:

txtTotalPrice.Text = TotalPrice.ToString();



其次,您需要更详细地说明您要尝试做的事情-我无法从该描述中得出您要达到的目的.


您好,亲爱的
抱歉,我阅读了您的问题,但我听不懂,我认为这是一个非常非常简单的问题,如果您发送更完整的问题和代码,我可以轻松回答您的问题


您的信息仍然令人困惑,尽管我还是给了一个机会:

价格+ =支票[i];
//  TotalPrice = Price.ToString("c"); //现在似乎变得不必要了
// 在文本框中解析价格
 double  dOldTotalPrice =  Double  .Parse(txtTotalPrice.TextTotalPrice);
// 将新价格添加到旧总价中
 double  dNewTotalPrice = dOldTotalPrice +价格; // 假设价格是一个数字?
// 将价格打印到文本框
txtTotalPrice.Text = dNewTotalPrice.ToString(" )




我想您必须对此稍作调整,但我希望它有助于将您推向正确的方向.后续问题当然是可以接受的(如果您有新代码,请在此下面添加注释以回答快速问题,如果您有新代码,请编辑原始问题并发表对此问题的评论,以便我得到通知.)


Thanks Guys. i just discovered my problem.
i Declared decimal Price = .00m;
for the txtTotalSales.Text to be parsed, the .00 becomes a problem. am just grateful for the coperation u guys have shown me so far.

Now am unto the next level and this is about the second question i posed before. Please check the code below and tell me what to do.


***************************************************************
*********************************************************************

<pre>sealed class SerialPortDataRead//for serial port communication
{

// Create the serial port with basic settings

private readonly SerialPort port = new SerialPort("COM5", 9600, Parity.None, 8, StopBits.One);
private readonly Form1 form1;

public SerialPortDataRead(Form1 form1)
{
// TODO: Complete member initialization
this.form1 = form1;
}

public SerialPortDataRead()
{
// TODO: Complete member initialization

}

private void Serial()
{
//beep the default system sound to indicate an incoming data
SystemSounds.Beep.Play();

// Attach a method to be called when there is data waiting in the port''s buffer

port.DataReceived += port_DataReceived;

// Begin communications

port.Open();

// Enter an application loop to keep this thread alive

Application.Run();

}

private void port_DataReceived(object sender, SerialDataReceivedEventArgs e)
{ // Arrange the incoming data in an array

string[] PortVal = { port.ReadExisting() };

double[] Check = new double[6]
{120, 120, 100, 100, 150, 100};//this is the price array

string[] SecondMenuList = new string[6]
{"Screw Driver", "Tape", "Glue", "Marker", "Halogen"};//this the mainarray

string FirstSelection = "";
string value = "";
using (Form1 frm = new Form1())
{
string[] c = { "1", "2", "3", "4" };
for (int i = 0; i &amp;lt; 3; i++)
if (PortVal[i] == SecondMenuList[i])//this is where the problem lies


// I want to only compare the first three letters of each elements in PortVal at

//any index with the first three letters of the elements in the

//SecondMenulist array. but i can only compare the elements

//what should i do here PLEASE?

{
FirstSelection += String.Format("{0}\t:{1:c}\r\n", SecondMenuList[i], Check[i]);
value = Check[i].ToString("c");
if (PortVal.Contains(c[i]))
{


//after the comparison, if there is any match, add the matched element

//in the SecondMenuList array to the DataGrid Table (named Table)

this.form1._names.Add("TABLE");
this.form1._dataArray.Add(new string[] { c[i] });
}
this.form1._names.Add("ITEM");
this.form1._dataArray.Add(new string[] { SecondMenuList[i] });
this.form1._names.Add("PRICE");
this.form1._dataArray.Add(new string[] { Check[i].ToString("c") });
}
frm.txtTotalSales.Text = value;
}
DateTime Date = new DateTime();
Date = DateTime.Now;
PrinterHelper.SendStringToPrinter(String.Format("Order(s)\r\n\n\t{0}\t:{1}\r\n\n\t{2}",
FirstSelection,
value,
Date));

}
}

public Form1()
{
InitializeComponent();


dataGridView1.DataSource = GetResultsTable();

}

public Form1(TextBox txtTotalSales)
{
// TODO: Complete member initialization
this.txtTotalSales = txtTotalSales;
}

private static SerialPortDataRead port_DataReceived()
{
throw new NotImplementedException();
}

public DataTable GetResultsTable()
{
DataTable Table = new DataTable();
for (int i = 0; i &amp;lt; this._dataArray.Count; i++)
{
string name = this._names[i];
Table.Columns.Add(name);
List&amp;lt;object&amp;gt; objectNumbers = new List&amp;lt;object&amp;gt;();
foreach (string number in this._dataArray[i])
{
objectNumbers.Add((object)number);
}
while (Table.Rows.Count &amp;lt; objectNumbers.Count)
{
Table.Rows.Add();
}
for (int a = 0; a &amp;lt; objectNumbers.Count; a++)
{
Table.Rows[a][i] = objectNumbers[a];
}
}
return Table;
}
</pre>
Thanks

解决方案

Firstly, you need to keep TotalPrice not as a string, but as a number of some form: int, float, decimal - one of those is probably what you want. You can then add the new ordered item price into it, and set the TextBox appropriately:

txtTotalPrice.Text = TotalPrice.ToString();



Secondly, you need to explain in better detail what you are trying to do - I can''t work out from that description what you are trying to achieve.


Hello Dear
Sorry,i read your question but i did not understand it,i think this is a very very easy problem , if you send a more complete question and code i can answer you easily


Your information is still confusing, I give it a shot though:

Price += Check[i];
//TotalPrice = Price.ToString("c"); //seems unnessecary now
//Parse the Price in the textbox
double dOldTotalPrice = Double.Parse(txtTotalPrice.TextTotalPrice);
//Add the new price to the old total
double dNewTotalPrice = dOldTotalPrice + Price; //Assuming Price is a number?
//Print the price into the TextBox
txtTotalPrice.Text = dNewTotalPrice.ToString("c")




I guess you have to adapt this a little, but I hope it helped push you in the right direction. Follow up questions are of course welcome (add a Comment under this for quick questions, if you have new code, please edit your original question and post a comment about it so I get notified).


这篇关于使用C#将新价格添加到旧价格并将结果保存在文本框中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆