Visual Basic .NET Dynamic updating Textbox - vb.net

I just started to study Visual Basic .NET. I would like to build a dynamic system log text box.
When program is passing one point of code or function, a textbox in another form is updating with the system log.
For example, when the program starts, it appends "System start" to the textbox in another form. When the program starts to use the "cal" function, then the program appends "System: start to cal data" on the textbox in another form.
However, I am not really sure how to update this system log dynamically. Do I need to use threading? Can I do it without threading? Can anyone give me an example?

To add to Mertis's suggestion, what you can do is to fire a code which concatenates details to your textbox whenever an object is called/code is executed
for example, a function was executed:
Function sampleFunc()
.....
Textbox1.text = Textbox1.Text & vbnewline & DateTime.Now & _
" a Function was Executed"
End Function
Note that I added a datetimenow, in case you needed time details.

Related

Too many arguments to show a form, but works in another project - Almost same code

I'm trying to get started with my first add-in for Autodesk Inventor, and of course, I'm starting from an existing sample to make this task easier for a newbie like me.
I have a decent command of VB.NET, but I cannot understand what's going on here.
In this link: https://drive.google.com/drive/folders/1rs2zVzf8Ib8iwd8JjCWZ-KQbWJUAqFyJ?usp=sharing
There are two ZIP files with VS2019 Solutions:
This_Sample_Works.zip - I used this as the baseline for my new project
This_Sample_Does_Not.zip
In both, there is this line of code:
dc.Show(New WindowWrapper(ThisApplication.MainFrameHWND))
But it only compiles in the project This_Sample_Works.zip - In the project within This_Sample_Does_Not.zip, I get the error in the image below.
Frankly, I'm not even asking someone to fix it for me. I just wanted to understand why it works in one project and not in another, despite the code being virtually the same.
What is bugging me is what piece of information/skills I currently don't have to understand what the compiler is telling me.
Public Function CreateChildDialog() As Long
CreateChildDialog = Nothing
Try
'Dim dc As Object
If Not dc Is Nothing Then
dc.Dispose()
dc = Nothing
End If
dc = New dockable_form(ThisApplication)
dc.Show(New WindowWrapper(ThisApplication.MainFrameHWND))
MsgBox("Handle ID:" & dc.Handle.ToInt64(), MsgBoxStyle.OkCancel, "Debug Info")
Return dc.Handle.ToInt64()
Catch ex As Exception
MsgBox("There is problem in CreateChildDialog" & vbCrLf & vbCrLf & ex.Message & vbCrLf & vbCrLf & ex.StackTrace & vbCrLf & vbCrLf & ex.ToString)
End Try
End Function
Any help is welcome - Thanks in advance!
In your project This_Sample_Does_Not the class dockable_form is a Control (the class inherits from System.Windows.Forms.UserControl).
In the reference project This_Sample_Works the class form_dockable is a Form (the class inherits from System.Windows.Forms.Form).
The two are very different types, though a Form can behave like a Control, the opposite is not true. A control will need a containing form.
Thus, the Control does not have a Parent that can be assigned when the method Show is called, it already has a parent.
From MSDN - Control.Show Method
Showing the control is equivalent to setting the Visible property to true. After the Show method is called, the Visible property returns a value of true until the Hide method is called.
Questions for your final preferred solution:
Do you really want to create a Control that is placed on another form? or
Do you want a Form that can be shown or popped up as required?

Running a report directly and from a navigation form - reference issue

I have a form in MS Access (365) which accepts various selection criteria and runs a report with those criteria built into the wherecondition of a DoCmd.OpenReport command. The report is run via a button with On Click VBA code, since the wherecondition has to be built according to which, if any, criteria have been chosen. That all works fine when I open the selection criteria form directly. I want to open the selection criteria form via a navigation form, since there will be other, similar reports with selection criteria that I want to run from within the same navigation form.
As stated above, everything works when I open the selection criteria form directly and run the report but when I tried it via a navigation form, it didn't work. That's ok, I found a solution on the MS Dev Center site which works when I run the selection criteria form (and then the report) from the navigation form. All fine. But then (of course) the references within the button On Click code don't work when I open the selection criteria form directly and run the report. I would like to be able to run the selection criteria form and then the report from both positions - directly from MS Access and via the navigation form. There will presumably be some way to achieve this but (as I said above, I am new to MS Access and VBA) I could spend a lot of time clutching at shadows. Hopefully, someone will be able to tell me the simplest way to do this?
Code sample with relevant comments below. On runnning the selection criteria form and report directly, the line commented as AAA works ok, MsgBox ABB and DDD and those beyond all show; on running them via the navigation form, line BBB works ok, MsgBox ABB and DDD and those beyond all show (well, they would except that I haven't yet coded the [NavigationSubform] option into all the other whereconditon building stuff). When I switch the lines AAA and BBB (ie, comment the other one out) MsgBox AAA show ok, then it fails with:
Can't find referenced form "frmSelectSpeciesSiteDates"
before reaching MsgBox DDD.
MsgBox "ABB"
'If Not Forms![frmSelectSpeciesSiteDates]![cboCommonName] = "" Then
' AAA Works when frmSelectSpeciesSiteDates is run directly
If Not Forms![frmNavSelectiveReports]![NavigationSubform].[Form]![cboCommonName] = "" Then
' BBB Works when frmSelectSpeciesSiteDates is called from a navigation form
strWhereCondition = strWhereCondition & "[common name] = " & Chr(34) & Me![cboCommonName] & Chr(34)
End If
MsgBox "DDD"
You have a form named frmSelectSpeciesSiteDates which has a combobox named cboCommonName. The form also includes VBA code which uses the combobox value to modify a string variable ...
strWhereCondition = strWhereCondition & "[common name] = " & Chr(34) & Me![cboCommonName] & Chr(34)
However you only want to modify the string when the combobox contains something other than an empty string. And that is where you're struggling. You reference the combobox one way when the form is opened directly as a top-level form, and another way when it is contained in a navigation form ...
Forms![frmSelectSpeciesSiteDates]![cboCommonName]
Forms![frmNavSelectiveReports]![NavigationSubform].[Form]![cboCommonName]
I suggest you abandon both of those and refer to the combobox the same as where you modify the string (Me!cboCommonName) ...
If Not Me!cboCommonName = "" Then
strWhereCondition = strWhereCondition & "[common name] = " & Chr(34) & Me!cboCommonName & Chr(34)
End If

Multiple instances of a predesigned form in VB

Visual Studio 2010,
Visual Basic .NET
I have a form (frmImages) that opens when an image is clicked on inside a WebBrowser (wbContent) contorl on a form (frmContent).
I want the user to be able to click multiple images and open multiple instances of frmImages.
UPDATE
Here is what I am working with now, am I heading in the right direction?
This gives me the ability to open multiple instances but opens a duplicate. So for every image I click on, two identical forms open.
Dim images As New frmImages
If engineLine.IndexOf("\") <> -1 Then
images.wbImages.Navigate(New Uri(engineLine & holdHTML))
Else
images.wbImages.Navigate(New Uri(filePath & "IETMS\" & engineLine & "\" & engineLine & holdHTML))
End If
images.Text = ietmSelect & " - " & workPacket & " - " & removeTitleLinks(figTitle)
images.Show()
You just need to create another instance of the form (NEW). Of coarse, your form cannot be modal or else they will never be able to access the parent form to click another image.
Dim x as new frmImages
x.show
x = new frmImages
x.show
Create all your form instances explicitly, especially when you want more than one instance of a form. In your example, you are creating one explicitly (frmImages2), but also using the VB default instance. VB allows you to use the name of the form class as a default instance, but this is a bad practice (and a bad feature - but VB is designed around making it easier for very junior programmers even if it makes it more confusing for all the rest).

Macro to save e-mail as text file, to be used in a rule

My problem is very similar to this thread and this one. I think my issue is to combine these two questions.
I am running:
OS: Windows 7 Enterprise Professional
Outlook 2010
VBA version 7.0
By reading these two questions as well as some other pages from Microsoft and elsewhere, I was able to open the VB editor and paste into it, this simple code:
Sub SaveEmail(msg As Outlook.MailItem)
' save as text
msg.SaveAs "C:\Users\mel\mailsave\email.txt" & Format(Now, "YYYYMMDDHHMMSS"), _
olTXT
End Sub
Is the "format" portion of my msg.SaveAs line, going to save a unique text file for each email matching my rule?
How do I run this macro to test and if successful, how do I run it repeatedly?
I tried going to the run menu and selecting run "sub/user form" item but the next dialog box is asking what to run and does not populate a list of macros available for running. Clicked on "save" icon but nothing changed.
Specifying a method with that signature (Sub method (var As Outlook.MailItem)) allows you to use the method when creating a mailbox rule. As far as I understand your question, you're beyond that point.
Question 1
The format portion of your code is only going to save a unique file at most once per second. You're appending the current date and time to the file. Your main problem, however, is not the timestamp, but the file format. You should apply the timestamp before the file extension, e.g.
msg.SaveAs "C:\Users\mel\mailsave\email" & Format(Now, "YYYYMMDDHHMMSS") & ".txt", olTXT
Question 2
If you add the macro to a rule, it will be run when the rule is matched. The macro can be tested by creating a method that grabs the currently selected mail, e.g.
Sub TestSaveEmail()
Call SaveEmail(ActiveExplorer.Selection(1))
End Sub
This macro can then be run by setting the cursor within the method and pressing F5.
The macro can also be added to the Outlook user interface by customizing the ribbon and adding a macro button. For help on customizing the ribbon, refer to the following article:
Customize the ribbon

Visual Basic 2010 crashes when clicking in specific subroutine

When clicking in a specific subroutine in Visual Basic 2010, the application crashes giving only the error message "Microsoft Visual Basic 2010 [Express] has encountered a problem and needs to close."
Nothing appears in the log and safe mode doesn't help.
When I had this problem, I found it was due to a line continuation over 400 lines.
For example:
Dim someString As String
someString = "some text and then a line continuation " & _
"and then some more text for 400 lines " & _
... & _
"and then finish the string"
To fix it, I just had to make my code a little sloppier by combining multiple "lines" on one line.
Hope this helps someone else!
It sounds like you are clicking on a module or a .vb file in the solution space. Try right clicking, then "edit code" if at all possible. If there is an Interop object, perhaps a control (.OCX) that not's registered ion Windows, then VS will crash -- promise!