Visual Basic 2010 Listbox Error - visual-studio-2010

what is the meaning of this error? sorry it's my first time to use Visual basic 2010, I'm not familiar with this kind of error, I use this for selecting all the files in the listbox and tried to move or copy to another listbox in other form.
Error 1 'ToArray' is not a member of
'System.Windows.Forms.ListBox.ObjectCollection'.
This is the code I use.
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If RadioButton1.Checked Then
Dim itemsToMove = ListBox1.Items.ToArray()
For Each item In itemsToMove
Form2.lstP.Items.Add(item)
ListBox1.Items.Remove(item)
Next
Form2.Show()
End If
End Sub
Can someone help me with this?

What the error means is that Listbox.ObjectCollection does not have a method or property called ToArray. So, you can't call the ToArray on this collection. It is not clear why you want to do this anyway.

There is no need to cast the ListBox to an array. If you need to know the number of items in the list you can do the following
Dim itemsToMove As Integer = ListBox1.Items.Count
Otherwise that line of code Dim itemsToMove = ListBox1.Items.ToArray() is not necessary. You can simply use
For Each item In ListBox1
Form2.lstP.Items.Add(item)
ListBox1.Items.Remove(item)
Next

Related

How do I add a save feature to a datagridview?

Hi i am fairly new to visual studio and I am having some trouble adding a save feature to my program. Basically what my program does is set event reminder for the user (it's like a daily planner but with no notifications). I have got the "add event', "delete", and "update buttons to work on the program and now all I have left is the "save" and "load" key. What I am trying to do is find a way to save the DataGridView so it can be opened back up in the program at a later date using the "load" key. If it would be easier to just remove the "load" and save the info right into the event reminder application, I could go that route but I don't have the first idea how to do that. This is what I have right now for the code in the main form:
Public Class MainForm1
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If DataGridView1.Columns(e.ColumnIndex).Name = "Delete" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then
Me.DataGridView1.EndEdit()
Me.DataGridView1.Rows.RemoveAt(e.RowIndex)
End If
If DataGridView1.Columns(e.ColumnIndex).Name = "Column4" AndAlso Me.DataGridView1.Rows(e.RowIndex).IsNewRow = False Then
Dim Update As UpdateWindow
Update = UpdateWindow
Update.Show()
End If
End Sub
Private Sub dltBtn_Click(sender As Object, e As EventArgs)
Dim dltBtn As dltWindow
dltBtn = dltWindow
dltBtn.Show()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Button1 As addBtn
Button1 = addBtn
Button1.Show()
End Sub
Private Sub UptBtn_Click(sender As Object, e As EventArgs)
Dim UptBtn As UpdateWindow
UptBtn = UpdateWindow
UptBtn.Show()
End Sub
Dim thisFilename As String = Application.StartupPath & "\Event reminder.dat"
Private Sub saveBtn_Click(sender As Object, e As EventArgs) Handles saveBtn.Click
Me.Validate()
Me.SaveGridData(DataGridView1, thisFilename)
End Sub
Private Sub BtnLoad_Click(sender As Object, e As EventArgs) Handles BtnLoad.Click
Me.LoadGridData(DataGridView1, thisFilename)
End Sub
Private Sub SaveGridData(ByRef ThisGrid As DataGridView, ByVal thisFilename As String)
ThisGrid.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText
ThisGrid.SelectAll()
IO.File.WriteAllText(thisFilename, ThisGrid.GetClipboardContent().GetText.TrimEnd)
ThisGrid.ClearSelection()
End Sub
Private Sub LoadGridData(ByRef ThisGrid As DataGridView, ByVal Filename As String)
ThisGrid.Rows.Clear()
For Each THisLine In My.Computer.FileSystem.ReadAllText(Filename).Split(Environment.NewLine)
ThisGrid.Rows.Add(Split(THisLine, " "))
Next
End Sub
End Class
I have gone on the other forums and asked about how to do this and they all said the bind the datagridview to a data table. If that is the route I have to go, how do I go about it? If anyone has some examples or code I could try out I would be much appreciative.
The problem I can see in the posted code is between the LoadGridData and the SaveGridData methods. When the LoadGridData method runs, it reads a file where each line is “Split” on spaces “ “. This is fine and if the file is properly “space” delimited it works as expected. So let us assume everything works and the data displays in the grid properly.
Then the user presses the saveBtn which calls the SaveGridData method. The code appears to “select” all the cells in the DataGridView to the clipboard, then proceeds to write the clipboard text to the SAME file name used when loading the grid. This also appears to work as expected.
The problem with this is that when the code copies all the cells of the DataGridView to the clipboard then, writes this copied clipboard text; it will use a “Tab” delimiter. The file Event reminder.dat which was read in using a “space” delimiter will be overwritten with a “tab” delimiter and not a “space” delimiter. Therefore, then next time the load method is run… it will fail.
If the load and save button are going to read the same file, then they MUST both agree on what character is used as a delimiter. You can use whatever character you want as a delimiter. You can use a “space” as the posted code does, however this limits each type to a string with no spaces. If one of the fields is a home address, chances are good, there will be a space in the string. Using a “space” as a delimiter is not necessarily the best of choices.
A simple solution is available with the current code. Currently if the file Events reminder.dat is space delimited and reads in properly to the data grid view, then it should be easy to replace the space delimiter with something else. In this case, since the SaveGridData method is delimiting the fields with a “tab” then we simply need to change the load method to “split” on “Tabs” and not “spaces” when reading back the file.
Step 1 – make a copy of Events reminder.dat
Step 2 – Run the program, press the load button and make sure all the data is properly loaded into the grid.
Step 3 – If the data is displayed properly in the grid, press the save button. (Updates file with tab delimiters)
Step 4 – Exit the program and make the change below in the LoadGridData method.
ThisGrid.Rows.Add(Split(THisLine, vbTab))
Step 5 – Run the program. Now both the load and write methods agree on what character (Tab) is used as a delimiter.
Lastly, to comment on using a DataTable the answer would be yes. DatagridViews and DataTables play nicely together. Hope this helps.
You may want to take a “Tour” of Stackoverflow to see how it works.

Conversion.VAL method - Global Hook

Our application is very large and we have been using VAL method a lot to ensure that data input in the textboxes is converted properly into related numbers. However, now we ran into few issues. For example, if someone writes 25,500, the VAL function will return 25 instead of 25500. Seems like it is expected behavior as VAL method stops proceeding once it find nonconvertible character like comma.
I would like to know if there is a way to have a global hook for this method I can create in my application, so that whenever I call a VAL method from my application, it should call my defined method, instead of calling from the Microsoft.Visualbasic namespace.
thank you
With a textbox a button and a label on the form this seems to do the trick.
Make a convert sub and call it.
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim result, middle As String
Dim newresult As Double
If TextBox1.Text Is Nothing Then
MsgBox("Empty TextBox!")
End If
result = (TextBox1.Text)
middle = result.Replace(",", String.Empty)
newresult = Val(middle)
Label1.Text = newresult.ToString
TextBox1.Text = Nothing
End Sub
Hopefully this helps. newresult is preserved as a double for further use, and the value is displayed only for clarity in this example

Handling GotFocus / LostFocus events on all TextBoxes in a Form VB6

I would like to create a handler which listens GetFocus / LostFocus events for all TextBoxes in a Form using VB6 how can I achieve that?
What i tried so far:
Option Explicit
Dim Cnt As Control
Private WithEvents Txt As VB.TextBox
Private Sub Form_Load()
For Each Cnt In Me.Controls
If TypeOf Cnt Is TextBox Then
Set Txt = Cnt
End If
Next Cnt
End Sub
Private Sub Txt_GotFocus()
Txt.BackColor = &H80000018
End Sub
Private Sub Txt_LostFocus()
Txt.BackColor = &H80000005
End Sub
but this only works for one TextBox in the Form
this only works for one TextBox in the Form because Txt can only refer to one textbox at a time.
One way to have a common handler is to create your Texboxes as a control array. Give them all the same name (ie txtBox). VB will automatically make an array of them. You can control their order in the array using the Index property. Now, your LostFocus will look like this:
Private Sub txtBox_LostFocus(Index As Integer)
txtBox(Index).Backcolor = &H80000005
End Sub
If you need to change what you do based on WHICH textbox it is, use the Index to tell which one it is. NOTE: Control arrays are quite handy, but they disappear in VB.NET. There are some equivalent methods but I would not get too attached to the exact way they work.
For more complex ops, the several events can call a common procedure passing the control as an argument.

vb2010 - TextBox1.Text from MS Access record

I am new to vb2010 and have been successful with google searches for all my previous problems but this one i cannot make any traction on so I am looking for a little help on the correct syntax.
Public Class Form2
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'IRuleDataSet.Stnds' table. You can move, or remove it, as needed.
Me.StndsTableAdapter.Fill(Me.IRuleDataSet.Stnds)
'Populates listbox1 with data from iRuleDataSet table
ListBox1.DataSource = IRuleDataSet.Stnds
ListBox1.DisplayMember = "Standards"
ListBox1.SelectedIndex = 0
End Sub
Private Sub StndsBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.StndsBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.IRuleDataSet)
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Label2.Text = ListBox1.Text & " IRule"
'~~>This is where I am stuck
'~~> TextBox1.Text = IRuleDataSets.Stnds.IRule.ListBox1.SelectedIndex bah i can't get this ):
End Sub
End Class
in form 2, I have loaded an access database into a DataSet labeld IRuleDataSet.Stnds
Table = Stnds
Field = IRule
Record Position = ListBox1.SelectedIndex
what would be the correct syntax or naming convention/logic to have the corresponding text for the record position in Field: "IRule" that the user selects in ListBox1 to display in TextBox1?
This isn't for any schooling subject or anything like that. This is merely a learning exercise for myself to put together a GUI for calculations that are currently done in an overly complicated xls file at my work, so any help would be appreciated. I don't know if it is becoming too late for me or not but I just cannot seem to find the correct way to approach this seemingly simple task.
Thank you
AHA!
sure enough as soon as i post a question i find the correct solution
TextBox1.Text = IRuleDataSet.Stnds(ListBox1.SelectedIndex)(2).ToString
what this does, it sets TextBox1 text to look in to the imported access file (as IRuleDataSet.Stnds) then looks at the 'row' (ListBox1.SelectedIndex), and then the column number (2). i know this is 2 because when i put in my dummy data in the database, the field of interest was the second column.
i hope this helps future googlers (:

Awesome Visual Studio Macros [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 11 years ago.
For a small community discussion, what are some essential Visual Studio macros you use?
I just started learning about them, and want to hear what some of you can't live without.
I add buttons on the toolbar for the following 3 macros. Each will take the currently selected text in any file and google it (or MSDN-it, or spell-check-it). Make up a nifty icon for the toolbar for extra style-points.
Private Const BROWSER_PATH As String = "C:\Program Files\Mozilla Firefox\firefox.exe"
Sub SearchGoogle()
Dim cmd As String
cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
Shell(cmd, AppWinStyle.NormalFocus)
End Sub
Sub SearchMSDN()
Dim cmd As String
cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}+site%3Amsdn.microsoft.com", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
Shell(cmd, AppWinStyle.NormalFocus)
End Sub
Sub SpellCheck()
Dim cmd As String
cmd = String.Format("{0} http://www.spellcheck.net/cgi-bin/spell.exe?action=CHECKWORD&string={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
Shell(cmd, AppWinStyle.NormalFocus)
End Sub
Show build duration in the Output window
Put this code in your EnvironmentEvents module. This will write the duration directly to the build window for any action on a solution (build, rebuild, clean, deploy).
You can change the IsBuild function to specify the actions you want to see this information for.
Dim buildStart As Date
Private Function IsBuild(ByVal scope As EnvDTE.vsBuildScope, ByVal action As EnvDTE.vsBuildAction) As Boolean
Return scope = vsBuildScope.vsBuildScopeSolution
End Function
Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
If (IsBuild(Scope, Action)) Then
buildStart = Date.Now
End If
End Sub
Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
If (IsBuild(Scope, Action)) Then
Dim buildTime = Date.Now - buildStart
WriteToBuildWindow(String.Format("Build time: {0}", buildTime.ToString))
End If
End Sub
Private Sub WriteToBuildWindow(ByVal message As String)
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim ow As OutputWindow = CType(win.Object, OutputWindow)
For Each owPane As OutputWindowPane In ow.OutputWindowPanes
If (owPane.Name.Equals("Build")) Then
owPane.OutputString(message)
Exit For
End If
Next
End Sub
Show the start page after you close a solution (but keep Visual Studio open)
Put this code in your EnvironmentEvents module:
Private Sub SolutionEvents_AfterClosing() Handles SolutionEvents.AfterClosing
DTE.ExecuteCommand("View.StartPage")
End Sub
Hide the start page after you open a solution
Put this code in your EnvironmentEvents module:
Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened
Dim startPageGuid As String = "{387CB18D-6153-4156-9257-9AC3F9207BBE}"
Dim startPage As EnvDTE.Window = DTE.Windows.Item(startPageGuid)
If startPage IsNot Nothing Then startPage.Close()
End Sub
These two together will cause your Start Page to hide itself when you open a solution. When you close the solution, the Start Page comes back.
I use the following lesser-known shortcuts very often:
Ctrl+Enter: Insert a blank line above the current line (and place the cursor there)
Ctrl+Shift+Enter: Insert a blank line below the current line (and place the cursor there)
Ctrl+Shift+V: Cycles the clipboard ring
Outlining: Collapse to definitions but expand regions
Are you working in one of those shops that insists on regions around everything, so that when you collapse to definitions, you can't see any code?
What you really need is a collapse-to-definitions-but-expand-regions macro, like this one:
Sub CollapseToDefinitionsButExpandAllRegions()
DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
DTE.SuppressUI = True
Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
objSelection.StartOfDocument()
Do While objSelection.FindText("#region",
vsFindOptions.vsFindOptionsMatchInHiddenText)
Loop
objSelection.StartOfDocument()
DTE.SuppressUI = False
End Sub
Put this in a regular macro module, assign it to a hot key, and your code is back.
(Except...if you work with some really nefarious individuals who put regions inside methods, this will unfortunately expand those methods. If anybody knows a way to write this to avoid that, feel free to edit.)
Insert GUID, great for WiX work, add to menu as button or as key shortcut.
Sub InsertGuid()
Dim objTextSelection As TextSelection
objTextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
objTextSelection.Text = System.Guid.NewGuid.ToString.ToUpper(New System.Globalization.CultureInfo("en", False))
End Sub
Organise usings for all .cs files in a solution - Original Author: djpark.
Sub OrganizeSolution()
Dim sol As Solution = DTE.Solution
For i As Integer = 1 To sol.Projects.Count
OrganizeProject(sol.Projects.Item(i))
Next
End Sub
Private Sub OrganizeProject(ByVal proj As Project)
For i As Integer = 1 To proj.ProjectItems.Count
OrganizeProjectItem(proj.ProjectItems.Item(i))
Next
End Sub
Private Sub OrganizeProjectItem(ByVal projectItem As ProjectItem)
Dim fileIsOpen As Boolean = False
If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
'If this is a c# file
If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
'Set flag to true if file is already open
fileIsOpen = projectItem.IsOpen
Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
window.Activate()
projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")
'Only close the file if it was not already open
If Not fileIsOpen Then
window.Close(vsSaveChanges.vsSaveChangesYes)
End If
End If
End If
'Be sure to apply RemoveAndSort on all of the ProjectItems.
If Not projectItem.ProjectItems Is Nothing Then
For i As Integer = 1 To projectItem.ProjectItems.Count
OrganizeProjectItem(projectItem.ProjectItems.Item(i))
Next
End If
'Apply RemoveAndSort on a SubProject if it exists.
If Not projectItem.SubProject Is Nothing Then
OrganizeProject(projectItem.SubProject)
End If
End Sub
Collapse all nodes of the Solution panel, very useful especially for big projects:
Public Module CollapseAllNodes
Sub RunCollapseAllNodes()
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
Return
End If
' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
CloseRecursif(UIHSolutionRootNode)
' Select the solution node, or else when you click
' on the solution windows scrollbar, it will synchronize the open document
' with the tree and pop out the corresponding node which is probably not
' what you want.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
Function CloseRecursif(ByRef element)
For Each UIHChild In element.UIHierarchyItems()
CloseRecursif(UIHChild)
If (UIHChild.UIHierarchyItems.Expanded = True) Then
UIHChild.UIHierarchyItems.Expanded = False
End If
Next
End Function
End Module
I use Jeff's FormatToHtml macros if I'm going to be pasting a code example into a blog post or an email.
I work with dual monitors, and I find Sharon's layout-switching macro (from a 1 monitor to a 2 monitor layout) totally invaluable. When you need to be referencing a web page or other program while typing a bit of code, Ctrl-Alt-1 to switch to a one monitor layout for your Visual Studio windows. Once you're done, Ctrl-Alt-2 to switch to your two monitor layout and get all your windows back. Awesome!
http://www.invisible-city.com/sharon/2008/06/workstation-hack-visual-studio-on-2.html
Not a macro on its own, but useful:
Public Sub WriteToOutputWindow(ByVal pane as String, ByVal Msg As String)
Dim owPane As OutputWindowPane
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim ow As OutputWindow = win.Object
Try
owPane = ow.OutputWindowPanes.Item(pane)
Catch
owPane = ow.OutputWindowPanes.Add(pane)
End Try
If Not owPane Is Nothing Then
owPane.Activate()
owPane.OutputString(Msg & vbCrLf)
End If
End Sub
I mapped ctrl-shift-G to a macro that generates a GUID in registry format - this is useful for editing IDL
I'm currently working on two different projects with different coding standards, one that uses tabs for line beginnings and another that uses spaces. This macro will toggle between which standard is used based on which environment is currently active:
Public Sub ToggleTabs()
If DTE.ActiveDocument.Language = "CSharp" Then
Dim currentSetting As Boolean = DTE.Properties("TextEditor", "CSharp").Item("InsertTabs").Value
DTE.Properties("TextEditor", "CSharp").Item("InsertTabs").Value = Not currentSetting
End If
If DTE.ActiveDocument.Language = "SQL" Then
Dim currentSQLSetting As Boolean = DTE.Properties("TextEditor", "SQL").Item("InsertTabs").Value
DTE.Properties("TextEditor", "SQL").Item("InsertTabs").Value = Not currentSQLSetting
End If
If DTE.ActiveDocument.Language = "HTML" Then
Dim currentHTMLSetting As Boolean = DTE.Properties("TextEditor", "HTML").Item("InsertTabs").Value
DTE.Properties("TextEditor", "HTML").Item("InsertTabs").Value = Not currentHTMLSetting
End If
If DTE.ActiveDocument.Language = "JScript" Then
Dim currentJScriptSetting As Boolean = DTE.Properties("TextEditor", "JScript").Item("InsertTabs").Value
DTE.Properties("TextEditor", "JScript").Item("InsertTabs").Value = Not currentJScriptSetting
End If
End Sub
I used to employ a lot of macros in VS 2002/2003. One example would be Region creation - I always like my classes to be divided into the following regions - "Private members", "Public Properties", "Public Methods" and "Private methods". So, I have a macro mapped to a shortcut key that creates these regions in any new class file.
Refactoring support in VS 2005/2008 (and the facility of adding common code snippets) as well as the use of Addins like DXCore and SlickEdit allow me to work without having to create too many macros anymore.
I couldn't let this question go without mentioning this one. It even has a video to show how to install and use it. This macro simply allows you to create the nested files in the solution explorer (like resources.resx).
Edit: Updated the link

Resources