Click here to Skip to main content
15,844,155 members
Home / Discussions / C#
   

C#

 
AnswerRe: Sent from one computer to another keystroke Pin
Dave Kreskowiak16-Aug-15 5:30
mveDave Kreskowiak16-Aug-15 5:30 
Questionc++ Qt or C# or JAVA !! Pin
Dreamer_X15-Aug-15 4:51
Dreamer_X15-Aug-15 4:51 
AnswerRe: c++ Qt or C# or JAVA !! Pin
OriginalGriff15-Aug-15 4:54
mveOriginalGriff15-Aug-15 4:54 
GeneralRe: c++ Qt or C# or JAVA !! Pin
Nish Nishant15-Aug-15 6:18
sitebuilderNish Nishant15-Aug-15 6:18 
GeneralRe: c++ Qt or C# or JAVA !! Pin
Pete O'Hanlon15-Aug-15 10:16
subeditorPete O'Hanlon15-Aug-15 10:16 
Questioncreating events for dynamically created button Pin
Member 1126181115-Aug-15 1:48
Member 1126181115-Aug-15 1:48 
AnswerRe: creating events for dynamically created button Pin
OriginalGriff15-Aug-15 2:19
mveOriginalGriff15-Aug-15 2:19 
AnswerRe: creating events for dynamically created button Pin
BillWoodruff15-Aug-15 3:58
professionalBillWoodruff15-Aug-15 3:58 
Hi Antony, and welcome to CodeProject !

I think you are using a strategy that is far too complex here.

Why not create one UserControl that has both a Label, and TextBox; then create multiple instances of that UserControl as needed ?

In the UserControl try setting the AutoSize property to 'true on the Label, and its Dock property to 'Left; set the TextBox 'Dock property to 'Fill. That way you can avoid the business your doing now to measure the width of the string.

Within the UserControl, you can expose the TextBox by creating a Public property that references the contents of the TextBox, or exposing the TextBox TextChanged Event.

I like to use "lazy-load" techniques, so I might write something like this in the UserControl:
C#
private string _currentText;

public string CurrentText
{
    set
    {
        _currentText = value;
        textBox1.Text = value;
    }
    get { return textBox1.Text; }
}

As you create the instances of the UserControl, you can, at the same time, create a generic List or Array to store either the TextBox itself or its Text data. Then, when your Button is pressed, you can just iterate over the reference to stored values, or data, and get what you need.

You could use Linq to make an IEnumerable<string>, containing all the strings in all the TextBoxes, or whatever else you need. Assuming I used the string Property shown above, and I kept a List named 'LblTbList<LabelAndTextUserControl> of the Custom User Controls in the Form where they created"
C#
public IEnumerable<string> GetData()
{
    return LblTbList.Select(itm => itm.CurrentText);
}
That function would return an IEnumerable containing all the strings in all the TextBoxes.

And the code in the context that "consumes" the data (the code your showing using 'ShowDialog) can then iterate over that, or whatever.
C#
private void button2_Click(object sender, EventArgs e)
{
    foreach (string txt in YourTestForm.GetData().ToList())
    {
        Console.WriteLine(txt);
    }
}
cheers, Bill
«I want to stay as close to the edge as I can without going over. Out on the edge you see all kinds of things you can't see from the center» Kurt Vonnegut.

QuestionTimeSpan Error?? Pin
Kevin Marois14-Aug-15 13:16
professionalKevin Marois14-Aug-15 13:16 
AnswerRe: TimeSpan Error?? Pin
Matt T Heffron14-Aug-15 15:16
professionalMatt T Heffron14-Aug-15 15:16 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 4:59
professionalKevin Marois17-Aug-15 4:59 
GeneralRe: TimeSpan Error?? Pin
Pete O'Hanlon17-Aug-15 5:24
subeditorPete O'Hanlon17-Aug-15 5:24 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 5:28
professionalKevin Marois17-Aug-15 5:28 
GeneralRe: TimeSpan Error?? Pin
Ravi Bhavnani17-Aug-15 6:12
professionalRavi Bhavnani17-Aug-15 6:12 
GeneralRe: TimeSpan Error?? Pin
Pete O'Hanlon17-Aug-15 6:15
subeditorPete O'Hanlon17-Aug-15 6:15 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 6:24
professionalKevin Marois17-Aug-15 6:24 
GeneralRe: TimeSpan Error?? Pin
Dr. John L. Aven31-Aug-15 18:16
Dr. John L. Aven31-Aug-15 18:16 
GeneralRe: TimeSpan Error?? Pin
Ravi Bhavnani17-Aug-15 6:24
professionalRavi Bhavnani17-Aug-15 6:24 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 6:25
professionalKevin Marois17-Aug-15 6:25 
GeneralRe: TimeSpan Error?? Pin
Ravi Bhavnani17-Aug-15 6:35
professionalRavi Bhavnani17-Aug-15 6:35 
GeneralRe: TimeSpan Error?? Pin
Matt T Heffron17-Aug-15 7:55
professionalMatt T Heffron17-Aug-15 7:55 
GeneralRe: TimeSpan Error?? Pin
Richard Deeming17-Aug-15 8:10
mveRichard Deeming17-Aug-15 8:10 
QuestionWhy don't I see a VSTO template Pin
Gary Huck14-Aug-15 10:47
Gary Huck14-Aug-15 10:47 
AnswerRe: Why don't I see a VSTO template Pin
Richard MacCutchan14-Aug-15 21:49
mveRichard MacCutchan14-Aug-15 21:49 
Question(Solved) How to send a message from a rich text box control to a form Pin
Leif Simon Goodwin14-Aug-15 1:56
Leif Simon Goodwin14-Aug-15 1: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.