Click here to Skip to main content
15,887,336 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to loop through textboxes? Pin
BillWoodruff25-Feb-18 8:46
professionalBillWoodruff25-Feb-18 8:46 
.... edit ...

Jim wrote:
Member 13070736 wrote:
that seems like way too much effort just to slot an array into textboxes.
It is an effort Smile | :) If you clarify what you are doing further, there may be simpler ways: are you using a database here ? As I sai to Luc:
Quote:
The need for a collection of TextBox Controls functioning as a kind of logical unit for inputting/validating a set of data is very common. I prefer to implement this in a UserControl (or Panel) that implements validation, provides options for either sequential entry, random entry, etc. ... or, in a Form shown as a Dialog.
... end edit ...

Sure, it's possible: there are several different ways. A key factor in choosing how to implement this is whether the values in your list will change, or, remain fixed.

Assuming they change, this is one technique:
C#
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace YourNameSpace
{
    public class MapIntsToTextBoxes : ObservableCollection<int>
    {
        public MapIntsToTextBoxes(params int[] ints)
        {
            // initialize the collection
            foreach (int i in ints)
            {
                this.Add(i);
            } 
        }
    }
}
Here's a usage example:
C#
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using YourNameSpace;

namespace YourWinFormProject
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private List<TextBox> TBoxes = new List<TBoxes>{
            textBox10,
            textBox11,
            textBox12,
            textBox13,
            textBox14
        };

        // construct and initialize
        private MapIntsToTextBoxes mapper = new MapIntsToTextBoxes(1,2,3,4,5);

        private void Form1_Load(object sender, System.EventArgs e)
        {
            mapper.CollectionChanged += MapperOnCollectionChanged;

            // change a vakue
            mapper[3] = 1000;
        }

        private void MapperOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            switch (e.Action)
            {
                case NotifyCollectionChangedAction.Replace:
                    TBoxes[e.OldStartingIndex].Text = e.NewItems[0].ToString();
                    break;
                case NotifyCollectionChangedAction.Add:
                    break;
                case NotifyCollectionChangedAction.Remove:
                    break;
                case NotifyCollectionChangedAction.Move:
                    break;
                case NotifyCollectionChangedAction.Reset:
                    break;
            }
        }
    }
}
There's a potential problem with this: if you expose an Observable Collection, remember that there's no way to cancel the other Actions, like Add, Remove. You can find some work-arounds for this, but, imho, they are complex.

And, there are other ways of achieving this including binding, but,in Win Forms, thee binding facility ... inferior to WPF, imho ... is something I avoid based on difficulties I've had with it: but, hey, it might work for you.
«... thank the gods that they have made you superior to those events which they have not placed within your own control, rendered you accountable for that only which is within you own control For what, then, have they made you responsible? For that which is alone in your own power—a right use of things as they appear.» Discourses of Epictetus Book I:12


modified 26-Feb-18 5:28am.

AnswerRe: how to loop through textboxes? Pin
Luc Pattyn25-Feb-18 12:33
sitebuilderLuc Pattyn25-Feb-18 12:33 
GeneralRe: how to loop through textboxes? Pin
OriginalGriff25-Feb-18 21:47
mveOriginalGriff25-Feb-18 21:47 
GeneralRe: how to loop through textboxes? Pin
Member 1307073625-Feb-18 22:06
Member 1307073625-Feb-18 22:06 
GeneralRe: how to loop through textboxes? Pin
OriginalGriff25-Feb-18 22:20
mveOriginalGriff25-Feb-18 22:20 
GeneralRe: how to loop through textboxes? Pin
BillWoodruff25-Feb-18 23:18
professionalBillWoodruff25-Feb-18 23:18 
GeneralRe: how to loop through textboxes? Pin
BillWoodruff25-Feb-18 23:02
professionalBillWoodruff25-Feb-18 23:02 
GeneralRe: how to loop through textboxes? Pin
Luc Pattyn26-Feb-18 3:09
sitebuilderLuc Pattyn26-Feb-18 3:09 
GeneralRe: how to loop through textboxes? Pin
OriginalGriff26-Feb-18 4:55
mveOriginalGriff26-Feb-18 4:55 
AnswerRe: how to loop through textboxes? Pin
jsc4227-Feb-18 9:42
professionaljsc4227-Feb-18 9:42 
QuestionC# - showing pdf files from mySQL database without using acrobat reader, how? Pin
Member 1365946625-Feb-18 6:38
Member 1365946625-Feb-18 6:38 
AnswerRe: C# - showing pdf files from mySQL database without using acrobat reader, how? Pin
Dave Kreskowiak25-Feb-18 8:03
mveDave Kreskowiak25-Feb-18 8:03 
AnswerRe: C# - showing pdf files from mySQL database without using acrobat reader, how? Pin
Gerry Schmitz25-Feb-18 14:30
mveGerry Schmitz25-Feb-18 14:30 
AnswerRe: C# - showing pdf files from mySQL database without using acrobat reader, how? Pin
sameer54925-Feb-18 22:14
sameer54925-Feb-18 22:14 
AnswerRe: C# - showing pdf files from mySQL database without using acrobat reader, how? Pin
Eddy Vluggen26-Feb-18 0:10
professionalEddy Vluggen26-Feb-18 0:10 
QuestionMessage Removed Pin
23-Feb-18 9:25
Member 1369343023-Feb-18 9:25 
QuestionMemory leak when converting Bitmap to BitmapImage Pin
Leif Simon Goodwin21-Feb-18 23:55
Leif Simon Goodwin21-Feb-18 23:55 
QuestionC# - How to find total rows affected when using EventLogReader (eventLogQuery)? Pin
Shanmuga Sundaram21-Feb-18 19:56
Shanmuga Sundaram21-Feb-18 19:56 
AnswerRe: C# - How to find total rows affected when using EventLogReader (eventLogQuery)? Pin
Gerry Schmitz22-Feb-18 11:53
mveGerry Schmitz22-Feb-18 11:53 
GeneralRe: C# - How to find total rows affected when using EventLogReader (eventLogQuery)? Pin
Shanmuga Sundaram22-Feb-18 19:22
Shanmuga Sundaram22-Feb-18 19:22 
QuestionIs it ok NOT to pass fields into the methods in the same class as parameters? Pin
User9874320-Feb-18 8:59
professionalUser9874320-Feb-18 8:59 
AnswerRe: Is it ok NOT to pass fields into the methods in the same class as parameters? Pin
Richard MacCutchan20-Feb-18 9:05
mveRichard MacCutchan20-Feb-18 9:05 
GeneralRe: Is it ok NOT to pass fields into the methods in the same class as parameters? Pin
User9874320-Feb-18 9:35
professionalUser9874320-Feb-18 9:35 
GeneralRe: Is it ok NOT to pass fields into the methods in the same class as parameters? Pin
Richard MacCutchan20-Feb-18 9:48
mveRichard MacCutchan20-Feb-18 9:48 
SuggestionRe: Is it ok NOT to pass fields into the methods in the same class as parameters? Pin
Richard Deeming20-Feb-18 10:00
mveRichard Deeming20-Feb-18 10:00 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.