2011-05-15 3 views
1

양식에 채워진 텍스트 상자 만 인쇄하고 그 근처에 레이블을 인쇄 할 수있는 코드를 작성하려고합니다. 아래의 코드를 검색하여 최종 결과라고 생각했습니다. 나는 어쩌면 내가 너희들의 도움이 필요합니다, 지금까지 멀리 용액으로부터 갔다 왜 모르겠어요채워진 텍스트 상자와 그 주변의 레이블 만 어떻게 인쇄합니까?

Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage 

    '=============================================================================================== 
    Dim name As String 
    Dim cc As Integer 
    Dim f As New Font("Times New Roman", 14, FontStyle.Underline) 
    Dim b As New SolidBrush(Color.Black) 
    Dim c As Integer = 60 

    'search for filled textboxes 

    For Each ctrl In Me.TabControl1.TabPages(0).Controls 

     If (TypeOf ctrl Is TextBox) Then 

      If CType(ctrl, TextBox).Text.Length > 0 Then 
       e.Graphics.DrawString(CType(ctrl, TextBox).Text, f, b, 30, c) 
       c = c + 30 

       If c > 60 Then 
        Exit For 
       End If 
      End If 
     End If 

    Next 
    '====================================================== 
    'to know if previuos for loop actualy find any 
    If c > 60 Then 
     ' to be print over textboxes if are there 
     e.Graphics.DrawString(Label6.Text, f, b, 16, 30) 

    End If 
    '====================================================== 
    ' this one to find out the name of the control wich found filled 
    For Each ctrl In Me.TabControl1.TabPages(0).Controls 

     If (TypeOf ctrl Is TextBox) Then 

      If CType(ctrl, TextBox).Text.Length > 0 Then 
       name = ctrl.Name 
       e.Graphics.DrawString(name, f, b, 0, c) 

      End If 
     End If 
     Exit For 
    Next 
    'and this step to print the label with its textbox 
    If name = "TextBox2" Then 
     e.Graphics.DrawString(Label1.Text, f, b, 0, c) 

    ElseIf name = "TextBox3" Then 
     e.Graphics.DrawString(Label2.Text, f, b, 0, c) 

    ElseIf name = "textbox4" Then 
     e.Graphics.DrawString(Label3.Text, f, b, 0, c) 
    ElseIf name = "textbox5" Then 

     e.Graphics.DrawString(Label4.Text, f, b, 0, c) 
    End If 


End Sub  

vb.net 2008을 사용하고있다?

답변

0

가장 먼저. 프로그래밍은 패턴을 이해하는 것입니다.

문제를 해결하려면 먼저 패턴을 만드십시오.

귀하의 경우에 필요한 패턴은 이름 지정 전략입니다.

레이블 및 텍스트 상자의 이름을 올바르게 지정하십시오. 예.

lblFirstName txtFirstName 
lblLastName txtLastName 

다음과 같이하면 코드가 변경됩니다. 레이블에 별도의 루프가 필요하지 않습니다.

Dim ControlStack As New Stack(Of TextBox) 

    For Each ctrl In Me.TabControl1.TabPages(0).Controls 'Modified to handle issue of taborder' 
     If (TypeOf ctrl Is TextBox) Then 
      ControlStack.Push(ctrl) 
     End If 
    Next 

    For Each ctrl In ControlStack 
     If (TypeOf ctrl Is TextBox) Then 

      If CType(ctrl, TextBox).Text.Length > 0 Then 
       name = ctrl.Name 
       dim labelName as string 
       labelName ="lbl" & mid(name ,4) 
       dim labelControl as Label 
       Dim controlsFound() As Control 
       controlsFound = Controls.Find(labelName,True) 
       If controlsFound.Length > 0 Then 
        labelControl = controlsFound(0) 
       End If 
       e.Graphics.DrawString(labelControl.Text , f, b, 0, c) 
       e.Graphics.DrawString(ctrl.Text, f, b, 250, c) 
      End If 
     End If 
     Exit For 
    Next 

같은 것을 이해하면 알려 주시기 바랍니다. 나는 힌트 코드에 완전한 코드를 제공하지 않았다.

+0

입니다. 이는 단지 눈부신입니다. "레이블 = Controls.Find (labelName, True)"로 표시된 "dim labelControl"에서 "value of type 'System.Windows.Forms.Control의 1 차원 배열을'시스템으로 변환 할 수 없습니다. '라는 문제를 해결했습니다. Windows.Forms.Label ' "괜찮은 것 같아요. –

+0

코드를 업데이트했습니다. Actully find는 배열을 반환합니다. 그래서 당신은 같은 원소로부터 단지 0 번째 원소만을 취해야합니다. 지금 시도해보십시오. –

+0

ITS WORKS 고맙습니다. 고맙습니다. SACHIN은 (는) 깊은 문제에서 벗어나겠습니다. –

관련 문제