vb.net program that generates code and copy it to clipboard - vb.net

I'm planning to create a vb.net program that generates mysql codes.
Is is possible that after the program has generated the code, it will copy it in to the clipboard immediately?

Clipboard.SetText("some test")
If this program is command line, you'll need to add a reference to System.Windows.Forms.

yes, you can just use the Clipboard.SetText(text As String) method to do this.

I have just used the same code;
Clipboard.SetText(Password)

Related

Can anyone help me about command line parameters in a WinForms application?

I have a problem understanding command line arguments in VB.NET. Need help from anyone who understands it.
This is for my app. What I wanted is when app is started with switch -U or any other defined parameter, open another form with progressbar.
I have created and defined new form with update functionality, but one thing missing - reaction on command line arguments.
This is for my new app running on Windows in separate folder (not in Program Files), where I want to use auto update function.
https://learn.microsoft.com/en-us/dotnet/api/system.environment.getcommandlineargs?view=netframework-4.8
The example is in C#, but the principle is the same... use GetCommandlineArgs which returns a string array with the command line args that your app was started with.
It's actually quite simple to do.
When your application loads, get a list of the command line variables, then iterate through them and look for the one you want, then act accordingly:
Public Sub Main()
Dim arguments As String() = Environment.GetCommandLineArgs()
For Each a In arguments 'loop through the args in case there are multiple
Select Case a.ToUpper 'compare in uppercase if you don't care how the user enters it.
Case "-U"
'the -U argument was found, set a flag, or perform an action, or otherwise act accordingly.
End Select
Next
End Sub
I always put it in a select case, because in my apps, I may have multiple arguments and I loop through them all and set properties accordingly. In a select case, it's easy to add other parameters later. You can easily add a case else in the event you want to throw up an 'invalid argument' message.

"Sub or Function not defined" when trying to run a VBA script in Outlook

As a first step in creating a VBA script to resize a currently selected image to 100% x 100%, I'm trying to reproduce the example in http://msdn.microsoft.com/en-us/library/ee814736(v=office.14).aspx. The macro is very simple:
Sub Test()
MsgBox ("Hello world")
End Sub
The VBA script was simply created in "Project1" which opens by default when one presses Alt+F11. However, I keep getting the error "Sub or Function not defined" when trying to run the VBA script (Figures 1 and 2).
How can I make the VBA script 'accessible' to Outlook?
Figure 1 Running the "Test" macro in Microsoft Outlook
Figure 2 "Sub or Function not defined" error, with module tree in the background
I solved the problem by following the instructions on msdn.microsoft.com more closely. There, it is stated that one must create the new macro by selecting Developer -> Macros, typing a new macro name, and clicking "Create". Creating the macro in this way, I was able to run it (see message box below).
I had a similar situation with this issue. In this case it would have looked like this
Sub Test()
MsqBox ("Hello world")
End Sub
The problem was, that I had a lot more code there and couldn't recognize, that there was a misspelling in "MsqBox" (q instead of g) and therefore I had an error, it was really misleading, but since you can get on this error like that, maybe someone else will notice that it was cause by a misspelling like this...
This error “Sub or Function not defined”, will come every time when there is some compile error in script, so please check syntax again of your script.
I guess that is why when you used msqbox instead of msgbox it throws the error.
This probably does not answer your question, but I had the same question and it answered mine.
I changed Private Function to Public Function and it worked.
I need to add that, if the Module name and the sub name is the same you have such issue.
Consider change the Module name to mod_Test instead of "Test" which is the same as the sub.
I think you need to update your libraries so that your VBA code works, your using ms outlook

String Addition / Subtraction Function

This was a possibility in VB Script by using a script control with the eval function. For example
ScriptControl1.Eval("(10+1.5)") 'Returns 11.5
Is there a way to do this in Vb.Net? The alternative would be to simply split up the string and verify if it is an addition or a subtracting and work from there. I was just wondering if there's already a built in function that I'm not yet aware of.
Thank you
I figured it out. I had to add a COM reference to Microsoft Script control like so:
Then I just declare my new variable as a Script Control.
Dim ScriptControl1 As New MSScriptControl.ScriptControl()
ScriptControl1.Eval("(10+1.5)") ' Returns 11.5
Note*
Make sure your Target CPU is at x86! I would receive a COMException if i had it set to Any CPU.

VB.NET: Run a method only once in the Application's life time

I'm making a Library type app which needs to scan the whole computer when it is run for the first time. Not again ever. How can I accomplish it?
I'll be using SQL database to store data. So, I can easily make a table there and store a flag and check it on first run, but is there any other way? Any native support for this in VB.NET?
Use either
Deployment.Application.ApplicationDeployment.CurrentDeployment.IsFirstRun
or
My.Application.Deployment.IsFirstRun
Edit:
Check this article for additional info.
I found the best way to be to use appsettings..
Using your SQL database seems like a viable solution. You can also write a flag to a settings file on the file system if you don't want to use a table to hold it. This is a pretty good site with some examples on how to write to a file.
http://www.freevbcode.com/ShowCode.asp?ID=4492
EDIT: If you're looking for a way to determine, using some native feature of VB.NET, whether a method has ever been run before (as in, last year), I think you're out of luck. That said, a quick-and-dirty approach might be to define a method to query the database for the flag you've referred to and store the result of that method in a static flag.
Public Sub MethodToRunOnlyOnce()
' this flag will maintain its value between method calls '
' in the same session '
Static methodAlreadyRun As Boolean = MethodHasBeenRun()
If methodAlreadyRun Then
Exit Sub
End If
Try
' ... code ... '
Finally
MethodToSetDatabaseFlag()
methodAlreadyRun = True
End Try
End Sub
Private Sub MethodToSetDatabaseFlag()
' code here to set Db flag '
End Sub
Private Function MethodHasBeenRun() As Boolean
' code here to check Db flag '
End Function
You could always store this flag in an xml file or in the registry so it's on the PC. If you store it in a database and there are multiple copies of this program running on different PCs, you would have to identify them in the DB somehow, whereas if you keep track of it locally you don't need to worry about it.

Resharper 4.5 Extract method - Can't get Function!

I am using Resharper 4.5 in Visual Studio 2008. Whenever I try to extract a block of code into a method, it tries to create a subroutine and not a function. The return type option is disabled. Does anyone have any advice as to how I can get it to create a function and not a subroutine?
thanks!
It's likely that the code you've highlighted doesn't have anything to return. If the code you've highlighted doesn't set variables that are used further down your code then there's nothing for your refactored code to return.
For example, if I highlight this code and Extract Method...
Program p = new Program();
p.DoStuff();
... there's nothing to return (I don't reference p beyond this code). If I highlight the first 2 lines from this code...
Program p = new Program();
p.DoStuff();
p.DoMoreStuff();
... then Resharper will create a method returning an instance of Program (i.e. "p").