How to restrict to select multi Select of Files in Open FileDialog? - vb.net

I have some task to select files in VB6 with the control "CommonDialog1".When the project is migrated this control is converted to openfile dialog.When some of the properties are not converted it is showing as commented.
This line was commented
'CommonDialog1.Flags = CommonDialog1.Flags Or &H80000 ' Dont ALLOWMULTISELECT
I want to know do we have any propery in VB.NET for Open File Dialog Control?

Just make a quick search, or use the help of IntelliSense if you're using Visual Studio.
Dim myOpenFile as New OpenFileDialog()
myOpenfile.Multiselect = False
Documentation

Related

Clear cells in multiple sheets from SSIS by VBA

How I can use below VBA coding in SSIS vba. I want to clear cells(data) from multiple sheets from SSIS by VBA coding
sub cod()
ThisWorkbook.Worksheets("Case management details").Range("A2:K10000").clear
ThisWorkbook.Worksheets("interface Timeliness").Range("A2:G20000").clear
ThisWorkbook.Worksheets("Life Events").Range("A2:N10000").clear
End sub
I had the same problem here. If you decided to solve this using SSIS component try with Script Task.
After you create excel steps will be:
Add Script Task on control flow.
Double click on component.
Press 'Edit Script...' button.
Right-click on the project in new visual studio windows (should be something like 'ST_' and some guid) and click on Manage NuGet Packages...
From 'Browse' tab install:
Microsoft.Office.Interop.Excel
Microsoft.CSharp
Into namespaces region add these 2 lines:
using Microsoft.Office.Interop.Excel;
using Microsoft.CSharp.RuntimeBinder;
Copy below code into ScriptMain class:
var appExcel = new Excel.Application();
string filename = #"D:\PathToExcel\ExcelFile.xlsx";
var newBook = appExcel.Workbooks.Open(filename);
var oSheet1 = newBook.Worksheets["Case management details"];
oSheet1.Range("A2", "K10000").Delete();
newBook.Save();
appExcel.Workbooks.Close();
appExcel.Quit();
Close without saving the second visual studio window (where you write script code).
Run the package
Note:
Instead of hardcoded file location, you can replace it with a package variable. In this case 2nd line will be:
string filename = Dts.Variables["User::ExcelFilePath"].Value.ToString();
I tested this package before write answer and everything work perfect!
With this logic, create multiple sheet instance and delete what you want.
If something isn't clear or non-logical write me in a comment and I will make correction.

VB.net, cannot access OpenFileDialog that has been added to project

I have decades of experience writing VB6 apps but today I tried to write a simple VB.net app using Microsoft Visual Studio 2019. I fell at the first hurdle. I'm sure I'm doing something really simple wrong so forgive the naive question.
Using the toolbox, I added an OpenFileDialog object to my main (one and only) form. Unlike VB6, this didn't appear on the form but in a sort of bar along the bottom of the window, but it was definitely there and I was able to set some properties.
I then added a button and wrote my first line of code:
With OpenFileDialog1
Wrong! I get errpr BC30451: 'OpenFileDialog1' is not declared. It may be inaccessible due to its protection level.
But I can see it sitting there in the design view, I have spelt its name correctly and the above code is
exactly copied from the example. I tried changing OpenFileDialog1's Modifiers property from Friend to Public - makes no difference.
What novice mistake have I made? How do I access an OpenFileDialog object that I have definitely added to my form?
The FolderBrowserDialog which can be dragged onto the form using the toolbox, is out of date and offers (from today's perspective) too few options. Do yourself a favor and use the Nuget Package Manager to download the modern OpenFileDialog.
Imports Microsoft.WindowsAPICodePack.Dialogs
in your Private Sub Button1_Click:
Dim Pfad As String
Using OFD1 As New CommonOpenFileDialog
OFD1.Title = "Datei auswählen"
OFD1.Filters.Add(New CommonFileDialogFilter("JPG", ".jpg"))
OFD1.Filters.Add(New CommonFileDialogFilter("JPEG", ".jpeg"))
OFD1.Filters.Add(New CommonFileDialogFilter("Bitmap", ".bmp"))
OFD1.Filters.Add(New CommonFileDialogFilter("PNG", ".png"))
OFD1.IsFolderPicker = False 'true to make it an FolderBrowserDialog ;-)
OFD1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If OFD1.ShowDialog = CommonFileDialogResult.Ok Then
Pfad = OFD1.FileName
Else
Return
End If
End Using
for Multiselect (with Bildpfade1 as a New List(of String) ):
Using OFD As New CommonOpenFileDialog
OFD.Title = "Bilder auswählen"
OFD.Filters.Add(New CommonFileDialogFilter("JPG", ".jpg;jpeg"))
OFD.Multiselect = True
OFD.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
If OFD.ShowDialog() = CommonFileDialogResult.Ok Then
For Each file As String In OFD.FileNames
Bildpfade1.Add(file)
Next
End If
End Using
I think I have found the answer but it looks, to me, like a bug in Visual Studio. I saved the project and closed Visual Studio. Restarted and reloaded the project. All the controls I had added to my form had vanished!
I rebuilt the now-empty project then started adding controls - lo and behold, I can now access all my controls from code whereas, previously, I couldn't access any of them. It looks like, prior to the initial build, none of the controls I thought I'd added had actually been added. Certainly they didn't survive save project, restart Visual Studio.
Seems to work now. Many thanks to those who contributed suggestions.

Automaticaly open xls file on program start?

To provide our users to edit excel files without ms excel, we have made a simple app with visual studio 2012 and devexpress Spreadsheet module.
It is very simple to open excel file and use it.
But now only one excel file is being used (with multiple sheets), and I would like the file being used to be opened always on startup.
If I add the path and filename to command line arguments, noting happens...
Using devexpress components is very different then vanilla code for me, I am a complete beginner here, so I have no idea how to continue - can someone, please point me in the right direction?
I have made a procedure, to open the file dialog and load the file - I don't know how to "pass" it to devexpress, so the file actually loads to workbook.
Private Sub OpenXls()
Dim ofd As OpenFileDialog = New OpenFileDialog
ofd.DefaultExt = "xls"
ofd.FileName = "FILE"
ofd.InitialDirectory = "C:\ref_files"
ofd.Title = "Select file"
End Sub
As you have pointed out - using the dialog is not the right way.
After some googling I have find out that this should be a better way:
Dim workbook As New Workbook
workbook.LoadDocument("C:\ref_files\file.xls", DocumentFormat.xls)
I do not get any error, but the file is also not shown...
Do I have to display the document manually after loading?
If you're using openfiledialogue to open file then you must use command to load the specific file at Form.Load event.
The backslash char in ofd.InitialDirectory = "C:\ref_files"escapes the letter R to carrige return.
Change this line to ofd.InitialDirectory = "C:\\ref_files" (add another backslash).

Getting selected files filepath without OpenFileDialog

I was wondering if there is anyway to get the file path of a selected file. I have registered a hotkey in reference to this.
E.g. RegisterHotKey(Me.Handle, 100, MOD_CONTROL Or MOD_SHIFT, Keys.D2)
Which will do certain actions on pressing ctrl, shift and 2. What i want to do is to get the path of a selected file WITHOUT opening OpenFileDialog
e.g. i select mydoc.doc located on my desktop, press ctrl shift and 2, and it will msgbox out the location of the file.
(meaning i click the file mydoc.doc on my desktop, press my hotkey and get the file location. Is there anyway to do this? (Just like how you would click a file in a folder to copy and paste it to another location, i want to click the file press my hotkey and msgbox out its location))
Is there anyway to do this or any direction anyone can point me in?Because i can't find any API which does this...
Thanks!
EDIT:
After reading all the updates and the several links here and there, I started to construct my own function for this, I'm just at the part to determine how many selected icons there are, but i keep getting back 0 icons is there something wrong with what I'm doing?
Public Function getDesktopFiles() As String
Dim vhandle As IntPtr = FindWindow("Progman", "Program Manager")
vhandle = FindWindowEx(vhandle, IntPtr.Zero, "SHELLDLL_DefView", vbNull)
vhandle = FindWindowEx(vhandle, IntPtr.Zero, "SysListView32", "FolderView")
Dim vItemcount As IntPtr
vItemcount = SendMessage(vhandle, LVM_GETSELECTEDCOUNT, 0, 0)
Return vItemcount
End Function
This question has a link to a Raymond Chen blog post which will give you the API calls you require, you will need to convert it to VB but it is based on COM so should be easy. The other answer to that question gives the C# version of Raymond's code...
Edited to add specifics:
1. Include project references to Microsoft Internet Controls (to get the SHDocVw namespace) and Microsoft Shell Controls and Automation (to get the Shell32 namespace)
2. Use SHDocVw.ShellWindows to get an enumeration of open browser windows.
3. Try to cast each item as a ShellBrowserWindow, and then try to cast the Document property as a Shell32.IShellFolderViewDual2 object.
4. The FocusedItem property gives the selected item.
VB.NET code:
Dim windows As New SHDocVw.ShellWindows
For Each window As Object In windows
Dim browser As SHDocVw.ShellBrowserWindow = window
Dim folder As Shell32.IShellFolderViewDual2 = browser.Document
Console.WriteLine(folder.FocusedItem.Path)
Next

How to find out Activex Controls from MS Access DB forms using vb.net

I am developing a Tool in vb.net and need find out Activex Controls from MS Access DB forms. I am able to conut number of controls in form, but unable to get the Activex Controls only from the form. Can any one have any idea how to achieve this, please suggest.
Can you access the controltype property? If so, I cannot help with vb.net, but here is some VBA that may help.
ActiveXCount = 0
For Each ctl In Screen.ActiveForm
If ctl.ControlType = 119 Then 'Custom control'
'Debug.Print ctl.Class'
ActiveXCount = ActiveXCount + 1
End If
Next
I am not sure what your are wanting is possible.
Try this: go into MS access and create new property that is the count of controls on the form. In VBA, me.Countrols.Count. Open the form using Access automation. OnFOrmLoad() write the number of controls to text file along with name of the form and then close the form.
Afterwards open the text file in VB.net. It is indirect but it would work.
How To Automate Microsoft Access From Visual Basic .NET
To automate:
Dim oAccess As Access.Application
' Start a new instance of Access for Automation:
oAccess = New Access.ApplicationClass()
' Open a database in exclusive mode:
oAccess.OpenCurrentDatabase(filepath:="c:\mydb.mdb", Exclusive:=True)
oAccess.DoCmd.OpenForm(FormName:="Employees")