10 個のテキスト ボックス txt_Address1、txt_Address2...txt_Address10 と、データベースに値を格納するための 10 個の列、つまり Address1、Address2...Address10 があります。ここで、テキスト ボックスの各値を取得し、対応する列に格納します。このために、テキスト ボックスごとに 10 行のコードを記述するのではなく、FOR ループで実行したいと考えています。誰かが私に適切な解決策を提案できますか?
3517 次
8 に答える
0
ステップ 1 :
すべてのForm
コントロールを調べて、コントロールのみを考慮する ことができTextBox
ます。
ステップ 2:すべてのフォーム コントロールから、" "TextBox
を含む TextBox をフィルタリングします。Name
txt_Address%
%
コードは次のとおりです。
List<String> txtValues=new List<string>();
foreach (var control in this.Controls)
{
if((control is TextBox) && (((TextBox) control).Name.Contains("txt_Address")))
txtValues.Add(((TextBox) control).Text.ToString());
}
于 2013-11-11T06:45:17.260 に答える
0
次のように Controls.Find() を使用できます。
for (int i = 1; i <= 10; i++)
{
Control[] matches = this.Controls.Find("txt_Address" + i.ToString(), true);
if (matches.Length > 0 && matches[0] is TextBox)
{
TextBox tb = (TextBox)matches[0];
// ... do something with "tb" ...
}
}
于 2013-11-11T06:45:18.433 に答える
0
または、リストなしでフォームからアクセスできます。
foreach(Control control in MyForm.Controls)
{
if(control is TextBox)
{
//do what you want
}
}
または、それらが groupBox にある場合
foreach(Control control in myGroupBox.Controls)
{
if(control is TextBox)
{
//do what you want
}
}
お役に立てれば!
または、FOR ループを使用します。
//Controls is the Controls collection of the form
for(int i=0;i<Controls.Count;i++)
{
if(Controls[i] is TextBox)
{
//do what you want
}
}
于 2013-11-11T06:39:48.407 に答える
0
そのように設計されたデータベースで動作するコードの作成に時間を費やす (無駄にする) 前に、データベースを再設計する必要があります。10 個のアドレスのテーブルに 10 個の列を含めることはお勧めできません。0 から無限大のアドレスを持つことができるはずです。リレーショナル データベースの作成方法を調べます。
基本的:
表: 顧客
CustomerID
Name
Etc.
表: CustomerAddresses
CustomerID
Address
City
State
Zip
于 2013-11-11T06:41:40.663 に答える
0
var columns = new Dictionary<string, string>();
for (int i = 1; i <= 10; i++) columns.Add("Address" + i, string.Empty);
var textBoxes = Controls.Find("txt_Address", true).Where(t => t is TextBox).ToList();
columns.ToList().ForEach(c =>
{
var index = c.Key.Replace("Address", string.Empty);
var textBox = textBoxes.FirstOrDefault(t => index.Equals(t.Name.Replace("txt_Address", string.Empty)));
if (textBox != null) columns[c.Key] = textBox.Text;
});
于 2013-11-11T07:38:37.213 に答える