Click here to Skip to main content
15,867,330 members
Home / Discussions / C#
   

C#

 
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 
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 

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.