Inquiridor
Por Favor me ajude como criar um criador de objetos.

Pergunta
-
Todas as Respostas
-
Boa noite amigo!
Eu estava intrigado com um post de um outro companheiro aqui do forum, sobre formulários mutantes e estava montando um exemplo para meu uso próprio...só pelo prazer de redescobrir o fogo. No meio do processo, percebi que criei um criador de objetos. Então se precisar, abaixo segue o código. É bem rudimentar ok? Mas pelo menos já te da um norte para estudar.
Module Module1 Dim WithEvents oButton As Button Dim WithEvents oTextBox As TextBox Function CriaObj(ByVal sTipo As String, ByVal sNome As String, ByVal iTop As Integer, ByVal ileft As Integer, ByVal iWidth As Integer, ByVal iHeigth As Integer, Optional ByVal sValor As String = "") As Object Select Case sTipo.ToUpper Case "LABEL" Dim labelAux As New Label labelAux.Name = sNome labelAux.Top = iTop labelAux.Left = ileft labelAux.Width = iWidth labelAux.Height = iHeigth If sValor.Trim <> "" Then labelAux.Text = sValor End If Return labelAux Case "TEXTBOX" oTextBox = New TextBox oTextBox.Name = sNome oTextBox.Top = iTop oTextBox.Left = ileft oTextBox.Width = iWidth oTextBox.Height = iHeigth If sValor.Trim <> "" Then oTextBox.Text = sValor End If Return oTextBox Case "BUTTON" oButton = New Button oButton.Name = sNome oButton.Top = iTop oButton.Left = ileft oButton.Width = iWidth oButton.Height = iHeigth If sValor.Trim <> "" Then oButton.Text = sValor End If AddHandler oButton.Click, AddressOf Form1.MyClick Return oButton End Select End Function End Module
Aí eu chamo essa função assim:
Me.Controls.Add(CriaObj("Label", "lblLogin", 10, 3, 100, 15, "Login:")) Me.Controls.Add(CriaObj("Label", "lblSenha", 30, 3, 100, 15, "Senha:")) Me.Controls.Add(CriaObj("TextBox", "txtLogin", 10, 110, 100, 15)) Me.Controls.Add(CriaObj("TextBox", "txtSenha", 30, 110, 100, 15)) Me.Controls.Add(CriaObj("Button", "btnEntrar", 60, 110, 70, 30, "Entrar")) Me.Controls.Add(CriaObj("Button", "btnSair", 60, 190, 70, 30, "Sair"))
Para criar os eventos click, eu ainda to meio que pastando nisso, mas eu consegui lendo esse link.
Eu também precisei criar essa classe:
Public Class Class1 ' Declare the event to be raised. ' Public Event MyClick() Sub RaiseMyEvent() ' ' Method to raise the event. ' RaiseEvent MyClick() End Sub End Class
E aí por fim criei a sub myclick para tratar o evento do botão:
Public Sub MyClick() ' ' Code to be executed when the event is raised. ' If Me.Controls("txtLogin").Text = "" Then MsgBox("Digite um Login!") Exit Sub End If If Me.Controls("txtSenha").Text = "" Then MsgBox("Digite uma Senha!") Exit Sub End If If Me.Controls("txtLogin").Text = "User" Then If Me.Controls("txtSenha").Text = "123" Then Mutacao(1) End If End If End Sub
Bom espero que te ajude esses códigos a criar o seu criador de objetos, se vc já não o criou.
Att.
Giovani
- Sugerido como Resposta Softlesk terça-feira, 11 de junho de 2013 12:29
-