2012-01-06 2 views
0

OK 여기에 내가 한 것과 세로로 설정된 값이 레이블에 복사되지만 가로로 표시됩니다. 한 열/행만 가능합니다. 그래서 여기 텍스트 상자의 값을 레이블 배열로 복사하는 방법은 무엇입니까?

Here's a photo

public partial class Form1 : Form 
{ 
    private Label l; 
    private Button bStart; 
    private TextBox txtVnes; 
    private Label[] pole; 

    public Form1() 
    { 
     InitializeComponent(); 
     bStart = new Button(); 
     bStart.Location = new Point(240, 165); 
     bStart.Width = 75; 
     bStart.Height = 25; 
     bStart.Text = "START"; 

     txtVnes = new TextBox(); 
     txtVnes.Location = new Point(240, 10); 
     txtVnes.Width = 160; 
     txtVnes.Height = 130; 
     txtVnes.Multiline = true; 

     int a = 0; 
     pole = new Label[42]; 
     for (int i = 1; i <= 6; i++) 
     { 
      for (int j = 1; j <= 7; j++) 
      { 
       l = new Label(); 
       l.Name = "label" + i.ToString() + j.ToString(); 
       l.Text = "Z"; 
       l.Width = 20; 
       l.Height = 20; 
       l.TextAlign = ContentAlignment.MiddleCenter; 
       l.Parent = this; 
       l.BackColor = Color.FromArgb(100, 149, 237); 
       l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25); 
       pole[a] = l; 
       this.Controls.Add(l); 
       a++; 

      } 
     } 

     this.Controls.Add(bStart); 
     this.Controls.Add(txtVnes); 

     bStart.Click += new EventHandler(bStart_Click); 

    } 


    private void bStart_Click(object sender, EventArgs e) 
    { 

     Regex regex = new Regex(@"^(\s)*(\d){6}\d(\s)*$"); 
     bool isValid = true; 
     string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, 
     StringSplitOptions.RemoveEmptyEntries); 


     if (ts == null || ts.Length < 1 || ts.Length > 6) 
     { 
      MessageBox.Show("Not valid"); 
     } 
     else 
     { 
      foreach (string t in ts) 
      { 
       if (regex.IsMatch(t) == false) 
       { 
        MessageBox.Show("Not valid"); 
        break; 
       } 
      } 

     } 

     if (isValid) 
     { 

      for (int i = 0; i < 6; i++) 
      { 
       if (i < ts.Length && regex.IsMatch(ts[i])) 
       { 

        pole[i].Text = ts[i]; 
       } 
       else 
       { 
        pole[i].Text = "not valid"; 
       } 
      } 

     } 
    } 

문제입니다 : 내가 버튼 bStart 하나의 값이 복사 및 라벨의 배열에서 하나 라베에서 교체를 클릭합니다. 이것은 다음과 같이 작동합니다 : 사용자가 bStart 버튼을 클릭하면 txtVnes 텍스트 상자의 모든 값을 레이블 배열의 각 레이블에 복사해야합니다. 모든 레이블에는 텍스트 "Z"가 있으며 단추를 클릭 한 후 텍스트 상자의 값으로 변경해야합니다. 당신이 볼 수 있듯이 l.Text = txtVnes.Text; 값을 복사하는 데 사용되지만 작동하지 않습니다. 네가 도와 준다면 고마워, 고마워!

+1

모든 관련 코드를 표시하십시오. 관련 코드 만 표시하십시오. 우리가 볼 필요가있는 부분이 있지만 당신은 그 부분을 우리에게 보여주지 않습니다. 그리고 당신은 우리가 정말로 보지 않아도되는 부분을 우리에게 보여주고 있습니다. –

+0

두 코드 부분 간의 관계가 표시되지 않습니다. –

답변

0

항상 동일한 레이블의 텍스트를 설정하고 있습니다. l. 라벨이 배열 pole에 있으므로 문자를 연속으로 pole 개의 색인으로 설정해야합니다. 나는 당신이 원하는

시작 부분에 유효한 모든 텍스트 :

string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); 
int k = 0; 
for (int i = 0; i < ts.Length && k < 6; i++) { 
    if (IsValid(ts[i])) { // Where IsValid is a method containing your validation logic. 
     pole[k++].Text = ts[i]; 
    } 
} 

// Fill remaining labels 
for (int i = k; i < 6; i++) { 
    pole[i].Text = "not valid"; 
} 

또는, 유 효와 유효하지 않은 문자가 혼합하려는 경우 :

string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); 
for (int i = 0; i < 6; i++) { 
    if (i < ts.Length && IsValid(ts[i])) { // Where IsValid is a method containing your validation logic. 
     pole[i].Text = ts[i]; 
    } else { 
     pole[i].Text = "not valid"; 
    } 
} 

참고 배열 인덱스가없는에서, 0에서 시작 1.


편집 # 2 :

IsValid 방법은 다음과 같을 것이다 :

private static Regex regex = new Regex(@"^(\s)*(\d){6}\d(\s)*$"); 

private static bool IsValid(string s) 
{ 
    return regex.IsMatch(s); 
} 

이 코멘트에있는 당신의 질문에 대답하기 : 예, 예 위 bStart_Click에 배치해야합니다.

그리고 폼 생성자에 또 다른 오류가 있습니다. this.Controls.Add(l);pole[a] = l; 바로 뒤에 for-loop 안쪽에 배치해야합니다. 그렇지 않으면 양식에 하나의 레이블 만 표시됩니다.

구현 코드를 anlyzed 한 후

마지막으로, 난 당신이 텍스트 상자에 다음과 같은 형식으로 텍스트를 입력 한 다음 해당 레이블에 숫자를 배치 할 수 있도록하고자하는 결론에 도달 :

1 2 3 4 5 6 7 
2 3 4 6 7 8 0 
0 1 2 6 6 6 7 
1 2 3 4 5 6 7 
2 3 4 6 7 8 0 
0 1 2 6 6 6 7 

전체 코드는 다음과 같아야합니다.

public partial class Form1 : Form 
{ 
    private Button bStart; 
    private TextBox txtVnes; 
    private Label[] pole; 

    public Form1() 
    { 
     InitializeComponent(); 
     bStart = new Button(); 
     bStart.Location = new Point(240, 165); 
     bStart.Width = 75; 
     bStart.Height = 25; 
     bStart.Text = "START"; 
     bStart.Click += new EventHandler(bStart_Click); 
     this.Controls.Add(bStart); 

     txtVnes = new TextBox(); 
     txtVnes.Location = new Point(240, 10); 
     txtVnes.Width = 160; 
     txtVnes.Height = 130; 
     txtVnes.Multiline = true; 
     this.Controls.Add(txtVnes); 

     int a = 0; 
     pole = new Label[42]; 
     for (int i = 1; i <= 6; i++) { 
      for (int j = 1; j <= 7; j++) { 
       var l = new Label(); 
       l.Name = "label" + i.ToString() + j.ToString(); 
       l.Text = "Z"; 
       l.Width = 20; 
       l.Height = 20; 
       l.TextAlign = ContentAlignment.MiddleCenter; 
       l.Parent = this; 
       l.BackColor = Color.FromArgb(100, 149, 237); 
       l.Location = new Point(10 + (j - 1) * 25, 15 + (i - 1) * 25); 
       pole[a] = l; 
       this.Controls.Add(l); 
       a++; 
      } 
     } 
    } 

    private void bStart_Click(object sender, EventArgs e) 
    { 
     string[] ts = txtVnes.Text.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); 
     int row = 0; 
     for (int i = 0; i < ts.Length && row < 6; i++) { 
      if (LineIsValid(ts[i])) { 
       for (int col = 0; col < 7; col++) { 
        pole[row * 7 + col].Text = ts[i][2 * col].ToString(); 
       } 
       row++; 
      } 
     } 

     // Fill remaining labels 
     for (; row < 6; row++) { 
      for (int col = 0; col < 7; col++) { 
       pole[row * 7 + col].Text = "Z"; 
      } 
     } 
    } 

    private static Regex regex = new Regex(@"^(\s)*(\d){6}\d(\s)*$"); 

    private static bool LineIsValid(string line) 
    { 
     return regex.IsMatch(line); 
    } 
} 

두 개의 중첩 루프가 bStart_Click에도 필요합니다. 하나는 행을위한 것이고 다른 하나는 열을위한 것입니다.

+0

미안하지만 IsValid 메서드에 무엇을 넣어야하는지 이해하지 못했습니다. 위의 코드는 이벤트 bStart_Click에 있어야합니다. 맞습니까? – Isabella

+0

글쎄, 당신의 문자열이 유효한지 확인하기 위해 당신의 Regex validation이나 무엇을했는지. (나는이 세부 사항으로 나의 예를 혼란스럽게하고 싶지 않았다.) –

+0

예, 그것은 내 요점이었습니다 :) 나는 그것을 시도하고 작동합니다. 고마워요! 이것은 제가 학교에서하고있는 프로젝트의 시작일뿐입니다. 프로그래밍에 익숙하지 않지만, 덕분에 더 많은 것을 이해할 수있었습니다 :) 코드에서 어딘가에 갇혀 있다면 다시 도움을 요청할 수 있습니까? 다시 한번 감사드립니다. – Isabella

관련 문제