Empty ControlCollection at Runtime - vb.net

I can't find an answer to this anywhere. I define a new instance of a forms control collection, but at runtime, the collection is empty. It works for one load button on the form, but not another. The code is exactly the same, but one works, the other doesn't. Here is the relevant code:
Private Sub miFLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miFLoad.Click
Dim FilePath As String = "C:\FList\FList.flt"
Dim LoadFile As New SaveandLoad.SaveAndLoad
Dim FileRead As New Simple3Des("MyPassword")
Dim FileString As String = FileRead.ReadFile(FilePath)
With LoadFile
.WhichList = dgFList
.FilePath = FilePath
.DecryptedString = FileRead.DecryptData(FileString)
End With
Call LoadFile.LoadFile()
End Sub
This load button not loading
Private Sub miCLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miCLoad.Click
Dim FilePath As String = "C:\FList\CList.clt"
Dim LoadFile As New SaveandLoad.SaveAndLoad
Dim FileRead As New Simple3Des("MyPassword")
Dim FileString As String = FileRead.ReadFile(FilePath)
With LoadFile
.WhichList = dgCourses
.FilePath = FilePath
.DecryptedString = FileRead.DecryptData(FileString)
End With
Call LoadFile.LoadFile()
End Sub
This one is.
Public Sub LoadFile()
Dim dgRow As DataGridViewRow
Dim dgCell As DataGridViewTextBoxCell
Dim Lines() As String = DecryptedString.Split(vbLf)
Dim LinesList As List(Of String) = Lines.ToList
LinesList.RemoveAt(Lines.Length - 1)
For Each Line As String In LinesList
Dim Fields() As String = Line.Split(",")
dgRow = New DataGridViewRow
For x = 0 To (WhichList.Columns.Count - 1) Step 1
dgCell = New DataGridViewTextBoxCell
dgCell.Value = Fields(x).ToString
dgRow.Cells.Add(dgCell)
Next
WhichList.Rows.Add(dgRow)
Next
Dim FormControls As New frmFacultyList.ControlCollection(frmFacultyList)
For Each DGV As DataGridView In FormControls
If WhichList.Name = DGV.Name Then
DGV = WhichList
DGV.Refresh()
End If
Next
End Sub
Here is where they pass the info to. Again, the FormControls variable is empty for FLoad button click, but not for CLoad button click. Any help would be appreciated.
Edit: Sorry, here are the relevant Public Properties
Public Property WhichList As New DataGridView
Public Property FilePath As String
Public Property DecryptedString As String
Public Property EncryptedString As String

Turns I solved my own problem. The Save Function wasn't writing to the file correctly, so it wasn't pulling the information correctly. Fixed.

Related

Visual Basics, Store comboBox value in variable

I want to save the comboBox value in a variable.But whenever I change the comboBox value the value gets set is null, and the selected index shows as '-1'.Below is my code.
Private Sub SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim form As CreateEvalForm = New CreateEvalForm //windows Form
Dim str As String = form.ComboBox1.SelectedIndex
MessageBox.Show(str) //shows null
Dim openingId As Integer = Val(form.ComboBox1.Text)
End Sub
Could anyone please suggest a solution?
Private Sub SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim str As String = ComboBox1.SelectedIndex
MessageBox.Show(str)
Dim openingId As Integer = Val(ComboBox1.Text)
End Sub
Does this work? But if you wanted a value from the combobox's selected value, you should try the following:
dim openingId as Integer = Val(comboBox1.SelectedValue)
Dim form As CreateEvalForm = New CreateEvalForm //windows Form
With this line you are creating a new form, so the combobox on that new form will be new as well and will not have a selected index.
You can just use the combobox in this way:
Dim str As String = ComboBox1.SelectedIndex
or
Dim str As String = Me.ComboBox1.SelectedIndex

How to display a random photo from a folder in a picture box and print out the photo name without extension

I have a folder named "MyPhotos", I want to display a random photo at each time a command button is clicked from "Myphoto" and print out the name of the displayed photo in the form without extension . I'm trying the following code, but still can't get the photo name
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
Dim DirectoryPath As String = Application.ExecutablePath
Directorypath = DirectoryPath.Substring(0, Directorypath.LastIndexOf("\bin")) & "\MyPhotos"
Dim bm As New Bitmap(GetRandomImageFilePath(Directorypath))
picImage.Image = bm
End Sub
Public Function GetRandomImageFilePath(ByVal folderPath As String) As String
Dim files() As String = Directory.GetFiles(folderPath, "*.jpg")
Dim random As Random = New Random()
Return files(random.Next(0, files.Length))
End Function
Any help please ?
I added this to your button method. All I used was FileInfo to get the details.
Dim finfo As FileInfo = New FileInfo(GetRandomImageFilePath(DirectoryPath))
Dim filename As String = finfo.Name.Replace(finfo.Extension, "")
Dim bm As New Bitmap(finfo.FullName)
picImage.Image = bm
You already have the full path being returned by GetRandomImageFilePath.
If you want just the filename, do this in your calling Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click
Dim DirectoryPath As String = Application.ExecutablePath
Directorypath = DirectoryPath.Substring(0, Directorypath.LastIndexOf("\bin")) & "\MyPhotos"
Dim imagePath as String = GetRandomImageFilePath(Directorypath)
Dim fileInfo as New FileInfo(imagePath)
Dim imageName as String = fileInfo.Name
Dim bm As New Bitmap(imagePath)
picImage.Image = bm
End Sub

My Crystal Report is not showing the hole list

Please help with my crystal report, because it doesn't show the hole list when i use parameter fields for a list. here is my class below
`Imports System.Windows.Forms.Application
Imports System.Data
Imports System.Data.SqlClient
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Windows.Forms
Public Class CrystalReportHelperClass
Dim Sql As New SqlHelper
Dim CR_ParamDisVals As New ParameterDiscreteValue
Dim CR_ParamVals As New ParameterValues
Dim CR_ParamDef As ParameterFieldDefinition
Dim CR_ParamDefs As ParameterFieldDefinitions
Dim CRPTDoc As New ReportDocument
Dim CRPTViewer As New CrystalReportViewer
Public Sub New(ByRef CReport As CrystalDecisions.Windows.Forms.CrystalReportViewer)
CRPTViewer = CReport
CRPTDoc = CReport.ReportSource
End Sub
Public Sub CrystalObjectParam(ByVal CrystalReportObject As String, ByVal Message As String)
CR_ParamDisVals.Value = Message
CR_ParamDefs = CRPTDoc.DataDefinition.ParameterFields
CR_ParamDef = CR_ParamDefs.Item(CrystalReportObject)
CR_ParamVals = CR_ParamDef.CurrentValues
CR_ParamVals.Clear()
CR_ParamVals.Add(CR_ParamDisVals)
CR_ParamDef.ApplyCurrentValues(CR_ParamVals)
End Sub
Public Function GetCrystalReport() As ReportDocument
Return CRPTDoc
End Function
End Class
`
then here is the form load
Private Sub ReportEmployeeList_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim RepDoc As New ReportDocument
RepDoc = CRPT_Employee.ReportSource
If Sql.HasConnection() = True Then
Dim DT As DataTable = Sql.ExecuteDataTableSP("SelectWorking")
For Each Data As DataRow In DT.Rows
CRPT.CrystalObjectParam("ID", Data(1))
Next
Else
MsgBox("System Database Cannot be Connected", MsgBoxStyle.Information)
End If
CRPT_Employee.ReportSource = CRPT.GetCrystalReport
CRPT_Employee.Refresh()
End Sub
then i have 1 parameter field in my crystal report, id -> discrete value
what should i do?
i already used other methods like this one
Private Sub SetCurrentValuesForParameterField(ByVal myParameterFields As ParameterFields, ByVal myArrayList As ArrayList)
Dim currentParameterValues As ParameterValues = New ParameterValues()
For Each submittedValue As Object In myArrayList
Dim myParameterDiscreteValue As ParameterDiscreteValue = New ParameterDiscreteValue()
myParameterDiscreteValue.Value = submittedValue.ToString()
currentParameterValues.Add(myParameterDiscreteValue)
Next
Dim myParameterField As ParameterField = myParameterFields(PARAMETER_FIELD_NAME)
myParameterField.CurrentValues = currentParameterValues
End Sub
modify your method as below if i understood what do you want>>>
Dim DT As DataTable = Sql.ExecuteDataTableSP("SelectWorking")
For Each Data As DataRow In DT.Rows
CRPT.SetParameterValue("ID", Data(1))

VB.net Multiple StreamReader and FileStream in a single button

I want to create multiple of these readers but my program only reads the first filestream is there a way for it to read them all? or do i have to place them in different buttons? here is my current code, :
Public aRecp As String()
Public listRecp As New List(Of String)
Public aEmail As String()
Public listEmail As New List(Of String)
Public aName As String()
Public listName As New List(Of String)
Public sArray As String()
Public sList As New List(Of String)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim fStream As New System.IO.FileStream("messages.txt", IO.FileMode.Open)
Dim sReader As New System.IO.StreamReader(fStream)
Dim Index As Integer = 0
Do While sReader.Peek >= 0
sList.Add(sReader.ReadLine)
Loop
sArray = sList.ToArray
fStream.Close()
sReader.Close()
Dim StreamName As New System.IO.FileStream("sendername.txt", IO.FileMode.Open)
Dim ReaderName As New System.IO.StreamReader(StreamName)
Dim IndexName As Integer = 0
Do While ReaderName.Peek >= 0
listName.Add(sReader.ReadLine)
Loop
aName = listName.ToArray
StreamName.Close()
ReaderName.Close()
Dim StreamEmail As New System.IO.FileStream("senderemail.txt", IO.FileMode.Open)
Dim ReaderEmail As New System.IO.StreamReader(StreamEmail)
Dim IndexEmail As Integer = 0
Do While ReaderEmail.Peek >= 0
listEmail.Add(sReader.ReadLine)
Loop
aEmail = listEmail.ToArray
StreamEmail.Close()
ReaderEmail.Close()
Dim StreamRecp As New System.IO.FileStream("recpname.txt", IO.FileMode.Open)
Dim ReaderRecp As New System.IO.StreamReader(StreamRecp)
Dim IndexRecp As Integer = 0
Do While ReaderRecp.Peek >= 0
listRecp.Add(ReaderRecp.ReadLine)
Loop
aRecp = listRecp.ToArray
StreamRecp.Close()
ReaderRecp.Close()
End Sub
Not a direct answer to your question (and there's nothing obvious to me in your posted code as to why it's only executing the first reader), but since you're reading text files it'd be a lot less code to use File.ReadAllLines(fileName), like this:
Public aRecp As String()
Public aEmail As String()
Public aName As String()
Public sArray As String()
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
sArray = File.ReadAllLines("messages.txt")
aName = File.ReadAllLines("sendername.txt")
aEmail = File.ReadAllLines("senderemail.txt")
aRecp = File.ReadAllLines("recpname.txt")
End Sub
File.ReadAllLines(fileName) returns an array that has each line of the text file as an element. Much simpler than creating the stream, peeking your way through and reading each line into a list and then converting it to an array.

Open Excel Workbook from ComboBox (VB.NET)

How to open an excel workbook from combobox?
I am using following code to populate combobox with excel filenames,
Dim files() As String
files = Directory.GetFiles("C:\Files\Folder", "*.xlsx", SearchOption.AllDirectories)
For Each FileName As String In files
ComboBox1.Items.Add(FileName.Substring(FileName.LastIndexOf("\") + 1, FileName.Length - FileName.LastIndexOf("\") - 1))
Next
But I do not have any idea about how to open the selected file.
Imports System
Imports System.IO
Imports System.Collections
Public Class Form1
Private Const TargetDir = "C:\Files\Folder"
Private FullFileList As List(Of String)
Private Sub ProcessFile(ByVal fn As String)
If Path.GetExtension(fn).ToLower = ".xlsx" Then
FullFileList.Add(fn)
ComboBox1.Items.Add(Path.GetFileName(fn))
End If
End Sub
Private Sub ProcessDirectory(ByVal targetDirectory As String)
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
Dim fileName As String
For Each fileName In fileEntries
ProcessFile(fileName)
Next fileName
Dim subdirectoryEntries As String() = Directory.GetDirectories(targetDirectory)
Dim subdirectory As String
For Each subdirectory In subdirectoryEntries
ProcessDirectory(subdirectory)
Next subdirectory
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
FullFileList = New List(Of String)
ProcessDirectory(TargetDir)
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex >= 0 Then
Dim prc As New System.Diagnostics.Process
Dim psi As New System.Diagnostics.ProcessStartInfo(FullFileList(ComboBox1.SelectedIndex))
psi.UseShellExecute = True
psi.WindowStyle = ProcessWindowStyle.Normal
prc.StartInfo = psi
prc.Start()
End If
End Sub
End Class
You'll need to handle an event to react to the user's selection: either a change to the selection in the ComboBox or (better) a Button click. In your event handler you can retrieve the filename from the ComboBox:
string filename = ComboBox1.SelectedItem.ToString()
Once you have the filename, you can open the file, although how you do this will depend on what you want to do with it. This answer has some information on opening Excel files in VB.NET.
Try using
Process.start("excel "+ComboBox1.SelectedItem.ToString());
on buttonclick event