Jump to content

Problema em mudar propriedade de controlo criado dinamicamente


zephirus
 Share

Recommended Posts

Olá,

Tenho este bocado de código:

Public btn As CheckBox

Sub Button1Click(sender As Object, e As EventArgs)
	label1.Text = Today
	For i As Integer = 0 To 23
		Dim lbl As New Label
		lbl.Name = "lbl" & i
		lbl.Text = i & " h"
		lbl.TextAlign = System.Drawing.ContentAlignment.MiddleRight
		tableLayoutPanel1.Controls.Add(lbl, 0, i)

		'Dim btn As New CheckBox
		btn = New CheckBox
		btn.Name = "btn" & i
		btn.Appearance = Appearance.Button
		btn.FlatStyle = FlatStyle.Flat
		btn.BackColor = Color.ForestGreen
		btn.Dock = DockStyle.Fill
		btn.Margin = New System.Windows.Forms.Padding(1)
		btn.Checked = False
		AddHandler btn.Click, AddressOf btn_Click
		tableLayoutPanel1.Controls.Add(btn, 1, i)
	Next i
End Sub

Basicamente, entre outras coisas, são criados 24 checkboxs dinamicamente. Pretendo depois que ao clicar nessas checkboxs, o estado da checkbox seguinte passe para "Checked = True". O código que tenho até agora é este:

Sub btn_Click(ByVal sender As Object,ByVal e As EventArgs)
Select Case IsEven(sender.Name.SubString(3))
   Case True
      Me.Text = "btn" & sender.Name.SubString(3) + 1
      Me.Controls("btn" & sender.Name.SubString(3) + 1).Checked = True
   Case False
End Select
End Sub

O problema é que ele não aceita esse controle. Diz que não está declarado. Onde é que está o problema?

Obrigado

Link to comment
Share on other sites

Tenta assim:

Sub btn_Click(ByVal sender As Object,ByVal e As EventArgs)
Select Case IsEven(sender.Name.SubString(3))
   Case True
      Me.Text = "btn" & sender.Name.SubString(3, sender.Name.ToString.Lenght - 3)
      Me.Controls("btn" & sender.Name.SubString(3, sender.Name.ToString.Lenght - 3)).Checked = True
   Case False
End Select
End Sub

PS: Não respondo a perguntas por mensagem que podem ser respondidas no fórum.

Link to comment
Share on other sites

Tenta assim:

Sub btn_Click(ByVal sender As Object,ByVal e As EventArgs)
Select Case IsEven(sender.Name.SubString(3))
   Case True
      Me.Text = "btn" & sender.Name.SubString(3, sender.Name.ToString.Lenght - 3)
      Me.Controls("btn" & sender.Name.SubString(3, sender.Name.ToString.Lenght - 3)).Checked = True
   Case False
End Select
End Sub

Obtenho este erro nas duas linhas que alteraste:

'Lenght' is not a member of 'String'. (BC30456)'

Link to comment
Share on other sites

Desculpa, enganei-me a escrever.

Sub btn_Click(ByVal sender As Object,ByVal e As EventArgs)
Select Case IsEven(sender.Name.SubString(3))
   Case True
      Me.Text = "btn" & sender.Name.SubString(3, sender.Name.ToString.Length - 3)
      Me.Controls("btn" & sender.Name.SubString(3, sender.Name.ToString.Length - 3)).Checked = True
   Case False
End Select
End Sub

PS: Não respondo a perguntas por mensagem que podem ser respondidas no fórum.

Link to comment
Share on other sites


    Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Select Case IsEven(sender.Name.ToString.Substring(3))
            Case True
                Dim obj As CheckBox = sender
                obj.Text = "btn" & sender.Name.ToString.Substring(3) + 1
                obj.Checked = True
            Case False
        End Select
    End Sub
Link to comment
Share on other sites

Desculpa, enganei-me a escrever.

Sub btn_Click(ByVal sender As Object,ByVal e As EventArgs)
Select Case IsEven(sender.Name.SubString(3))
   Case True
      Me.Text = "btn" & sender.Name.SubString(3, sender.Name.ToString.Length - 3)
      Me.Controls("btn" & sender.Name.SubString(3, sender.Name.ToString.Length - 3)).Checked = True
   Case False
End Select
End Sub

Ops, também não reparei que era apenas um erro de spelling. De qualquer forma, não está a funcionar, dá-me este erro:

System.NullReferenceException: Variável de objecto ou de bloco With não definida.

  at Container..ctor

  at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSet

  at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateSetComplex

  at System.Windows.Forms.Control.OnClick

  at System.Windows.Forms.CheckBox.OnClick

  at System.Windows.Forms.CheckBox.OnMouseUp

  at System.Windows.Forms.Control.WmMouseUp

  at System.Windows.Forms.Control.WndProc

  at System.Windows.Forms.ButtonBase.WndProc

  at ControlNativeWindow.OnMessage

  at ControlNativeWindow.WndProc

  at System.Windows.Forms.NativeWindow.DebuggableCallback

  at ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop

  at ThreadContext.RunMessageLoopInner

  at ThreadContext.RunMessageLoop

  at System.Windows.Forms.Application.Run

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel

  at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run

  at Reservations.My.MyApplication.Main in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81

Gooden, penso que onde tens "obj.Text" querias dizer "obj.Name" mas também não funciona.

Link to comment
Share on other sites

caraças pá!!! o objecto ta dentro de um layout painel -.-" lol 👍


    Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Select Case IsEven(sender.Name.ToString.Substring(3))
            Case True
                Dim obj As CheckBox
                obj = Me.tableLayoutPanel1.Controls("btn" & Int(sender.Name.ToString.Substring(3) + 1))
                obj.Text = "btn" & Int(sender.Name.ToString.Substring(3) + 1)
                obj.Checked = True
            Case False
        End Select
    End Sub
Link to comment
Share on other sites

caraças pá!!! o objecto ta dentro de um layout painel -.-" lol 👍


    Sub btn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Select Case IsEven(sender.Name.ToString.Substring(3))
            Case True
                Dim obj As CheckBox
                obj = Me.tableLayoutPanel1.Controls("btn" & Int(sender.Name.ToString.Substring(3) + 1))
                obj.Text = "btn" & Int(sender.Name.ToString.Substring(3) + 1)
                obj.Checked = True
            Case False
        End Select
    End Sub

É isso mesmo, Gooden!!! Muito obrigado. Já agora deixo aqui outra questão. Apesar de isto já estar a funcionar, há alguma coisa que possa ser feita de maneira diferente para melhorar? Isto da criação de controles em runtime é um pouco novo para mim.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.