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

C#

 
AnswerRe: Sent from one computer to another keystroke Pin
Dave Kreskowiak16-Aug-15 4:30
mveDave Kreskowiak16-Aug-15 4:30 
Questionc++ Qt or C# or JAVA !! Pin
Dreamer_X15-Aug-15 3:51
Dreamer_X15-Aug-15 3:51 
AnswerRe: c++ Qt or C# or JAVA !! Pin
OriginalGriff15-Aug-15 3:54
mveOriginalGriff15-Aug-15 3:54 
GeneralRe: c++ Qt or C# or JAVA !! Pin
Nish Nishant15-Aug-15 5:18
sitebuilderNish Nishant15-Aug-15 5:18 
GeneralRe: c++ Qt or C# or JAVA !! Pin
Pete O'Hanlon15-Aug-15 9:16
mvePete O'Hanlon15-Aug-15 9:16 
Questioncreating events for dynamically created button Pin
Member 1126181115-Aug-15 0:48
Member 1126181115-Aug-15 0:48 
AnswerRe: creating events for dynamically created button Pin
OriginalGriff15-Aug-15 1:19
mveOriginalGriff15-Aug-15 1:19 
AnswerRe: creating events for dynamically created button Pin
BillWoodruff15-Aug-15 2:58
professionalBillWoodruff15-Aug-15 2: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 12:16
professionalKevin Marois14-Aug-15 12:16 
AnswerRe: TimeSpan Error?? Pin
Matt T Heffron14-Aug-15 14:16
professionalMatt T Heffron14-Aug-15 14:16 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 3:59
professionalKevin Marois17-Aug-15 3:59 
GeneralRe: TimeSpan Error?? Pin
Pete O'Hanlon17-Aug-15 4:24
mvePete O'Hanlon17-Aug-15 4:24 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 4:28
professionalKevin Marois17-Aug-15 4:28 
GeneralRe: TimeSpan Error?? Pin
Ravi Bhavnani17-Aug-15 5:12
professionalRavi Bhavnani17-Aug-15 5:12 
GeneralRe: TimeSpan Error?? Pin
Pete O'Hanlon17-Aug-15 5:15
mvePete O'Hanlon17-Aug-15 5:15 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 5:24
professionalKevin Marois17-Aug-15 5:24 
GeneralRe: TimeSpan Error?? Pin
Dr. John L. Aven31-Aug-15 17:16
Dr. John L. Aven31-Aug-15 17:16 
GeneralRe: TimeSpan Error?? Pin
Ravi Bhavnani17-Aug-15 5:24
professionalRavi Bhavnani17-Aug-15 5:24 
GeneralRe: TimeSpan Error?? Pin
Kevin Marois17-Aug-15 5:25
professionalKevin Marois17-Aug-15 5:25 
GeneralRe: TimeSpan Error?? Pin
Ravi Bhavnani17-Aug-15 5:35
professionalRavi Bhavnani17-Aug-15 5:35 
GeneralRe: TimeSpan Error?? Pin
Matt T Heffron17-Aug-15 6:55
professionalMatt T Heffron17-Aug-15 6:55 
GeneralRe: TimeSpan Error?? Pin
Richard Deeming17-Aug-15 7:10
mveRichard Deeming17-Aug-15 7:10 
QuestionWhy don't I see a VSTO template Pin
Gary Huck14-Aug-15 9:47
Gary Huck14-Aug-15 9:47 
AnswerRe: Why don't I see a VSTO template Pin
Richard MacCutchan14-Aug-15 20:49
mveRichard MacCutchan14-Aug-15 20:49 
Question(Solved) How to send a message from a rich text box control to a form Pin
Leif Simon Goodwin14-Aug-15 0:56
Leif Simon Goodwin14-Aug-15 0: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.