.net - How can C# textbox values be written into a Typed Collection? -
my objective pass values windows form textbox control typed collection of type double can perform arithmetic operations on values in collection. eventually, convert doubles string , write them out messagebox. windows form has 3 buttons; submit, display, , close.
submit take values, such 4, 10.5, , write them typed collection. collection needs double. defined follows: list<double> dnumbers = new list<double>();
do need create collection of type string , cycle through values in textbox , write them string collection, , convert them collection of type double?
i'm sorry not have code share since syntactically incorrect , generates conversion errors. should zip application , attach it? appreciated.
i using .net version 4.5
thank replies. have no professional programming experience, , attempting learn .net c# , nuances thank patience. new stack overflow , learning q , protocol. here display event handler.
public void btndisplay_click(object sender, eventargs e) {
messagebox.show(convert.tostring(dnumbers.count), "dnumbers list count"); foreach (double dnum in dnumbers) { txtdisplayvalues.text += dnum + " "; } }
if want to: - enter in textbox double values 4.5 10.5 22.5 ans on - save each individual value in list of double values
do following: - split string in text box individual double values, using split method in regex class in
system.text.regularexpressions namespace. result in a
string array.
-loop through array, convert each string double , add list.
using system.text.regularexpressions; ....... private void submitbutton_click(object sender, routedeventargs e) { // step 1- split string after each blank space string[] numbersintextbox = regex.split(textbox.text, @" "); //step 2 - convert double , add list foreach (string strnumber in numbersintextbox) { double dnumber = convert.todouble(strnumber); dnumbers.add(dnumber); } }
Comments
Post a Comment