Visual Basic Web Browser change form when certain webpage content detected - vb.net

I am trying to make a form on VB.NET that when a certain webpage is loaded into the web browser (in this case its a .txt file with a string of numbers). It will look for those numbers and if it finds them it will close that form and open another. I have tried to do it many times and my latest attempts came out with this code. Any help would be appreciated.
Dim passcontents As String
passcontents = WebBrowser.DocumentText = "test.com/test.txt"
If WebBrowser.DocumentText = "test.com/test.txt" And passcontents Then
ActualGen.Show()
Me.Close()
Else
End If

There are a couple of basic problems with the code you've posted.
1) Are you trying to test passcontents as a boolean or a string? You've defined it as a String, but then you are trying to assign the equality of two other strings to it, as you would a Boolean.
Try setting Option Strict On at the top of your code - it gives a few more helpful pointers to check in this case.
2) The If... Then condition is checking the same data as passcontents (may) already be providing. Are you just trying to check that the correct page is loaded, or do you want to check for certain page contents as well?
You appear to be getting mixed up between the browser's document URL and the loaded document contents.

Related

How to change the image displayed on a button at runtime in Visual Basic?

I am trying to change the Image attribute associated with a button when the button is clicked.
I have added the image I want to display (called "Black Pawn.png") as a resource within the solution and (from searching around and looking at similar questions) am trying to set the image attribute to it as shown below:
Private Sub boardButtonA2_Click(sender As Object, e As EventArgs) Handles boardButtonA2.Click
Dim sprites As Object = My.Resources.ResourceManager
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
End Sub
But when I click the button, the default image on it just disappears and is replaced with nothing instead of Black Pawn.png.
I am pretty new to using Visual Basic and Visual Studio so I may have failed to have added the image as a resource properly but I can see the image in the Solution Explorer above the form the button is in.
Any help or advice would be a great help.
Thanks.
EDIT:
Having thought more about the potential scenario, I'm wondering whether this answer is actually relevant. Your example code uses a hard-coded String but I'm wondering whether the actual String really would be a user selection. I'll leave it here anyway.
ORIGINAL:
There's no reason to use a hard-coded String to get a resource. If the String was from user entry then maybe, depending on the circumstances. As it is though, you should be using the dedicated property for that resource. That means using this:
boardButtonA2.Image = My.Resources.Black_Pawn
Just be aware that a new object is created every time you get a resource from My.Resources. For that reason, don't keep getting the same property over and over. If you need to use a resource multiple times, get it once and assign it to a field, then use that field multiple times, e.g.
Private blackPawnImage As Image
Private Function GetBlackPawnImage() As Image
If blackPawnImage Is Nothing Then
blackPawnImage = My.Resources.Black_Pawn
End If
Return blackPawnImage
End Function
and then:
boardButtonA2.Image = GetBlackPawnImage()
Also, I suggest that you change the name of the property to BlackPawn rather than Black_Pawn. You can change it to whatever you want on the Resources page of the project properties.
EDIT:
If this application is a chess game then you definitely will need every resource image for the playing pieces so you probably ought to get all of them at load and assign them to variables, then use them from those variables over the course of the game.
(I am assuming the question is for WinForms, if it isn't I will remove this answer)
When adding images as resources to a project, you have to pay attention to the name given to the resource after it is imported - the name can be changed if you want.
The image below is from one of my projects:
The name of the files on disk are:
CSV - Excel.png
GreenDot.png
RedDot.png
To fix your problem change the line:
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
to be:
boardButtonA2.Image = sprites.GetObject("Black_Pawn")
I don't think adding the image by going into Project > Add Existing Item properly added the image as a resource.
I instead went into *Project > (Solution Name) Properties > Resources > Images > Add Existing Item * and added it that way and was then able to get it working using jmcilhinney's method (I think my original method/a variation of it would work too but theirs is better).

Search text file and put matching results in listbox

I have a VB.NET project in which there is a form where there is a TextBox control, a ListBox control and an external text file that contains a list of outlook folder paths for client emails.
Essentially, the user enters into the text box the name of a client and/or their unique reference number, presses the search button (yes - I know I could make the results appear as they type, I want a button!) and it comes up with the matching results for the company name or serial number that are in the text file and puts them in the list box, with the full path of the outlook email folder.
For example:
If I put into the textbox: "06967759-274D-40B2-A3EB-D7F9E73727D7"
It would put the following result into the listbox:
"EIS Admin\Contacts{06967759-274D-40B2-A3EB-D7F9E73727D7}"
And the user can then go to that folder and find the email(s).
I have gone through several revisions both of my own code and code pasted from online with people having the same issue, only to have Visual Studio throw no errors, run the code and have no luck, with it doing nothing but clearing the list box, and not showing matching results of any kind.
I understand this may be a repeat question but I am extremely confused, can't get anything to work and need some help regarding my issue.
Here is the current code (from online - not mine):
lbx_OFL_Results.Items.Clear()
Dim i As Integer
For i = 0 To lbx_OFL_Results.Items.Count - 1
If i > lbx_OFL_Results.Items.Count - 1 Then Exit For
If Not lbx_OFL_Results.Items(i).Contains(tbx_FindText.Text) Then
lbx_OFL_Results.Items.Remove(lbx_OFL_Results.Items(i))
i -= 1
End If
Next
The list box is called "lbx_OFL_Results"
The textbox is called "tbx_FindText"
I start by clearing the list box of all items (when the form loads, it fills the list box will all lines of the text file, so I need to clear it).
Form Load Event Code:
Dim lines1() As String = IO.File.ReadAllLines("C:\ProgramData\WPSECHELPER\.data\Outlook Folder Wizard\outlookfolders.txt")
lbx_OFL_Results.Items.AddRange(lines1)
For the rest of the code it seems to be doing some form of a 'sort search' then removing any excess results.
If anyone can suggest edits to my code, or new code then that would be sublime.
Thanks.
Thanks to #Jimi for the answer.
Code:
listbox.Items.Clear()
listbox.BeginUpdate()
For i as Integer = 0 To lines1().Length - 1
If lines1(i).Contains(searchbox.Text) Then
listbox.Items.Add(lines1(i))
End If
Next
listbox.EndUpdate()
I have another question which solves how to make this search non case-sensitive. It can be found here.

How can I redirect to the same page multiple times with a different query string?

I have a page that contains a table with a list of data and an icon (on each row) which redirects the user to a new aspx page with a queryString and does some custom logic to then download the file. The user has asked that we make a "download all" button so they don't have to go through and manually click every icon in every row.
My code is close, I feel, but it will hang after the first file is downloaded and will never progress further.
Here is the code I have so far
Protected Sub ibDownloadAll_Click(sender As Object, e As ImageClickEventArgs)
'Get Parameters
'Run stored procedure to get the query string we're going to use
'Fill DataSet
For Each myItem As DataRow In ds.Tables(0).Rows
Response.Redirect("redirectPage?ID=" &myItem.Item("ID")) 'Gets stuck after here
Next
End Sub
I realized if I added the second parameter (Indicates whether execution of the
current page should terminate)
Response.Redirect("redirectPage?ID=" &myItem.Item("ID"), False)
Then it would make it to the end of the function, running through the loop as expected, but then only outputting/downloading the last file.
Is there something I'm missing or an alternative that can be used to effectively redirect multiple times? Unfortunately with the framework I'm using I'm not able to use Response.Write and put custom scripts in that way, nor can I really change the page that we are redirecting to.
You can't redirect to multiple pages (a redirect close the request) and you also can't send multiple files. Your only option would be to have a request that zip all the files together. You can do this with System.IO.Compression.ZipArchive. You don't need to save the zip on disk, you could just send the memory stream.

My.Settings "Destination array was not long enough. Check destIndex and length, and the array's lower bounds."

I have two forms with combo boxes. The combo box values are stored in My.Settings.testDevices. (System.Collections.Specialized.String.Collection) with a scope of User.
The second form adds the ability to add items to testDevices, and then upon exit it updates My.Settings.testDevices.
Now, only if I make a change to the settings (adding items only), when I exit back to the main form (which remains loaded throughout the process), my application crashes with the following message:
"Additional information: Destination array was not long enough. Check destIndex and length, and the array's lower bounds."
As I understand it, this might be a concurrency issue, however I'm not sure.
My code:
In my main form Load event: (to load from My.Settings)
testDevicesComboBoxMain.Items.Clear()
My.Settings.testDevices.CopyTo(mainFormTestDevices, 0)
testDevicesComboBoxMain.Items.AddRange(mainFormTestDevices)
Where "testDevicesComboBoxMain" is the combo box on the main form.
On the secondary form Close Event: (to save to My.Settings)
Dim items(testDevicesComboBox.Items.Count - 1) As String
testDevicesComboBox.Items.CopyTo(items, 0)
My.Settings.testDevices.Clear()
My.Settings.testDevices.AddRange(items)
My.Settings.Save()
I have found similar questions on here, but none with answers that I understand :P
As I am a beginner with vb.net, could any answers be provided in an easy to understand form please!
Thanks.
I forgot to add:
Public items(My.Settings.testDevices.Count - 1) As String
Public mainFormTestDevices(My.Settings.testDevices.Count - 1) As String
I tried setting separate declarations just in case there was some kind of conflict. These obviously do the same thing, just with different names.
I fixed it by adding a For loop to read from My.Settings.
For Each i As String In My.Settings.testDevices
testDevicesComboBoxMain.Items.Add(i)
Next
This seems to have cured the problem, and may perhaps be a more "modern" way of doing it?

Pass parameter value to Revit family using .net api

I want to pass the parameter values to Revit family.I have spend many hours on google. In result i got few links which tells Read and Write Parameter Values with VB.NET
Read and Write Parameter Values with VB.NET
in this example we are fetching parameters and writing a value in text file called ParametersValue.txt. But i am confused, how should i pass this file to Revit?
I'm hoping someone can steer me in the right direction. I would really appreciate it!
One of the first things I would do after downloading the SDK mentioned in the previous post, is install the included revit lookup addin. This has been incredibly valuable in figuring what elements are called in the API, as well as determining which storage type the parameter is using. If all of the parameters you want to update are strings, those will be fairly straightforward to set from your text file. However, if,for example, the parameter value that you think is a string is actually set by an elementid, then there will be some coding involved to get the proper info to set the parameter value.
You can easily pass parameters to component families using "FamilyManager" class. The FamilyManager class provides access to family types and parameters. Just get the parameter and set its value. As we are working in family editor for component families, we have to load the parameter values in the project. I have tried this on Revit 2019. You have to
Open family in family editor
Activate the plugin
Click on Load into Project button on addins ribbon.
Then check the parameter value in the properties of that family.
Public Sub SetParamtersForComponentFamilies(ByVal doc As Document, ByVal parameterValue As String)
Dim f As Family = doc.OwnerFamily
Using trans As Transaction = New Transaction(doc, "Creating transaction for parameters")
trans.Start()
Dim familyMgr As FamilyManager = doc.FamilyManager
Dim n As Integer = familyMgr.Parameters.Size
Dim comment As FamilyParameter = familyMgr.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_COMMENTS)
familyMgr.Set(comment, parameterValue)
TaskDialog.Show("Paramters", "TypeComments : Updated")
trans.Commit()
End Using
End Sub
I use C# when writing Revit API code because that's what all the samples are written in, but I might be able to get you pointed in the right direction with some additional details. Are you looking to assign a value to a specific parameter? For example: Height=30"?
If so you first have to "get" the parameter. In the example in spidernet he goes through every parameter of a selected element:
Dim element As Autodesk.Revit.DB.Element = SelElement(cmdData.Application.ActiveUIDocument.Selection).Element 'Prompts you to select an element
For Each p As Parameter In element.Parameters 'Goes through every parameter in "element" and assigns the parameter to "p"
If p.Definition.Name = "Height" Then 'Check if "p" is the name you want, "Height"
p.Set(2.5) 'Because Revit knows FEET, so in order to type in 30in you use 2.5
End If
Next 'Loop through parameters
If you're looking for it to do something else, please post again.
Also, not sure you're aware, but a blog that is FULL is great Revit API info is Jeremy Tammik's: http://thebuildingcoder.typepad.com. A lot of his examples are C#, which is why I started learning C# instead of VB.NET.
If you don't have it already, make sure you get the SDK for Revit 2014 here: http://images.autodesk.com/adsk/files/Revit2014SDK_RTM0.exe
It has a TON of samples that might help too. Good Luck!