2011-10-03 2 views
1

이 포럼을 좋아합니다. VB.NET과 관련하여 초보자이며 약간의 문제가 발생했습니다.동적 UserControls에서 개체 호출 VB.NET

은 여기 내 코드

'decleare variables 
    Dim vmcount As Integer 
    Dim tabcount As Integer 
    Dim userControl As Control 
    Dim UserControlName As String 

    vmcount = combo_vmcount.SelectedItem 

    tabcount = 1 

    tab_con_vm.TabPages.Clear() 


    While (tabcount <= vmcount) 

     Dim tabname As New TabPage 

     'Load variables 
     userControl = New calc_usercontrol_vm 

     tabname.Text = "VM" & tabcount 

     tabname.Name = "VM" & tabcount 

     UserControlName = "UCVM" & tabcount 

     userControl.Name = UserControlName 

     'actions 
     tab_con_vm.TabPages.Add(tabname) 


     tabname.Controls.Add(userControl) 

     'next 
     tabcount = tabcount + 1 


    End While 
End Sub 

말썽 I는 동적으로 생성 된 UserControls의 개체를 호출 할 수있는 방법을 밖으로 작동 데 스 니펫입니다. 어쩌면 옵션을 생각해 보았지만 구문을 얻으려고 애쓴다. 사람이 어떤 아이디어 나 다른 방법이 있는지 ..

감사합니다 얘들 아 리처드

+0

어떤 대상을 언급하고 있습니까? – Josh

답변

1

이 문제를 해결하는 가장 좋은 방법은 아니지만 전역 배열을 만들고 사용자 컨트롤을 구축해야합니다.

' USERCONTROL ARRAY 
Public UCVMARRAY(30) As calc_usercontrol_vm 






Dim tabcount As Integer 

    Dim UCVMARRAYindex As Integer 

    Dim userControl As Control 'control variable 

    Dim UserControlName As String 


    vmcount = combo_vmcount.SelectedItem 

    tabcount = 1 

    UCVMARRAYindex = 0 

    tab_con_vm.TabPages.Clear() 


    While (tabcount <= vmcount) 

     Dim tabname As New TabPage ' Relook at this to improve the method used. Issue was that new page was not generated on loop. 

     'Load variables 

     userControl = New calc_usercontrol_vm 
     ' loads UC 

     tabname.Text = "VM" & tabcount 

     tabname.Name = "VM" & tabcount 

     UserControlName = "UCVM" & tabcount 

     userControl.Name = UserControlName 

     UCVMARRAY(UCVMARRAYindex) = userControl 'places it back 

     'actions 
     tab_con_vm.TabPages.Add(tabname) 

     tabname.Controls.Add(userControl) 


     'next 
     tabcount = tabcount + 1 
     UCVMARRAYindex = UCVMARRAYindex + 1 


    End While 
End Sub 

그것은 아주 기본입니다하지만 난 그게 작동있어 답변은 위의 대부분 좋은 해결책이지만 vb.net의 나는 내 지식 처음까지하지 ​​않았다.

-1

를 호출 Page.LoadControl 방법 다음 preferrebly 페이지 수명주기의 초기화 부분에, 당신의 페이지에 추가 궁금.

+0

mm .. 이것은 asp.net처럼 보이지 않습니다. .. – gbianchi

1

키가있는 TabControl의 Name입니다 당신은 당신이 인덱스를 모른다면 당신은 방법을 사용할 수 있습니다 또는

Dim calc_usercontrol As calc_usercontrol_vm = TabPages(index).userControl 

작업하고자하는 탭의 인덱스를 알고 있다면

Dim index as Integer = TabPages.IndexOfKey("TabControlName") 
+0

+1, 이것은 올바른 방법입니다 (@msarchet, 내 생각에 잘못된 코드를 사용했습니다) – gbianchi

+0

감사합니다 msarchet! 나는이 문제를 해결하는 데 여전히 어려움을 겪고 있습니다. 그런 고통에 빠져서 죄송합니다. 조금 더 자세히 설명해 주시겠습니까? – SYNACK

+0

@ 리차드 당신이 정교해야 할 것은 무엇입니까? – msarchet