-1

This is a stand alone application, and the data doesn't need to be saved for a later time, and data will not be shared between users, it's for one user to input their data and carry out an assessment, the user can then note down the results, but none of this needs to be stored into the app for a later time.

It's a 2 stage assessment, the first stage requires the user to fill out a number of forms with structural details of columns, depending on how many columns they have, and the second stage needs to sum some of those column values and some will need to be averaged, to then display the final values of the assessment, onto a final form.

Only numbers are entered into each form, and results will also be in the form of numbers which are then displayed on a graph. There are around 30 text boxes of user input, which are input into a input form that pops of from a button from the parent form, stage one of the assessment is then carried out for each column on each parent form. Each parent form renderred onto a new tab using this EasyTabs solution Creating a C# Application with Chrome-Style Tabs using EasyTabs in WinForms - YouTube[^] that I found online.

I'm an absolute beginner on C# , so I couldn't figure out how I would take a value from each form and display the sum or average onto the final form, if I don't know how many forms there will be for each user during run time, as each user will have a different amount, I was thinking it's maybe some sort of loop during run time, but I'm just not sure what that would look like.

After speaking to a friend, they recommended a single form, with a save and refresh button, where the data gets saved onto a file, and then gets retrieved would be better, but there are say 10 different user input values that need to be picked up from each form, and then averaged or summed, I started learning about StreamReader and StreamWriter and how files work in C#, but it was really difficult to figure out how to lay out the data in the file, how to get C# to sum the correct values together etc etc.

What would be the best way to approach this problem?

Thank you for your help.

velKal
  • 17
  • 7

1 Answers1

0

If by "Form" you mean the WinForms definition of Form (Basically a new window), then I advise you to have a look at this post describing how to get data from one Form to the other.

To make a TextBox only accept numbers, you can use the a regular expression validation ^[0-9]*$ if you want to allow empty inputs or ^[0-9]+$ if you want at least one digit. if you want to allow negative numbers as well it is ^(\+|-)?[0-9]$. Note: all those will allow leading zeroes. For decimals you need to allow periods as well. ^(\+|-)?[0-9]*(\.[0-9]+)?$. Read up on regular expressions if you need anythin else. This website can help you design and test them.

To get a number from your text box you need to parse the text in the textbox. dotNet has plenty of functions for that already. For example Int32.TryParse() or if you want it to throw an exception Int32.Parse(). Others are similar. Have a look at the MSDN documentation to see how to use them.

As for averaging: Making a database, connecting to it and then using it to calculate the average of a number does seem like a pretty roundabout way of doing things. In programming you tell the computer what to do. So if someone told you to calculate an average, would you write the numbers in a book an then send them off to someone to calculate for you? Surely not.
You can just add all the numbers together in your code and divide by the amount of numbers. But there is an even easier solution. Add all your numbers to a List<int> and call it's .Sum() method and then divide by the return of it's .Count() method.

Saving the data to a file is only needed if you want to shut off the program between the user inputs or want to document the inputs.
Writing and reading numbers from a file is fairly straight forward though. Have a look at these tutorials provided by Microsoft: Write to file, Read from file, Read file one line at a Time. Writing to files uses the exact same methods as System.Console, just that you use a file stream instead of StdIO.

As for data layouts, there are several sane variants. For something simple you can just make your own (i.e. the position in the file dictates where the data goes, or each data point comes with a descriptor before it, for example inputA: 24). For more complex data, I would consider creating a class for your data and then serialize the data, for example using the popular Newtonsoft JSON library or XML and don't worry about how the file is structured.

FalcoGer
  • 2,278
  • 1
  • 12
  • 34
  • Thank you, I really appreciate your comprehensive answer, I have managed to get the values from one window to another, and I also agree that the database is a roundabout way of doing things. However because the number of windows "forms" for each user will be different, and so the number of values added to the list is not a set amount, I'm trying to figure out how to tell C# to go through each window, pick out the value from that window, add it to the list, find the average, and display that to a textbox on the a final window. – velKal Mar 24 '19 at 11:47
  • @Transcendent04 why would you want to open different windows if the user can only enter one value at a time anyway. your whole problem seems a bit like horrible UI design to me. What is the reason why you are so bent on having multiple windows? – FalcoGer Mar 24 '19 at 19:33
  • I agree, I think maybe my UI design is quite bad. But the reason is because each window (tab) is for a single column, the user enters data for that column this is around 20 textboxes of input and 30 textboxes of results on a window (tab) , and then calculations are done to carry out a local strength assessment for that column, the results are displayed on a graph on the same window. – velKal Mar 24 '19 at 20:15
  • Then a new tab is added by the user and the form is rendered on that tab and details for the next column can be input by the user, and so on, one the user has input all columns a global assessment is carried out on a separate "final" form for the whole floor of the building, which requires some values from each column form (tab). – velKal Mar 24 '19 at 20:16
  • I don't see the problem here. you have the text boxes, the user puts data in, and when they're done they press a button. you then fetch the data from the boxes and do whatever you do with it. If you need 500 text boxes, then you're either doing something wrong or you have a very weird problem. In any case, you get the value of a text box with `tbObj.Text` If you do have 500 text boxes then you don't get around getting 500 values in 500 lines. Well, if they are just a list of numbers for which it doesn't matter where they are, then a textbox is not the correct form element. – FalcoGer Mar 24 '19 at 20:19
  • My problem is the number of forms each user will have is different, and so the number of values from the textboxes will also be different for each user, I'm thinking make a list of forms that I can loop on a form list, and then also a list for the textbox data on each form in a datalist, and then find the final values from the lists, both lists grow during run time depending on number of forms. Or to write the data on a file, and then read the file for the data, put that into a list then use linq, but I'm struggling to find tutorials on this sort of thing, I'm not explaining well apologies.. – velKal Mar 24 '19 at 20:34
  • @Transcendent04 so you want to dynamically create form controls? or you could just disable those not applicable to the user if they're not vastly different. how about you make a class for your data and then display that in a list box and you add items to that list box with a button. – FalcoGer Mar 25 '19 at 07:22