How to get list of printers not printing direct? - vb.net

I need a list of printers that DO NOT print direct. Getting a list that do print direct seems reasonably easy. But how to do the opposite?
Dim PrintServer As New SysPrint.PrintServer
Dim arrFlags(0) As SysPrint.EnumeratedPrintQueueTypes
arrFlags(0) = System.Printing.EnumeratedPrintQueueTypes.DirectPrinting
Dim QColl As SysPrint.PrintQueueCollection = PrintServer.GetPrintQueues(arrFlags)
PrintServer.GetPrintQueues Method
EnumeratedPrintQueueTypes Enumeration
MSDN says that the EnumeratedPrintQueueTypes has a FlagsAttribute attribute that allows a bitwise combination of its member values. So I should be able to specify NOT direct somehow. How do I do it?
I tried to do this arrFlags(0) = Not System.Printing.EnumeratedPrintQueueTypes.DirectPrinting but that returned no results. Clearly incorrect.
So how do I manipulate the flags attribute to eliminate all printers printing direct?

This is one way to do it but it seems very inelegant:
'get full list
Dim PrintServer As New SysPrint.PrintServer
Dim QColl As SysPrint.PrintQueueCollection = PrintServer.GetPrintQueues()
'get those not printing direct
Dim Qcoll2 As List(Of SysPrint.PrintQueue) = QColl.Where(Function(x) Not (x.IsDirect)).ToList
'select name only
Dim strList As List(Of String) = Qcoll2.Select(Function(x) x.Name).ToList

Related

Get the contents of a line in a string

I am using Visual Studio.net, Visual Basic and I have a question.
If I have a string that has many lines in it, what is the best way to get the contents of a certain line?
E.g If the string is as follows:
Public Property TestProperty1 As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
What is the best way to get the contents of line 2 ("Get")?
The simplest is to use ElementAtOrdefault since you don't need to check if the collection has so many items. It would return Nothing then:
Dim lines = text.Split({Environment.NewLine}, StringSplitOptions.None)
Dim secondLine = lines.ElementAtOrDefault(1) ' returns Nothing when there are less than two lines
Note that an index is zero-based, hence i have used ElementAtOrDefault(1) to get the second line.
This is the non-linq approach:
Dim secondLine = If(lines.Length >= 2, lines(1), Nothing) ' returns Nothing when there are less than two lines
That depends on what you mean by "best".
The easiest, but least efficient, is to split the string into lines and get one of them:
Dim second As String = text.Split(Environment.NewLine)(1)
The most efficient would be to locate the line breaks in the string and get the line using Substring, but takes a bit more code:
Dim breakLen As Integer = Environment.Newline.Length;
Dim firstBreak As Integer = text.IndexOf(Environment.Newline);
Dim secondBreak As Integer = text.IndexOf(Environment.NewLine, firstBreak + breakLen)
Dim second As String = text.Substring(firstBreak + breakLen, secondBreak - firstBreak - breakLen)
To get any line, and not just the second, you need even more code to loop through the lines until you get to the right one.

How to assign a value to an array from a combobox

The code I have is:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
I have declared Dbase as array and assigned Nothing, Db_ComboBox is a combobox.
For that assignment statement, I'm getting the following error: "Reference 'Dbase' has a value of 'Nothing'"
What is the reason for this error, and how can I take the value from the combobox and save it in the array?
You need to change this:
Dim Dbase() As String = Nothing
to this (declare an array of 1 element):
Dim Dbase(0) As String
And then this line will work:
Dbase(0) = Db_ComboBox.Text
If you need to change your array size you can use Redim or Redim preserve, as required.
If you anticipate contents of Dbase to change often, I am all with #Joel's suggestion about switching to List(Of String) instead of handling array sizes manually.
Let's look at your code:
Dim Dbase() As String = Nothing
Dbase(0) = Db_ComboBox.Text
Especially the first line. That first line creates a variable that can refer to an array, but the = Nothing portion explicitly tells it, "Do not create a real array here yet". You have, effectively, a pointer that doesn't point to anything.
I get here that what you really need is a List collection that you can append to over time:
Dim Dbase As New List(Of String)()
Dbase.Add(Db_ComboBox.Text)
Dbase() IS NOTHING. Look at this example:
cargoWeights = New Double(10) {}
atmospherePressures = New Short(2, 2, 4, 10) {}
inquiriesByYearMonthDay = New Byte(20)()() {}
That's how you declare arrays.
More examples: http://msdn.microsoft.com/en-us/library/vstudio/wak0wfyt.aspx

IndexOf a ComboBox just will not work for me

VB2010. I am trying to populate a ComboBox with the contents of an Enumeration of units. I have managed to do this with a Dictionary. Something like
Dim dUnits As New Dictionary(Of String, Integer)
Dim da As String
For Each enumValue As eUnits In System.Enum.GetValues(GetType(eUnits))
da = ConvertEnumToCommonName 'gets unique name for an enumeration
dUnits.Add(da, enumValue)
Next
cbo.DisplayMember = "Key" 'display the the common name
cbo.ValueMember = "Value" 'use the enumeration as the value
cbo.DataSource = New BindingSource(dUnits, Nothing)
When I load up my form that works well. Now the user can choose to select a default unit to display. So then I try
Dim defUnits As eUnits = eUnits.Feet
Dim idx As Integer = cbo.Items.IndexOf(defUnits) 'doesnt work, returns a -1
cbo.SelectedIndex = idx
I have been doing some research for some time and am fairly sure that this has to do with the ComboBox storing Values as a string and in reality I'm searching for an enumeration which is an integer. Don't know if I have that right or not. Anyway, I just cant seem to get the default item selected. Is there another approach I can try?
First of all you have a collection of integers and you're searching for the enum value. For that, try one of the following:
Store the enum value in the dictionary instead of a string:
Dim dUnits As New Dictionary(Of String, eUnits)
Keep the integers in the Dictionary but use the integer value of the enum when searching the ComboBox:
Dim idx As Integer = cbo.Items.IndexOf(CInt(defUnits))
But this is not going to work yet. You are data-bound to a Dictionary, which means the items in cbo.Items are not of the enum type, but of the type of the elements in the Dictionary (KeyValuePair(Of String, eUnits) assuming #1 above).
The easiest solution is just to set the SelectedValue property of the combo box instead of the SelectedIndex. Assuming you used option #1 above, this would be:
cbo.SelectedValue = defUnits
If you used option #2 instead, you'll have to convert this to an integer first:
cbo.SelectedValue = CInt(defUnits)

Adding values to array

I am trying to run an event which will search through the different files in a given directory. The goal is to have it search for all files that begin with 'SP_', which are .sql files containing Stored Procedures. I would then like to add the full text of these Procedures to an array to be used later. This is causing an error when run, which I believe is because 'FullProcedureArray()', the string array I am trying to load does not have defined boundaries. When I declare it as 'FullProcedureArray(7)', or with some other value, it appears to run fine. But I don't want to have to hard-code a boundary for 'FullProcedureArray'; I would rather let it be defined by whatever the number of files in the folder is.
My question: Is there a way to declare 'FullProcedureArray' without having to give it an absolute value? I may just be missing something painfully obvious, but I haven't worked with this type of array much in the past. Thanks in advance for your help.
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim ProcedureArray As String()
Dim ProcedureText As String
Dim FullProcedureArray() As String
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
Dim fileSystemInfo As System.IO.FileSystemInfo
Dim i As Integer = 0
For Each fileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
If (fileSystemInfo.Name.Contains("SP_")) Then
ProcedureArray = System.IO.File.ReadAllLines(AppDataLocation & fileSystemInfo.Name)
ProcedureText = Join(ProcedureArray, "")
FullProcedureArray.SetValue(ProcedureText, i)
i = (i + 1)
End If
Next
An array by definition has a fixed upper bound. If you don't want a fixed upper bound, don't use an array. Use, for example, a List(Of String) instead:
Dim AppDataLocation As String = "C:\Files\TestFiles\"
Dim ProcedureList As New List(Of String)
Dim sourceDirectoryInfo As New System.IO.DirectoryInfo(AppDataLocation)
For Each fileSystemInfo As System.IO.FileSystemInfo In sourceDirectoryInfo.GetFileSystemInfos
If (fileSystemInfo.Name.Contains("SP_")) Then
Dim ProcedureText As String = _
System.IO.File.ReadAllText(AppDataLocation & fileSystemInfo.Name)
ProcedureList.Add(ProcedureText)
End If
Next
If, for some reason, you still need the result as an array afterwards, simply convert the list to an array:
Dim myArray() As String = ProcedureList.ToArray()
If you don't want to give a size to your array or want to change at runtime, you can use "Redim Preserve"
http://msdn.microsoft.com/en-us/library/w8k3cys2%28v=vs.71%29.aspx

vb.net in InDesign Scripting - Grouping TextFrames

I want to group textframes in my InDesign CS3 vb.net script. It worked for InDesign 2.0 but it does not work with InDesign CS3. Here is my code:
Dim myDoc As InDesign.Document = Nothing
Dim myGroup As InDesign.Group = Nothing
Dim myObjectList(2)
myObjectList.SetValue(myOuterTextFrame, 0)
myObjectList.SetValue(myInnerTextFrame, 1)
myObjectList.SetValue(myContentTextFrame, 2)
myGroup = myDoc.Groups.Add(myObjectList)
Getting error "Unable to cast object of type 'System.Object[]' to type 'InDesign.Objects'."
I know you asked for this a long time ago so I'm mostly answering for future searches. I haven't found a fully managed way to do this using the .Net Framework and believe me, I've searched for it. I've tried a million different casts, subclassing, reflection, you name it. What ultimately worked in the end was JavaScript. Below is a method that takes an InDesign.Document object and two or more integers that represent InDesign item IDs. It then creates some JavaScript and has InDesign execute it. Finally it returns an InDesign.Group created from those objects.
Public Function GroupObjects(ByVal indesignDocument As InDesign.Document, ByVal ParamArray objectIds() As Integer) As InDesign.Group
'Sanity checks
If indesignDocument Is Nothing Then Throw New ArgumentNullException("indesignDocument")
If objectIds Is Nothing OrElse objectIds.Count < 2 Then Throw New ArgumentException("You must pass at least 2 object ids")
'We'll assign a unique label to the group that we create in JavaScript so that we can find it in managed code later
Dim GID = Guid.NewGuid().ToString()
'Create the JavaScript
Dim Buf As New StringBuilder()
Buf.AppendLine("var items = new Array();")
For Each ID In objectIds
Buf.AppendFormat("items.push(app.activeWindow.activePage.pageItems.itemByID({0}));", ID)
Buf.AppendLine()
Next
Buf.AppendLine("var g = app.activeWindow.activePage.groups.add(items);")
Buf.AppendFormat("g.label='{0}';", GID)
Dim IA = indesignDocument.Parent
IA.DoScript(Buf.ToString(), InDesign.idScriptLanguage.idJavascript)
'Loop through all document groups looking for the object with the label created above
For Each G As InDesign.Group In indesignDocument.Groups
If Not String.IsNullOrWhiteSpace(G.Label) AndAlso G.Label = GID Then Return G
Next
Return Nothing
End Function
To use it in your code you'd say:
Dim MyGroup = GroupObjects(myOuterTextFrame, myInnerTextFrame, myContentTextFrame)
This one worked for me:
Type type = Type.GetTypeFromProgID("InDesign.Application");
Host = (InDesign.Application)Activator.CreateInstance(type);
InDesign.Objects o = Host.CreateCollection();
I found my answer in the InDesign Scripting Samples - the Neon sample script gave grouping examples