Windows forms scalling - vb.net

I'm trying to avoid wrong form showing when "Scale and layout" in windows screen settings is different than 100%.
I've folloved this LINK, and it works but only in 2 cases
1 - in debugging mode in VS
2 - if i put my *.exe file together with *.manifest and *.config files in same location.
If *.exe is alone then it does not work.
I'm looking for a clue what to do next.
Update
This is how application is run.
Private Sub Monitor_Click(sender As Object, e As EventArgs)
Dim trd As New Thread(AddressOf ShowMonitor)
trd.SetApartmentState(Threading.ApartmentState.STA)
trd.Start()
End Sub
Private Sub ShowMonitor()
Dim frMonitor As System.Reflection.Assembly = System.Reflection.Assembly.Load(File.ReadAllBytes("path to folder with DLL files\Monitor.dll"))
Dim oType As System.Type
Dim pControl As System.Object
oType = frMonitor.GetType("Monitor.frmMonitor")
pControl = Activator.CreateInstance(oType)
Application.Run(pControl)
End Sub
So my "monitor" application works as expected only in 2 cases written above.

Related

Get fileName into Textbox before Loading Form1

I have a problem with Drag and Drop file Code, I try many methods but I failed this is my Code.
Module Module1
Sub Main(ByVal args() As String)
Dim pathstring As String
If args.Length > 0 Then
Dim path = args(0)
pathstring = path
End If
End Sub
End Module
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = pathstring
End Sub
End Class
Above Code working fine with Console Application, but not in WindowsApplication
I want to get filename into Textbox1 Control before loading Form.
You need to learn how WinForms apps work. That Main method isn't even being executed because it's not the entry point for the app. Unless you disable the Application Framework, you need to handle the app's Startup event if you want to do something before creating the startup form. That said, you can get the commandline arguments anywhere, any time by calling Environment.GetCommandLineArgs.

location of recent files list vb.net

I am looking to add a recent files list to an application I am writing.
I was thinking of adding the recent files to an xml file.
Where should this file be stored?
And how should it be called from the code?
I would imagine the xml would be stored in the same folder that the application is installed in, but not everybody will install the application in the same directory.
Is there a way to code it in such a manner that it will always be stored in the same folder as the application will be installed in?
much thanks in advance!
Here is an example using My.Settings. It requires you to open the Settings page of the project properties and add a setting of type StringCollection named RecentFiles as well as a ToolStripMenuItem with the text "Recent".
Imports System.Collections.Specialized
Public Class Form1
Private Const MAX_RECENT_FILES As Integer = 10
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
LoadRecentFiles()
End Sub
Private Sub LoadRecentFiles()
Dim recentFiles = My.Settings.RecentFiles
'A StringCollection setting will be Nothing by default, unless you edit it in the Settings designer.
If recentFiles Is Nothing Then
My.Settings.RecentFiles = New StringCollection()
recentFiles = My.Settings.RecentFiles
End If
'Get rid of any existing menu items.
RecentToolStripMenuItem.DropDownItems.Clear()
'Add a menu item for each recent file.
If recentFiles.Count > 0 Then
RecentToolStripMenuItem.DropDownItems.AddRange(recentFiles.Cast(Of String)().
Select(Function(filePath) New ToolStripMenuItem(filePath,
Nothing,
AddressOf RecentFileMenuItems_Click)).
ToArray())
End If
End Sub
Private Sub UpdateRecentFiles(filePath As String)
Dim recentFiles = My.Settings.RecentFiles
'If the specified file is already in the list, remove it from its old position.
If recentFiles.Contains(filePath) Then
recentFiles.Remove(filePath)
End If
'Add the new file at the top of the list.
recentFiles.Insert(0, filePath)
'Trim the list if it is too long.
While recentFiles.Count > MAX_RECENT_FILES
recentFiles.RemoveAt(MAX_RECENT_FILES)
End While
LoadRecentFiles()
End Sub
Private Sub RecentFileMenuItems_Click(sender As Object, e As EventArgs)
Dim menuItem = DirectCast(sender, ToolStripMenuItem)
Dim filePath = menuItem.Text
'Open the file using filePath here.
End Sub
End Class
Note that the Load event handler includes a bit of code to allow for the fact that a setting of type StringCollection will be Nothing until you assign something to it. If you want to avoid having to do that in code, do the following.
After adding the setting, click the Value field and click the button with the ellipsis (...) to edit.
Add any text to the editor and click OK. Notice that some XML has been added that includes the item(s) you added.
Click the edit button (...) again and delete the added item(s). Notice that the XML remains but your item(s) is gone.
That XML code will cause a StringCollection object to be created when the settings are first loaded, so there's no need for you to create one in code.
EDIT:
I tested that by adding the following code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Using dialogue As New OpenFileDialog
If dialogue.ShowDialog() = DialogResult.OK Then
UpdateRecentFiles(dialogue.FileName)
End If
End Using
End Sub
I was able to add ten files to the list via that Button and then they started dropping off the end of the list as I added more. If I re-added one that was already in the list, it moved to the top. If I closed the app and ran it again, the list persisted.

Declaration of variables fails in vb.net VS 2015

VB in VS 2015. This code fails. Neither the boolean nor the integer have a value when the code halts. The error message is "BC30451: is not declared. It may be inaccessible because...". Perhaps this is Microsoft's way of telling me I'm an idiot for not coding in C#? Anyone know why this is failing?
Shared Sub test()
Dim N As Boolean = False
Dim i As Integer = 5
Stop ' Neither N nor I are accessible
End Sub
[Edit - add code from comment]
Private Sub cmdSaveAll_Click(sender As Object, e As EventArgs) Handles cmdSave1.Click
Dim N As Boolean = False
Dim i As Integer = 5
Dim ss As String = "xxxx"
Stop ' Neither N nor I nor ss are accessible
End Sub
If you never use a variable, Visual Studio is smart enough not to add it to the stack. The same goes for Subroutines that are never called in a program - they are not added to the executable.
Try this:
Shared Sub test()
Dim N As Boolean = False
Dim i As Integer = 5
If N Then
Console.WriteLine("This line will never be hit.")
Else
Console.WriteLine(i)
End If
End Sub
Alternately, you could declare your variables outside of the routine, so that they have scope elsewhere.
Private N As Boolean
Private i As Integer
Private Sub cmdSaveAll_Click(sender As Object, e As EventArgs) Handles cmdSave1.Click
N = False
i = 5
End Sub
Shared Sub test()
If N Then
Console.WriteLine("This line will be hit if N is changed in another method.")
Else
Console.WriteLine(i)
End If
End Sub
I don't have a solution, but the problem must be in the application under development or a bug in 2015. I created a new project and this code works just fine.
New Info: I was in release mode. Switching to debug fixed the problem. I have been, however, developing in release mode for quite some time, so I don't know if this is a bug or meant to be and I never encountered the problem before. Hmmm, most strange.

Form not popping up upon running

Here I have my vb6 code that plays around with an excel file. (I took this code off some other website.) This code compiles and builds but when I hit f5 it does the form does not po up. If I remove all the code in the Class then it pops up.
How would I fix this?
Sorry I am new at vb6, but not coding in general.
Imports Excel = Microsoft.Office.Interop.Excel
Public Class Form1
Dim xl As New Excel.Application
Dim xlsheet As Excel.Worksheet
Dim xlwbook As Excel.Workbook
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'the benifit of placing numbers in (row, col) is that you
'can loop through different directions if required. I could
'have used column names like "A1" 'etc.
'Text1.Text = xlsheet.Cells(2, 1) ' row 2 col 1
'Text2.Text = xlsheet.Cells(2, 2) ' row 2 col 2
'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
xl.ActiveWorkbook.Close(False, "f:\a.xls")
xl.Quit()
End Sub
Private Sub Form_Load()
xlwbook = xl.Workbooks.Open("f:\a.xls")
xlsheet = xlwbook.Sheets.Item(1)
End Sub
Private Sub Form_Unload(Cancel As Integer)
xlwbook = Nothing
xl = Nothing
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
xlsheet.Cells(2, 1) = "adsfasdfasdf"
xlsheet.Cells(2, 2) = "qwerqwer"
xlwbook.Save()
'don't forget to do this or you'll not be able to open
'book1.xls again, untill you restart you pc.
xl.ActiveWorkbook.Close(False, "f:\a.xls")
xl.Quit()
End Sub
End Class
Then it means that you are not adding reference to your project
follow the link to code and further details..
http://vb.net-informations.com/excel-2007/vb.net_excel_2007_open_file.htm
check file name, path and extension carefully.
("f:\a.xls")
extension may be xlsx.
Also check the name of sheet 1.

Excel VSTO Workbooks.Open only working when another action is taken first

I am working on a VSTO add-in. I have a customized ribbon, and on that ribbon a button called TemplateCallButton. I also have several other functions and buttons, one of which just opens a folder with templates (included as example). The TemplateCallButton only works and adds in a template file if one of the other actions has been completed (seemingly doesn't matter which one). After any other action has run then it works as expected.
What's more frustrating is that this behavior only seems to happen on machines I deploy on, and not the one I'm developing on. Here is the TemplateCallButton code:
Public Class InsightLabProcessor
Dim MainTemplatePath As String = "C:\Insight\Insight.xltm"
....
Private Sub TemplateCallButton_Click(sender As Object, e As RibbonControlEventArgs) Handles TemplateCallButton.Click
Dim objApp As Excel.Application
objApp = Marshal.GetActiveObject("Excel.Application")
objApp.Visible = True
Dim objWorkbook As Excel.Workbook = objApp.Workbooks.Open(MainTemplatePath)
objWorkbook.Worksheets(4).Activate()
End Sub
and here is the code for the button that just opens a folder:
Private Sub PhaseCodeFolderOpenButton_Click(sender As Object, e As RibbonControlEventArgs) Handles PhaseCodeFolderOpenButton.Click
Process.Start("explorer.exe", "C:\Insight\Phase Codes")
End Sub
or one that opens the control form:
Private Sub ControlPannel_Click(sender As Object, e As RibbonControlEventArgs) Handles ControlPannel.Click
Dim controlpanel As New ControlPanel
controlpanel.Show()
controlpanel = Nothing
End Sub
I feel like I must be missing something simple.
Thanks.
So the problem is in fact the one addressed here: http://support.microsoft.com/kb/238610, which seems pretty vicious to deal with as an add-in. The best solution I've found (again not very elegant) is to just open the command line, write out that we're waiting for the first instance to load, the close it before anyone get's too curious. I tried this on 4 machines and empirically found the longest wait time I needed was 250 ms, so I doubled it to 500 in this:
...
Shell("C:\Windows\System32\cmd.exe", AppWinStyle.MaximizedFocus)
System.Threading.Thread.Sleep(10) 'give cmd just a bit to take the line
SendKeys.Send("Waiting for Excel to register in Running Object Table")
System.Threading.Thread.Sleep(490)
Dim Term() As Process = Process.GetProcessesByName("cmd")
For Each P As Process In Term
P.Kill() 'user probably doesn't have any other instances of cmd open, if they do they are colaterial damage, may handle that if it becomes an issue
Next
Dim objApp As Excel.Application
objApp = Marshal.GetActiveObject("Excel.Application")
objApp.Visible = True
Dim objWorkbook As Excel.Workbook = objApp.Workbooks.Open(MainTemplatePath)
objWorkbook.Worksheets(4).Activate()
Again I would come back and except anything that was more elegant or acceptable to an end user. I would really love to know if there was a way to force Excel to register to the ROT. Perhaps I should turn that into another question.