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

C#

 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Foothill27-Feb-18 3:08
professionalFoothill27-Feb-18 3:08 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Eddy Vluggen27-Feb-18 3:12
professionalEddy Vluggen27-Feb-18 3:12 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Foothill27-Feb-18 3:31
professionalFoothill27-Feb-18 3:31 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Eddy Vluggen27-Feb-18 3:42
professionalEddy Vluggen27-Feb-18 3:42 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Eddy Vluggen27-Feb-18 5:23
professionalEddy Vluggen27-Feb-18 5:23 
AnswerRe: Finally! I found a valid use for the ref keyword Pin
Bohdan Stupak26-Feb-18 23:18
professionalBohdan Stupak26-Feb-18 23:18 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Foothill27-Feb-18 3:25
professionalFoothill27-Feb-18 3:25 
AnswerRe: Finally! I found a valid use for the ref keyword Pin
Gerry Schmitz27-Feb-18 6:15
mveGerry Schmitz27-Feb-18 6:15 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Richard Deeming27-Feb-18 6:30
mveRichard Deeming27-Feb-18 6:30 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Foothill27-Feb-18 6:32
professionalFoothill27-Feb-18 6:32 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Gerry Schmitz27-Feb-18 7:01
mveGerry Schmitz27-Feb-18 7:01 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
Richard Deeming27-Feb-18 7:45
mveRichard Deeming27-Feb-18 7:45 
GeneralRe: Finally! I found a valid use for the ref keyword Pin
phil.o27-Feb-18 13:22
professionalphil.o27-Feb-18 13:22 
AnswerRe: Finally! I found a valid use for the ref keyword Pin
Bernhard Hiller27-Feb-18 21:30
Bernhard Hiller27-Feb-18 21:30 
Question[Solved] How to force DataGridView.CellValidating fires when user mouse click somewhere else? Pin
Mars Lam25-Feb-18 22:26
Mars Lam25-Feb-18 22:26 
AnswerRe: How to force DataGridView.CellValidating fires when user mouse click somewhere else? Pin
Eddy Vluggen26-Feb-18 0:02
professionalEddy Vluggen26-Feb-18 0:02 
GeneralRe: How to force DataGridView.CellValidating fires when user mouse click somewhere else? Pin
Mars Lam26-Feb-18 16:38
Mars Lam26-Feb-18 16:38 
GeneralRe: How to force DataGridView.CellValidating fires when user mouse click somewhere else? Pin
Eddy Vluggen27-Feb-18 1:56
professionalEddy Vluggen27-Feb-18 1:56 
AnswerRe: How to force DataGridView.CellValidating fires when user mouse click somewhere else? Pin
Gerry Schmitz26-Feb-18 5:40
mveGerry Schmitz26-Feb-18 5:40 
QuestionBarcode scanner with MVC C# web Application. Pin
Member 1366174225-Feb-18 19:00
Member 1366174225-Feb-18 19:00 
AnswerRe: Barcode scanner with MVC C# web Application. Pin
OriginalGriff25-Feb-18 20:06
mveOriginalGriff25-Feb-18 20:06 
Questionhow to loop through textboxes? Pin
Member 1307073625-Feb-18 7:03
Member 1307073625-Feb-18 7:03 
AnswerRe: how to loop through textboxes? Pin
Maciej Los25-Feb-18 7:27
mveMaciej Los25-Feb-18 7:27 
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 

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.