Help…
Os posteo una cosilla, con la esperanza de que algún programador pueda ayudarme. Os explico:
Tengo que cargar cuadros de texto y cuadros de imagen en tiempo de ejecución. En los de texto funciona todo bien, pero con los de imagen…
Llega un momento en el programa que tengo que borrar todos los cuadros de texto. Bien, pues hay algunos que n ose borran. Os pongo el código relativo a esto:
For j = 1 To 3
For Each nombreimg In Me.TabPage1.Controls
If InStr(nombreimg.Name, “img”) >= 1 Then
Me.TabPage1.Controls.Remove(nombreimg)
End If
Next
Next
Explico el código. Se introduce en “nombreimg” todos los nombres de los objetos en un tabpage del formulario. Luego se comprueba que si ese nombre contiene la cadena “img” (al crear los cuadros de imagen, le pongo de nombre img1, img2 etc) pues que lo borre.
Y puede que se os venga a la cabeza dos preguntas.
- Porque no creas los objetos en tiempo de ejecución en una matriz?
Porque eso estaría muy bien en VB6, pero vb2008 (requerido por la API) no lo soporta, con lo que tengo que apañarmelas. - Porqué ejecutas el código 3 veces? (for j=1 to 3). Pues para forzar que busque más de una vez en los objetos, y que encuentre los que tengan de nombre “img”. Esto lo he hecho como “solución” a que no se borraban todos los cuadros de texto, pero sigue sin ir. Tampoco voy a poner que ejecute el código 1000 veces. Los borraría todos, si, pero a costa de un buen rato, mal programado.
Ahí lo llevais. Os pongo el código del formulario a ver si es que encontrais otra cosa (lo que empieza por comilla simple es comentado, para comprobar cosillas o distintas formas de hacerlo, pasad de ellas):
Public Class Form2
Dim i As Integer
Dim txt As New Windows.Forms.TextBox
Dim nombretxt As Control, nombreimg As Control, j As Integer
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Text = tw.AccountInformation.ScreenName + ” en PhobosTwit”
imgusuario.ImageLocation = tw.AccountInformation.ProfileImageUrl
LinkLabel1.Text = tw.AccountInformation.ScreenName
Call cargartwits()
End Sub
Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
Process.Start(tw.AccountInformation.Url)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
tw.Update(twitear.Text)
twitear.Text = “”
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Text = “”
Call borratwits()
End Sub
Public Function cargartwits()
j = 1
i = 0
For Each tweet As TwitterVB2.TwitterStatus In tw.HomeTimeline()
If TextBox1.Text = “” Then
PictureBox1.ImageLocation = tweet.User.ProfileImageUrl
TextBox1.Text = tweet.User.ScreenName + ” : ” + tweet.Text
Else
Dim txt As New Windows.Forms.TextBox
Dim img As New Windows.Forms.PictureBox
Me.TabPage1.Controls.Add(txt)
Me.TabPage1.Controls.Add(img)
With img
.Left = PictureBox1.Left
.Top = PictureBox1.Top + PictureBox1.Height + i
.Enabled = True
.Height = PictureBox1.Height
.Width = PictureBox1.Width
.Visible = True
.Name = Trim(“img”) ‘+ Trim(Str((j))))
.ImageLocation = tweet.User.ProfileImageUrl
End With
With txt
.Name = “txt” + Trim(Str((j)))
.BorderStyle = BorderStyle.FixedSingle
.BackColor = Color.Gainsboro
.Left = TextBox1.Left
.Top = TextBox1.Top + TextBox1.Height + i
.Enabled = True
.Multiline = True
.Height = TextBox1.Height
.Width = TextBox1.Width
.Visible = True
.Text = img.Name + tweet.User.ScreenName + ” : ” + tweet.Text
‘.Text = img.Name
End With
i = i + 45
j = j + 1
End If
Next
For Each mention As TwitterVB2.TwitterStatus In tw.Mentions
ListBox2.Items.Add(mention.User.ScreenName + ” : ” + mention.Text)
Next
End Function
Public Function borratwits()
Dim hastaloshuevos As String
TextBox1.Text = “”
j = 1
For Each nombretxt In Me.TabPage1.Controls
hastaloshuevos = Trim((“txt” + Trim(Str((j)))))
If nombretxt.Name = hastaloshuevos Then
‘MsgBox(“ahilasdao”)
Me.TabPage1.Controls.Remove(nombretxt)
j = j + 1
End If
Next
For j = 1 To 3
For Each nombreimg In Me.TabPage1.Controls
’ hastaloshuevos = Trim(“img” + Trim(Str((j))))
‘If nombreimg.Name = hastaloshuevos Then
If InStr(nombreimg.Name, “img”) >= 1 Then
‘MsgBox(“ahilasdao”)
Me.TabPage1.Controls.Remove(nombreimg)
End If
Next
Next
Call cargartwits()
End Function
End Class
Obviamente faltan cosas, que están en un módulo aparte y son indiferentes para este problema.