2011-02-01 117 views
11

我有一個winforms應用程序,它在屏幕上有37個文本框。每一個按順序編號:在文本框中循環

DateTextBox0 
DateTextBox1 ... 
DateTextBox37 

我試圖通過文本框迭代和值分配給每一個:

int month = MonthYearPicker.Value.Month; 
int year = MonthYearPicker.Value.Year; 
int numberOfDays = DateTime.DaysInMonth(year, month); 

m_MonthStartDate = new DateTime(year, month, 1); 
m_MonthEndDate = new DateTime(year, month, numberOfDays); 

DayOfWeek monthStartDayOfWeek = m_MonthStartDate.DayOfWeek; 
int daysOffset = Math.Abs(DayOfWeek.Sunday - monthStartDayOfWeek); 

for (int i = 0; i <= (numberOfDays - 1); i++) 
{ 
//Here is where I want to loop through the textboxes and assign values based on the 'i' value 
    DateTextBox(daysOffset + i) = m_MonthStartDate.AddDays(i).Day.ToString(); 
} 

讓我澄清一下,這些文本框出現在獨立的面板(37他們)。所以爲了讓我循環使用foreach,我必須遍歷主要控件(面板),然後遍歷面板上的控件。它開始變得複雜。

有關如何將此值分配給文本框的任何建議?

+0

是否要分配「TextBox.Name」或「TextBox.Text」? – abatishchev 2011-02-01 13:29:41

+0

我想將Textbox.Text分配給名稱爲DateTextBox(daysoffset + i)的文本框。 – Taryn 2011-02-01 13:40:33

回答

32

要獲得所有的控制和子控制遞歸指定類型的,使用這個擴展的方法,:

public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control 
{ 
    var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>(); 
    return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children); 
} 

用法:

var allTextBoxes = this.GetChildControls<TextBox>(); 
foreach (TextBox tb in allTextBoxes) 
{ 
    tb.Text = ...; 
} 
4

可以遍歷在一個相當簡單的方式在表單中的文本框:

Func<ControlCollection, List<TextBox>> SearchTextBoxes = null; 
SearchTextBoxes = coll => { 
    List<TextBox> textBoxes = new List<TextBox>(); 

    foreach (Control c in coll) { 
     TextBox box = c as TextBox; 
     if (box != null) 
      textBoxes.Add(box); 
     if (c.Controls.Count > 0) 
      textBoxes.AddRange(SearchTextBoxes(c.Controls)); 
    } 

    return textBoxes; 
}; 

var tbs = SearchTextBoxes(this.Controls).OrderBy(tb => tb.Name); 

編輯:根據新的需求變更。幾乎沒有優雅的LINQ的解決方案,當然:)

+0

這將錯過任何不直接位於表單上的控件(例如面板,標籤頁或類似內容)。 – 2011-02-01 13:27:23

+0

我知道我可以通過控件循環,但此屏幕包含與DateTextBoxes以及另一個控件的37個面板。因此,爲了讓我獲得數據,我必須通過面板控件循環,然後通過面板中的控件來獲取數組。有沒有辦法讓我做到這一點,而不使用數組?只需將DateTextBoxes分配給它所需的數據? – Taryn 2011-02-01 13:29:26

0

調用InitialiseComponents()後,將文本框添加到表單上的集合成員變量。然後你可以按順序遍歷它們。

2

遍歷表單中的控件並檢查控件的名稱(如果匹配),然後根據需要設置Text屬性。

int i = 0; 
foreach (Control contrl in this.Controls) { 
    if (contrl.Name == ("DateTextBox" + i.ToString())) { 
     contrl.Text = "requiredtexttobeset"; 
    } 
    i = i + 1; 
} 
0

你可以像下面

Dictionary<TextBox, int> textBoxes = new Dictionary<TextBox, int>(); 

foreach (TextBox control in Controls.OfType<TextBox>()) 
    textBoxes[control] = Convert.ToInt32(control.Name.Substring(11)); 

現在..通過他們向循環創建TextBoxDictionaryint ..

foreach (var item in textBoxes.Select(p => new { textBox = p.Key, no = p.Value})) 
    item.textBox.Text = item.no.ToString(); // whatever you want... 

祝你好運!

6

如果是「文本框」,則可以循環顯示錶單中所有控件,並返回它們的完整列表。

public List GetTextBoxes(){ 
    var textBoxes = new List(); 
     foreach (Control c in Controls){ 
      if(c is TextBox){ 
       textBoxes.add(c); 
     } 
    } 
return textBoxes; 
} 
1

如果你想不「的foreach」做(如果你有特殊的箱子調整/地址)

int numControls = Page.Form.Controls.Count; 

    for (int i = 0; i < numControls; i++) 
    { 
     if (Page.Form.Controls[i] is TextBox) 
     { 
      TextBox currBox = Page.Form.Controls[i] as TextBox; 
      currbox.Text = currbox.TabIndex.ToString(); 
     } 
    } 
1
 //THE EASY WAY! Always post easy solutions. It's the best way. 
     //This code is used to loop through all textboxes on a form for data validation. 
     //If an empty textbox is found, Set the error provider for the appropriate textbox. 
     foreach (var control in Controls) 
     { 
      if (control is TextBox) 
      { 
       //Box the control into a textbox. Not really needed, but do it anyway 
       var textbox = (TextBox)control; 

       if (String.IsNullOrWhiteSpace(textbox.Text)) 
       { 
        //Set the errorProvider for data validation 
        errorProvider1.SetError(textbox, "Data Required!"); 
        textbox.Text = String.Empty; //Clear out the whitespace if necessary 
        //blnError = true; 
       } 
      } 
     } 
2

由於這個職位似乎是從一次復活自己的時間和自上述解決方案在控件內部找不到控件,例如在groupbox中,這會找到它們。只需添加您的控制類型:

public static IList<T> GetAllControls<T>(Control control) where T : Control 
    { 
     var lst = new List<T>(); 
     foreach (Control item in control.Controls) 
     { 
      var ctr = item as T; 
      if (ctr != null) 
       lst.Add(ctr); 
      else 
       lst.AddRange(GetAllControls<T>(item)); 

     } 
     return lst; 
    } 

而且它的使用:

 var listBoxes = GetAllControls<ListBox>(this); 
     foreach (ListBox lst in listBoxes) 
     { 
      //Do Something 
     } 
1

你可以簡單地這樣做伴侶......

foreach (TextBox txt in this.Panel.Controls.OfType<TextBox>()) 
      { 
       txt.Text="some value you assign"; 
      } 

如果你的文本框的形式直接與不在面板上,那麼您可以用this.Controls替換this.Panel.Controls。對於你來說,這應該很簡短明瞭。

0

其他答案只是不爲你削減它?
我發現這是對類似問題的回答,但我現在找不到線索。它通過位於control內的給定type的所有控件遞歸循環。所以包括...的孩子的孩子等。我的例子將TextBoxForeColor更改爲Hot Pink!

public IEnumerable<Control> GetAllControlsOfType(Control control, Type type) 
{ 
    var controls = control.Controls.Cast<Control>(); 

    return controls.SelectMany(ctrl => GetAllControlsOfType(ctrl, type)) 
           .Concat(controls) 
           .Where(c => c.GetType() == type); 
} 

實現:

IEnumerable<Control> allTxtBxs = GetAllControlsOfType(this, typeof(TextBox)); 
foreach (TextBox txtBx in allTxtBxs) 
{ 
    txtBx.ForeColor = Color.HotPink; 
} 

神似abatishchev's answer(其中,,只返回一級子控件),但也足夠不同,值得它自己的答案,我想。