foreach 루프를 통해 TextBox 및 NumericUpDown 도구가 동적으로 생성되는 코드가 있습니다. 이제 각 반복마다 두 값이 데이터베이스의 동일한 행에 추가되는 두 값을 모두 가져오고 싶습니다. NumericUpDown 값을 가져 오는 코드가 있고 TextBox 값을 가져 오는 방법을 구현할 수 있는지 궁금합니다. 동적 NumericUpDown 및 C# 윈도우 폼에서 동적 numericupdown 및 textbox 값을 얻는 방법
코드는 버튼 클릭에 동적 NumericUpDown 값을 얻기 위해 (데이터베이스 코드가 작동)
t = temp.Split(';'); // e.g. t = Arial;32
int pointX = 70;
int pointY = 80;
int pointA = 300;
int pointB = 80;
for (int i = 0; i < N; i++)
{
foreach (object o in t)
{
TextBox a = new TextBox();
a.Text = o.ToString();
a.Location = new Point(pointX, pointY);
a.Size = new System.Drawing.Size(150, 25);
panel.Controls.Add(a);
panel.Show();
pointY += 30;
NumericUpDown note = new NumericUpDown();
note.Name = "Note" + i.ToString();
note.Location = new Point(pointA, pointB);
note.Size = new System.Drawing.Size(40, 25);
note.Maximum = new decimal(new int[] { 10, 0, 0, 0 });
panel.Controls.Add(note);
pointB += 30;
}
}
텍스트 상자
foreach (NumericUpDown ctlNumeric in panel.Controls.OfType<NumericUpDown>())
{
var value = ctlNumeric.Value;
int number = Decimal.ToInt32(value);
sample_save = "INSERT INTO forthesis (testVal) VALUES ('" + number + "');";
MySqlConnection myConn = new MySqlConnection(connectionString);
MySqlCommand myCommand = new MySqlCommand(sample_save, myConn);
MySqlDataAdapter da = new MySqlDataAdapter(myCommand);
MySqlDataReader myReader;
myConn.Open();
myReader = myCommand.ExecuteReader();
myConn.Close();
MessageBox.Show(number.ToString());
}
샘플 출력을 생성하기위한
어떻게 TextBox에서 값을 가져올 수 있습니까? 귀하의 도움을 많이 주시면 감사하겠습니다. 정말 고맙습니다!!!
Dictionary<string, TextBox> textboxes = new Dictionary<string, TextBox>();
for (int i = 0; i < N; i++)
{
foreach (object o in t)
{
TextBox a = new TextBox();
.....
NumericUpDown note = new NumericUpDown();
note.Name = "Note" + i.ToString();
......
textboxes.Add(note.Name, a);
}
}
foreach (NumericUpDown ctlNumeric in panel.Controls.OfType<NumericUpDown>())
{
var value = ctlNumeric.Value;
int number = Decimal.ToInt32(value);
TextBox tb = textboxes[ctrlNumeric.Name];
String text = tb.Text;
.........
}
당신이 TextBoxs를 제거하는 경우 & NumericUpDowns - 다음뿐만 아니라 사전을 취소해야합니다 -이 같은 일치하는 쌍 텍스트 상자를 저장하기 위해 텍스트 상자 사전 -
DB에 추가로 삽입되는 'number' 변수에 값을 모두 추가 하시겠습니까? –
@ Rakitić hmmm 다른 변수에 저장하는 것이 좋습니다. –
사전과 같은 것을 사용하려고 시도하는 중 : http://stackoverflow.com/questions/12238599/find-wpf-control-by-name - 두 가지를 사용할 수 있습니다. 각 컨트롤에 대해 동일한 키가있는 사전 – PaulF