Alternate between hiding and showing different controls on form - vb.net

I'm working on a small project to dynamically add browsers to a form and alternate them on a timer so every tick one browser changes from visible=true to false and so on, but can't for the life of me figure out how to do it.
'Some Variables
Dim Browsers() As WebControl
Dim XMLFile As String = "\\********.xml"
Dim Controllist As New ArrayList
Dim BrowserCount As Integer
Private Sub Left_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set form properties
Me.Location = New Point(Screen.AllScreens(0).Bounds.X, Screen.AllScreens(0).Bounds.Y)
Me.FormBorderStyle = Windows.Forms.FormBorderStyle.None
Me.WindowState = FormWindowState.Maximized
Me.TopMost = True
Try
'Load xml file with urls
Dim xmlDoc As New XmlDocument
Dim nodeList As XmlNodeList = xmlDoc.SelectNodes("//BottomLeft/url")
xmlDoc.Load(XMLFile)
'Define number of browsers based on xml
Dim numberOfBrowsers As Integer = nodeList.Count + 1
ReDim Browsers(numberOfBrowsers)
'Put urls in an array to use later
Dim UrlList As New ArrayList
Debug.WriteLine("Number of browser is: " & numberOfBrowsers)
For Each inst As XmlNode In nodeList
UrlList.Add(inst.InnerText)
Debug.WriteLine("Added to array: " & inst.InnerText)
Next
'Set properties and add the browsers
For counter As Integer = 0 To numberOfBrowsers - 1
Debug.WriteLine("Start adding browsers")
Browsers(counter) = New WebControl
With Browsers(counter)
Debug.WriteLine("Setting properties")
.Dock = DockStyle.Fill
.Name = "Browers" & counter
.Source = New Uri(UrlList(counter))
If counter = 0 Then
.Visible = True
Else
.Visible = False
End If
End With
'Add the actual browsers after the properties have been set
Me.Controls.Add(Browsers(counter))
AddHandler Browsers(counter).LoadingFrameComplete, AddressOf All_Browsers_Loaded
Debug.WriteLine("Browsers is added. Current Count is: " & counter)
Next
Catch ex As Exception
Debug.WriteLine(ex)
End Try
'Count all the added browsers to alternate between them
Dim allWebBrowser As New List(Of Control)
For Each web As WebControl In FindControlRecursive(allWebBrowser, Me, GetType(WebControl))
Debug.WriteLine("Webcontrols on form: " & web.Name)
BrowserCount = +1
Next
'Start timer to switch browsers
Timer1.Interval = 1000
Timer1.Start()
End Sub
Private Sub All_Browsers_Loaded(ByVal sender As System.Object, ByVal e As Awesomium.Core.FrameEventArgs)
Dim Browser As WebControl = DirectCast(sender, WebControl)
Browser.Zoom = "80"
Debug.WriteLine("Browsers is now zoomed out?")
End Sub
Public Function ChangeBrowser()
Dim Changed As Boolean
For I As Integer = 0 To BrowserCount
With Browsers(I)
If Browsers(I).Visible = True Then
Browsers(I).Visible = False
Changed = True
End If
If Changed = True Then
Browsers(I).Visible = True
Changed = False
End If
If I = BrowserCount Then
Browsers(0).Visible = True
Continue For
End If
End With
Next
Return False
End Function
In the ChangeBrowser() function I'm trying to get the first visible browser and make it not visible. Then set the Changed Boolean to true.
Whats the best way to handle this?

Related

Get items and index of dynamic comboBox in VB.NET

I have created a list of eight dynamic comboBoxes through the function combox1Gen() and then i have loaded items to them from a text file of values through function loadComboboxItems(). On every respective comboBox's item selection i need to display the value and its index. what code should in put inside ComboTName_SelectedIndexChanged()?
One more issue is there. My comboBoxes with items get loaded very slowly in a hanging way. whats wrong with my code?
My code is below:
Public ComboBoxesTname As New List(Of ComboBox)
Dim i As Integer = 0
Do While i <= 7
Combo1Gen(i)
i = i + 1
Loop
loadComboboxItems()
Private Function Combo1Gen(ByVal n As Integer) As Boolean
Try
Dim newCombo As New ComboBox
With newCombo
.Name = "MyComboBox1" & n.ToString
.Left = 110
.Top = 120 + (52 * n) + 20
.Width = 180
.Height = 20
.Visible = True
End With
ComboBoxesTname.Add(newCombo)
GroupBox1.Controls.Add(newCombo)
GroupBox1.AutoSize = True
AddHandler newCombo.SelectedIndexChanged, AddressOf ComboTName_SelectedIndexChanged
Return True
Catch ex As Exception
MessageBox.Show(ex.ToString)
Return False
End Try
End Function
Private Function loadComboboxItems() As Boolean
Try
Dim listT As New List(Of String)()
listT = ReadVars.readVars(GetFolderPath.GetFolderPath("\vars\"), "items.txt")
For i = 0 To ComboBoxesTname.Count - 1
ComboBoxesTname(i).Items.AddRange(listT.ToArray)
Next
Return True
Catch ex As Exception
Debug.WriteLine(ex.ToString)
MessageBox.Show("Error: " & Err.ToString)
Return False
End Try
Return False
End Function
Private Sub ComboTName_SelectedIndexChanged()
End Sub
I am not sure exactly what you want to do in SelectIndexChanged but I showed how to get the values from the combo that was clicked.
See comments and explanations in line.
Public ComboBoxesTname As New List(Of ComboBox)
Private Sub LoadCombos()
For i = 0 To 7
Combo1Gen(i)
Next
loadComboboxItems()
End Sub
'Since you never use the return value I changed this to a Sub
Private Sub Combo1Gen(ByVal n As Integer)
Dim newCombo As New ComboBox
With newCombo
.Name = "MyComboBox1" & n.ToString
.Left = 110
.Top = 10 + (52 * n) + 20
.Width = 180
.Height = 20
.Visible = True
End With
ComboBoxesTname.Add(newCombo)
GroupBox1.Controls.Add(newCombo)
GroupBox1.AutoSize = True
AddHandler newCombo.SelectedIndexChanged, AddressOf ComboTName_SelectedIndexChanged
End Sub
'Since I don't have access to ReadVars I created an arbitrary array to test
Private listT As String() = {"Mathew", "Mark", "Luke", "John"}
'You should have readVars return an array of strings
'Create this once as a Form level variable so you don't have to read the file over and over
'Private listT As String() = ReadVars.readVars(GetFolderPath.GetFolderPath("\vars\"), "items.txt")
'Since you never use the return value I changed this to a Sub
Private Sub loadComboboxItems()
For i = 0 To ComboBoxesTname.Count - 1
ComboBoxesTname(i).Items.AddRange(listT)
Next
End Sub
'Added the appropriate parameters
Private Sub ComboTName_SelectedIndexChanged(sender As Object, e As EventArgs)
'Cast sender to a ComboBox so we can use the Properties of a ComboBox
Dim Combo = DirectCast(sender, ComboBox)
Dim Name = Combo.Name
Dim Item = Combo.SelectedItem
MessageBox.Show($"The selected item in {Name} is {Item}")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadCombos()
End Sub
EDIT
In response to your question in comments. I added a Function and a few changes to ComboTName_SelectedIndexChanged
Private Sub ComboTName_SelectedIndexChanged(sender As Object, e As EventArgs)
'Cast sender to a ComboBox so we can use the Properties of a ComboBox
Dim Combo = DirectCast(sender, ComboBox)
Dim Name = Combo.Name
Dim N As Integer = GetValueOfN(Name)
Dim Item = Combo.SelectedItem
MessageBox.Show($"The selected item in {Name} is {Item} The number of the combo cox is {N}")
End Sub
Private Function GetValueOfN(ComboName As String) As Integer
Dim NumString = ComboName.Substring(ComboName.IndexOf("1") + 1)
Return CInt(NumString)
End Function

Threading doesn't complete the task before ending the thread loop

So I did something similar a while ago, but this is essentially a username checker for a (specific) website, they can load in usernames via text file and it will put it into a list box, now I have the start button and it's meant to check each username. Before, however, it froze the program when they checked, but it worked. I tried making it "threaded" so it didn't freeze.
The problem now is that it doesn't check them all, and finishes instantly.
CODE:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Button6.Enabled = False
Dim goodUsers As New SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
Dim flag As Boolean
Dim Incomplete As Integer = 0
Dim Taken As Integer = 0
Dim sb As New StringBuilder
If goodUsers.ShowDialog() = DialogResult.OK Then
Dim checkerMT As New Thread(
Sub()
For Each i As String In UsernameList.Items
WebRequest.Create("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString)
Dim cResult As String = New System.Net.WebClient().DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If cResult = "taken" Then
flag = False
ElseIf cResult = "nottaken" Then
flag = True
End If
If flag = True Then
sb.Append(i & vbNewLine)
Else
Incomplete = Incomplete + 1
Taken = UsernameList.Items.Count - Incomplete
End If
Next
End Sub
)
checkerMT.Start()
Try
File.WriteAllText(goodUsers.FileName, sb.ToString)
Catch ex As Exception
Exit Sub
End Try
End If
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
Button6.Enabled = True
End Sub
You cannot access UI elements (UsernameList.Items) from a thread other than the UI. Instead add a background worker to your form to handle the basic threading stuff (progress reporting, finish reporting, exception handling). Pass into this an object that contains the settings your job needs to do its work without interacting with the ui.
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Button6.Enabled = False
Dim goodUsers As New SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
If goodUsers.ShowDialog() = DialogResult.OK Then
'Note: You'll need to add the filenames
BackgroundWorker1.RunWorkerAsync(New State() With {.Names = {}, .FileName = goodUsers.FileName})
End If
End Sub
Class State
Public Names As List(Of String)
Public StringBuilder As New System.Text.StringBuilder
Public Incomplete As Integer
Public Taken As Integer
Public FileName As String
End Class
Private Sub BackgroundWorker1_DoWork(sender As System.Object, e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
Dim state = CType(e.Argument, State)
For Each i As String In state.Names
Using cli = New System.Net.WebClient()
Dim cResult = cli.DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If cResult = "nottaken" Then
state.StringBuilder.Append(i & vbNewLine)
Else
state.Incomplete = state.Incomplete + 1
state.Taken = state.Names.Count - state.Incomplete
End If
End Using
Next
IO.File.WriteAllText(state.FileName, state.StringBuilder.ToString)
End Sub
Private Sub BackgroundWorker1_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
If e.Error IsNot Nothing Then
MessageBox.Show(e.Error.ToString(), "Error")
Else
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
End If
Button6.Enabled = True
End Sub
The SaveFileDialog can be used as "Using directive"
Why do you create a webrequest on the one hand, and on the other hand use a webclient? That does not make sense at all.
Instead of your strange if condition, write:
flag = (cResult.Equals("nottaken"))
All code you want to run after the actions you are currently running in your thread have to be in your thread as well, because it is async.
You have to invoke if you use user controls within a thread
and so on..
Please turn Option Strict On and Option Infer Off
There are lots of other things you could do better.
Please remove the webrequest and webclient combination yourself, that does not make sense at all.
Take a look at this, I cleared it a little bit:
Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
Dim Incomplete As Integer = 0
Dim Taken As Integer = 0
Dim sb As New StringBuilder
Using goodUsers As SaveFileDialog = new SaveFileDialog()
goodUsers.Filter = "TXT file (*.txt)|*.txt"
If not goodUsers.ShowDialog() = DialogResult.OK Then Exit Sub
Dim checkerMT As New Thread(
Sub()
Me.invoke(sub()
Button6.Enabled = False
For Each i As String In UsernameList.Items
WebRequest.Create("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString)
Dim cResult As String = New System.Net.WebClient().DownloadString("http://yatobooter.cf/other/checkusr.php?username=" + i.ToString).ToString
If (cResult.toLower().Equals("nottaken")) Then
sb.Append(String.Concat(i , Environment.NewLine)
Else
Incomplete += 1
Taken = UsernameList.Items.Count - Incomplete
End If
Next
File.WriteAllText(goodUsers.FileName, sb.ToString)
Button6.Enabled = True
MessageBox.Show("Checking available usernames, complete!", "NameSniper Pro")
End Sub)
End Sub
)
checkerMT.IsBackground = True;
checkerMT.Start()
End Sub

Datagridview strange display error on init

upon initialization of my UI i get the following very strange behavior regarding to the drawing of the datagridview:
Basically, expect the first row header and the column headers (which i did not include in the pic) it looks like the DGV prints what is on the Screen "behind" his application.
What the hell is this and does anyone know a way to fix it?
Code of Container:
Public Class DGVControl
Dim dt As DataTable
Public Sub init()
dt = New DataTable
Dim arr(ldfAttributes.Count - 1) As String
Dim cms As New ContextMenuStrip
Dim i As Integer = 0
For Each att As String In Attributes.Keys
Dim cmsitem As New ToolStripMenuItem
dt.Columns.Add(att, GetType(String))
cmsitem.Name = att
cmsitem.Text = att
cmsitem.Owner = cms
cmsitem.CheckOnClick = True
cmsitem.Checked = my.Settings.shownColumns.Contains(att)
AddHandler cmsitem.CheckedChanged, AddressOf showOrHideColumn
cms.Items.Add(cmsitem)
arr(i) = "No Data"
i += 1
Next
For i = 1 To my.settings.anzEntries
dt.Rows.Add(arr)
Next
MainDGV.DataSource = dt
MainDGV.ContextMenuStrip = cms
For Each attName as String In Attributes.key
showOrHideColumn(cms.Items(attName), New EventArgs())
Next
MainDGV.RowHeadersWidth = 90
MainDGV.RowTemplate.Height = 40
MainDGV.RowHeadersDefaultCellStyle.BackColor = Color.White
MainDGV.RowHeadersDefaultCellStyle.Font = New Font(MainDGV.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
MainDGV.ColumnHeadersDefaultCellStyle.BackColor = Color.White
MainDGV.ColumnHeadersDefaultCellStyle.Font = New Font(MainDGV.ColumnHeadersDefaultCellStyle.Font, FontStyle.Bold)
MainDGV.BackgroundColor = Color.White
End Sub
Private Sub showOrHideColumn(sender As Object, e As EventArgs)
Dim cmsitem As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
MainDGV.Columns(cmsitem.Name).Visible = cmsitem.Checked
End Sub
'taken from: http://stackoverflow.com/questions/710064/adding-text-to-datagridview-row-header
Private Sub nameRowHeaders(sender As Object, e As EventArgs) Handles MainDGV.DataBindingComplete
Dim dgv As DataGridView = CType(sender, DataGridView)
For i As Integer = 0 To dgv.RowCount - 1
dgv.Rows(i).HeaderCell.Value = ("Entry " &(i+1).toString())
Next
End Sub
End Class
EDIT:
As soon as you once select a row, all cells will be displayed in the right way until you restart the application

For Loop works not working, or giving error. Works when conditions are reversed

I don't know if its the late hour, but I am working on the following array For Loop:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is My.Resources.list Then
pboxes(i).Image = Nothing
End If
Next
End Sub
The loop is supposed to check if any of the picture boxes in the array have an Image called List stored on the Resources folder in them. If it does, set the image to Nothing. However, I run it and nothing happens., no errors, nothing.
So I reversed my For Loop as follows to see what happens:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If pboxes(i).Image Is Nothing Then
pboxes(i).Image = My.Resources.list
End If
Next
End Sub
This works but is not what I want, I want the opposite.
What am I doing wrong here?
One option is if you set the pictures in the box programatically, set My.Resources.list to be referenced by a global variable, ie Public pbList = My.Resources.list
Then, when you set the picture initially, use that variable, so: picMainImage.Image = pbList
Finally, in your If statement, you should then be able to check If pboxes(i) is pbList Then...
Once it becomes a variable, it seems to become static and therefore wherever you use it, it will always be the same.
EDIT: some actual code that I used a few months back:
In the module (outside sub)
Public pbimage As System.Drawing.Image = My.Resources.placeholder
Then in the Sub
If imgpath <> "" Then
Me.lblImg.ImageLocation = imgpath
Else
Me.lblImg.ImageLocation = Nothing
Me.lblImg.Image = pbimage
End If
and then this is what I use for all pictures without issues (its a function that I have run when you click on an image - if its the placeholder then you can browse for an image and save it to a data folder, otherwise it does nothing)
Private Sub changeImg(sender As Object, e As MouseEventArgs) Handles {ALL YOUR IMAGES}.Click
If TypeOf sender Is PictureBox Then
If DirectCast(sender, PictureBox).Image Is pbimage Then
Dim ofd As New OpenFileDialog
ofd.Title = "Please select image"
ofd.Filter = "Image Files|*.jpg"
If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then
Dim rn As New Random
Dim r As Long = rn.Next(111111, 999999)
Dim newfilename As String = My.Settings.dataPath & r.ToString & Format(Now, "ddmmyy") & ".jpg"
Try
FileCopy(ofd.FileName, newfilename)
DirectCast(sender, PictureBox).ImageLocation = newfilename
Catch ex As Exception
MessageBox.Show("Check permissions to the Data folder", "Permissions error")
End Try
End If
End If
End If
End Sub
Images cannot be compared that way because the image is copied into memory and will always be different even if the pixels match. Compare the pixels directly to find out if the image is the same.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pboxes() As PictureBox = {picMainImage, picImage2, picImage3, picImage4}
For i As Integer = 0 To pboxes.Count - 1
If IsSameImage(pboxes(i).Image, My.Resources.list) = True Then
pboxes(i).Image = Nothing
End If
Next
End Sub
Public Function IsSameImage(ByVal oBitmap1 As Bitmap, ByVal oBitmap2 As Bitmap) As Boolean
For x = 0 To oBitmap1.Width - 1
For y = 0 To oBitmap2.Height - 1
If Not oBitmap1.GetPixel(x, y) = oBitmap2.GetPixel(x, y) Then
Return False
End If
Next
Next
Return True
End Function

Printing multiple pages from an ArrayList

First let me start off with I am not a vb.net developer. In fact I have never been trained in the art of VB. That being said I am working on a very simple application that takes a csv file and parses a single column to an array list. Now i need to take that array list and print a individual page (Without preview) of each item on the array list. So each item on the array list will have its own page.
Heres what i have so far. Im sure Im way off seeing as I cant figure out how to turn this into multi-pages.
Private Sub Print()
Dim PrintPreviewSelected As Boolean = False
'Set the doc to print
Dim pDoc As New PrintDocument
pDoc.PrintController = New StandardPrintController 'turns off the printing page x of y dialog
Try
Using sr As New StreamReader(file)
defPrinter = sr.ReadToEnd()
End Using
Catch e As Exception
End Try
If defPrinter = "" Then
If Me.PrintDialog1.ShowDialog() = DialogResult.OK Then
pDoc.PrinterSettings.PrinterName = Me.PrintDialog1.PrinterSettings.PrinterName
End If
Else
pDoc.PrinterSettings.PrinterName = defPrinter
End If
pDoc.DefaultPageSettings.Landscape = True
pDoc.DefaultPageSettings.Margins = New Margins(40, 10, 10, 10)
pDoc.OriginAtMargins = True
AddHandler pDoc.PrintPage, AddressOf PrintSett
If PrintPreviewSelected Then
PrintPreviewDialog1.Document = pDoc
PrintPreviewDialog1.UseAntiAlias = True
PrintPreviewDialog1.WindowState = FormWindowState.Maximized
PrintPreviewDialog1.ShowDialog()
Else
If txtFile.Text <> "" Then
pDoc.Print()
Else
MessageBox.Show("You must select a file first", "Select a file.")
End If
End If
RemoveHandler pDoc.PrintPage, AddressOf PrintSett
End Sub
Private Sub PrintSett(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim fnt10 As Font = New Font("Courier New", 34, FontStyle.Regular)
e.Graphics.DrawString("", fnt10, Brushes.Black, 318, 412)
End Sub
Any help would be appreciated! I know i havent laid any of the foundation for you guys to work off of but frankly Im strait up lost. Thanks guys!
Following matzone's suggestion I was able to figure it out.
Dim PageNumber As Integer = 1
Dim morePage As String
Private Sub PrintSett(ByVal sender As Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs)
Dim ReportFont As New Font("Arial", 45, FontStyle.Regular)
Dim VerticalPrintLocationSingle As Single = 412
Dim HorizontalPrintLocationSingle As Single = e.MarginBounds.Left
Dim TextString As String
Dim sngCenterPage As Single
If customerList.Count > (PageNumber) Then
If customerList.Item(PageNumber) IsNot "" Then
TextString = customerList.Item(PageNumber)
Console.WriteLine(customerList.Item(PageNumber))
sngCenterPage = Convert.ToSingle(e.PageBounds.Width / 2 - e.Graphics.MeasureString(customerList.Item(PageNumber), ReportFont).Width / 2)
PageNumber += 1
morePage = True
End If
Else
morePage = False
customerList.Clear()
End If
e.Graphics.DrawString(TextString, ReportFont, Brushes.Black, sngCenterPage, VerticalPrintLocationSingle)
e.HasMorePages = morePage
End Sub
Thanks again!