[Year 12 SofDev] C# - Adding data to an Array from user input

Hem, Lony L hem.lony.l at edumail.vic.gov.au
Thu Feb 9 19:04:22 AEDT 2017


Hi Michael,

I can’t say what is right or wrong but personally I do tend to find input box popping up from a loop consecutively very annoying. Another way this could be implemented is to use one textbox, one add button and a ListBox.

Code as follows

        List<int> lst = new List<int>();//global list of int
        private void btnAddItem_Click_1(object sender, EventArgs e){
            lst.Add(int.Parse(txtInput.Text));//add to list
            txtInput.Text = ""; //clear for next input
            //listBox display should always reflect the array
            lstToListBox(lst, ref lstDisplay);
        }
        //clear and repropagates listbox display
        private void lstToListBox(List<int> lst, ref ListBox listBox) {
            listBox.Items.Clear();
            foreach (int item in lst)
              listBox.Items.Add(item.ToString());
        }

Some disadvantages with the lstToListBox procedure, not efficient as it keeps clearing and re-adding old stuff on call which is redundant and refreshes can be noticeable when lists are sufficiently large. Advantage: well could easily replace the procedure for another display type and not need to worry about discrepancies in the list and listbox. Also can reuse the procedure in other buttons like delete etc. Hope that helped.

Regards,
Lony

From: sofdev-bounces at edulists.com.au [mailto:sofdev-bounces at edulists.com.au] On Behalf Of Cooper, Michael J
Sent: Monday, 6 February 2017 12:58 PM
To: Year 12 Software Development Teachers' Mailing List <sofdev at edulists.com.au>
Subject: [Year 12 SofDev] C# - Adding data to an Array from user input

Hello fellow SD teachers,

C# coding question - what is the best way to allow a USER to enter data into an array using a GUI, not the console?

I’ve used InputBoxes and loops with Visual Basic, however InputBoxes don’t seem to be thing in C#, without adding the Microsoft.VisualBasic namepace.

I can add data to an array during design time easily enough, but I want the USER to be able to click on a button and be prompted to enter data, which then populates the array.

Or am I going about this the wrong way entirely and should be using another technique?

Michael Cooper
ICT Co-ordinator & Network Support

[id:image001.png at 01D2440D.0D6FCCA0]
braybrooksc.vic.edu.au<http://www.Braybrooksc.vic.edu.au>
Ph. 9312 2900


IMPORTANT - This email and any attachments may be confidential. If received in error, please contact us and delete all copies. Before opening or using attachments check them for viruses and defects. Regardless of any loss, damage or consequence, whether caused by the negligence of the sender or not, resulting directly or indirectly from the use of any attached files our liability is limited to resupplying any affected attachments. Any representations or opinions expressed are those of the individual sender, and not necessarily those of the Department of Education and Training.



IMPORTANT - This email and any attachments may be confidential. If received in error, please contact us and delete all copies. Before opening or using attachments check them for viruses and defects. Regardless of any loss, damage or consequence, whether caused by the negligence of the sender or not, resulting directly or indirectly from the use of any attached files our liability is limited to resupplying any affected attachments. Any representations or opinions expressed are those of the individual sender, and not necessarily those of the Department of Education and Training.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.edulists.com.au/pipermail/sofdev/attachments/20170209/cde11fc8/attachment-0001.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.png
Type: image/png
Size: 9943 bytes
Desc: image001.png
Url : http://www.edulists.com.au/pipermail/sofdev/attachments/20170209/cde11fc8/image001-0001.png 


More information about the sofdev mailing list