I am writing a macro to automate tasks on a spreadsheet in LibreOffice Calc.
One of those tasks is simply to add the numbers contained in a given cell range and write the total in the appropriate cell when one of these cells is edited. (The cells actually contain text: the names of different services. The program then fetches the number of hours associated with each service's name to add them all up.)
Editing such a cell triggers the Modify_modified(oEv) event listener.
The listener then calls the subroutine UpdateTotalHoursOfAgent(calendarSize, allServices, agentTopleftCell) which performs the task described above.
The problem is that arguments calendarSize and allServices, which are defined in other places in the code, are out of scope in the event listener.
I do not know how to pass those arguments to the listener.
I tried using global variables instead even though it is frowned upon, but I suspect that they reach the end of their lifetime when the main program's execution is complete, and are not available anymore when a cell is edited afterwards.
How can I pass arguments calendarSize and allServices to the UpdateTotalHoursOfAgent subroutine when Modify_modified(oEv) is triggered?
Here's part of the code used to create the event listener (found on a forum):
Private oListener, cellRange as Object
Sub AddListener
Dim sheet, cell as Object
sheet = ThisComponent.Sheets.getByIndex(0) 'get leftmost sheet
cellRange = sheet.getCellrangeByName("E4:J5")
oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener") 'create a listener
cellRange.addModifyListener(oListener) 'register the listener
End Sub
Sub Modify_modified(oEv)
' *Compute agentTopleftCell*
REM How to obtain calendarSize and allServices from here?
UpdateTotalHoursOfAgent(calendarSize, allServices, agentTopleftCell)
End Sub
Sub Main
' *...code...*
Dim allServices As allServicesStruct
Dim calendarSize As calendarStruct
AddListener
' *...code...*
End Sub
I tried using global variables...
Probably you did not do it correctly. Here is how to set a global variable.
Private oListener, cellRange as Object
Global AllServices
Type allServicesStruct
svc As String
End Type
Sub AddListener
Dim sheet, cell as Object
sheet = ThisComponent.Sheets.getByIndex(0) 'get first sheet
cellRange = sheet.getCellrangeByName("E4:J5")
oListener = createUnoListener("Modify_","com.sun.star.util.XModifyListener")
cellRange.addModifyListener(oListener)
End Sub
Sub Modify_modified(oEv)
MsgBox AllServices.svc
End Sub
Sub Main
Dim allServicesLocal As allServicesStruct
allServicesLocal.svc = "example"
AllServices = allServicesLocal
AddListener
End Sub
Result:
This was adapted from my answer at https://stackoverflow.com/a/70405189/5100564
Bit of a rookie issue here. How do you deal with data verification using Access Events? The problem is that when I use SetFocus to return the Cursor to the field with the errant data, Access goes through the _Exit and/or _LostFocus Events of the next Control in the Tab Order. If those include data validation procedures, the desired SetFocus is circumvented.
Using techturtles answer, I came up with this "solution" (read "hack").
Private Sub ServicingEmployee_LostFocus() 'Check data validity
If dataEntryCancelled Then Exit Sub
Dim cntrl As Control
Set cntrl = Me.ServicingEmployee
Dim myResponse As Integer
If IsNull(cntrl.Value) Then
myResponse = MsgBox("This field cannot be left blank.", vbOKOnly, "Enter Date Collected")
dataErrorInPreviousField = True
**'Next line sets a Public Control**
Set controlWithDataEntryError = Me.ServicingEmployee
End If
End Sub
Private Sub Client_GotFocus() '**Check for data Error in previous Control according to Tab Order**
If dataErrorInPreviousField Then Call dataErrorProc
End Sub
Public Sub dataErrorProc()
With controlWithDataEntryError
.SetFocus
.SelStart = 0
.SelLength = Len(.Text)
End With
dataErrorInPreviousField = False
End Sub
Private Sub Client_Exit(Cancel As Integer) '**Example of Bypassing _Exit Event**
If dataEntryCancelled Or dataErrorInPreviousField Then Exit Sub
.
...
End Sub
My question is this: Is there a simpler way to accomplish this?
First, I would strongly encourage you to add table constraints in addition to your form validation, if you haven't already done so. Assuming this form is tied to a table in Access, just make sure the ServicingEmployee field (in the table) is set to required. That way, you have another level of validation making sure the field has a value.
For form validation, you can use either the form's or control's BeforeUpdate event. If you use the form's event, you only have to run the check once.
I would also suggest having a separate function (within the form) to check validity. Something like this (here, I'm using a form tied to an Employees table, with a first name and last field which should both be non-null):
Private Function isFormValid() As Boolean
' we start off assuming form is valid
isFormValid = True
If IsNull(Me.txtFirstName) Then
MsgBox "First name cannot be blank", vbOKOnly
Me.txtFirstName.SetFocus
isFormValid = False
ElseIf IsNull(Me.txtLastName) Then
MsgBox "Last name cannot be blank", vbOKOnly
Me.txtLastName.SetFocus
isFormValid = False
End If
End Function
Private Sub Form_BeforeUpdate(Cancel As Integer)
If Not isFormValid Then
Cancel = 1
End If
End Sub
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.
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
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