issue with self defined structure/ textboxes - vb.net

here is the situation:
I have defined a structure called "cell"
in this structure I have included textbox called "display"
my code is this:
Dim new_cell As New cell
new_cell.display.Multiline = True
there are no syntax or run-time "errors" but having put a breakpoint on the second line, when the program gets to it it stops running the sub and carries on as normal, can anybody explain what is the problem?
EDIT
cell definition:
Structure cell
Dim candidates As List(Of Integer)
Dim constraints As List(Of Integer)
Dim x As Integer
Dim y As Integer
Dim display As TextBox
End Structure

As suggested, the second line of your first code snippet will throw a NullReferenceException. If that code is in the Load event handler of a form then, on 64-bit systems, the exception will just be swallowed and the only notification will be in the Output window.
If you want a TextBox then you have to create a TextBox. Where that TextBox gets created depends on what you're trying to achieve. Regardless, that type should be a class rather than a structure. You should also think about using properties rather than public fields and you also ought to think about starting appropriate names with an upper-case letter.

Related

How to make my module move an object using value set in UserForm/

I'm trying to automatize some processes I often do at work. Unfortunately I have no clue about programming so I've been struggling a lot with this.
I have an userform that has a textbox where you're supposed to type in how much you want the object to be moved on X axis. There are multiple objects and I can't wait to experiment how to move them all in proper direction, but the issue is, I can't even move one.
I created a variable, but I can't seem to be able to use it with Object.Move.
Right now VBA tells me to assign the values to an array, so I did that, but I can't put variables into an array apparently.
I've tried simply declaring the variable as public, calling it let's say for example "Value", and using Object.Move (Value), 0
or
Object.Move Value(), 0
Tried both, because I was not sure which one is correct.
After many trials I finally skipped most of the previous errors I was getting, and now I'm stuck at trying to set up an Array using a variable from the UserForm.
Here's the code inside Userform
VBA
Public Sub TextBox1_Initialize()
M = TextBox1
Unload M
End Sub
And here's the code inside the module
VBA
Public M As Integer
Sub Move()
Dim XY(M, 0) As Integer
Object.Move XY
End Sub

VBA can't take reference of an object

I've got an array I need to populate with references to labels on a display. I've managed to reduce my issue to a MWE below:
Private Sub Button1_Released()
Me.TestLabel.Caption = "Button1"
End Sub
Private Sub Button2_Released()
Dim o As MSForms.Label
Set o = Me.TestLabel
o.Caption = "Button2"
End Sub
The code for button1 works while buttton2 fails with message, "Method Caption of object ILabelControl failed (0x80010105)".
TypeName(Me.TestLabel) returns "Label". When I change my Dim to "Label" instead of "MSForms.Label" I get behavior that looks like an uninitialized variable being accessed. For example: sometimes button2 will produce chinese characters in the label, often nothing happens but the VBA module will crash.
I have tried removing and restoring the typelib reference with no effect. I have tried changing the reference from the office v16 fm20.dll to the syswow64 v14 fm20.dll with no effect.
This is a previously functioning application developed on one machine and moved to another. I have considered recreating the displays, but the MWE comes from a new display, created on the new machine, with just a label and two buttons.
Pretty stuck here.
ed: changing the dim from MSForms.Label to Object worked

Declaring WebBrowser in VB.NET Other Form

I am working on a project that has WebBrowsers in Other Forms;
I wrote the code below to control these WebBrowsers; but I need the code to recognize (Declare) the WebBrowsers of these forms.
Dim openForm As Form = Nothing
For Index As Integer = My.Application.OpenForms.Count - 1 To 0 Step -1
openForm = My.Application.OpenForms.Item(Index)
If openForm IsNot Me Then
MyWebBrowser.navigate("http://www.google.com/") ' PROBLEM IN THIS LINE
End If
Next
My Module created them as below:
Module MMMBrowser
Dim frmNew As New Form
Dim MekdamBrowser As New WebBrowser
Other info gleaned from comments:
there is form factory of some sort which creates new frmNew
there are many of these open at a time, which is the reason for the backwards loop thru OpenForms to find the last one.
The MekdamBrowser reference is an attempt to refer to the browser on the form.
The easy things is to provide a way for outsiders to tell the form to navigate somewhere using a new Sub, and let the form drive the browser control. This probably eliminates the need for a global MekdamBrowser reference. In the browser form add something like this:
Public Sub GotoNewURL(url As String)
myWebBrowserName.navigate(url)
End Sub
This procedure only exists for Form1 not the generic Form type, so we need to change how you find the form to use. Your existing loop is wonky. It will only ever find the last instance of a form which is not the current form. If you add a third form type, it wont work well:
Dim lastBrowserFrm As Form1 ' use the class name!
' this will try to get the last Instance of Form1
lastBrowserFrm = Application.OpenForms.OfType(Of Form1)().LastOrDefault
' LastOrDefaultcan return nothing if there are none,
' so test
If lastBrowserFrm IsNot Nothing Then
lastBrowserFrm .GotoNewUrl("www.stackoverflow.com")
Else
' create a new one, I guess
End If
Your loop was not considering that there could be other form types in the collection which are not Form1 or even if a new browser form was the last one created! This is more important now because GotoNewURL is only available on Form1 instances.
I changed the name to lastBrowserFrm to reflect what is really going one - it will just find the last one created. If you are trying to work with a specific instance, you need to provide a way to track the ones you create such as with a List(of Form1) or use the Name property so you can tell one from the other. As is, you do not a way to get back a specific form instance.

How can I use value of a string to control another control in VB.net?

I have been playing around with some code, and I have made easily 50+ controls that all are labeled: PictureBox[XCoordinate]_[YCorrdinate] (Replacing the brackets and contents with the coordinates of them on a little grid I made.)
The problem with this is it is a real pain to use a control when doing loops to update all the picture boxes. I want to know how to do code like:
'This code assumes that the picture boxes are all initialized.
Dim XCoordiante As Integer = 5
Dim YCorrdinate As Integer = 2
PictureBox[XCoordinate]_[YCoordiante].Image = [Put Image Here]
I am going to put this within a loop. Is there a way that I can do this without manually typing it all and risking missing something within a case statement? And also, I would have to retype it for every different kind of change I want to make (ex: tag or error image).
Would a pointer somehow help? I don't really know how to do this, but it would be really helpful if possible.
When you create them, save them to a List:
Private pList As New List(Of PictureBox)
Dim pic As New PictureBox
With Pic
.Location = ...
' etc
End With
Me.Controls.Add(pic)
pList.Add(pic)
Assuming they are created in some sort of order:
For n As integer = 0 To pList.Count = 1
' add code to look at Plist(n).X and .Y to determine what to do (?)
Plist(n).Image = ...
Next n
If there is more info to capture, create a custom class of a PicBox and the other info, and make the list a List(Of myPicClass).

Not all controls in collection are being copied

I'm a bit confused here. I'm copying all the controls from one form to a panel on the main form and for some reason only about half of them copy.
Private Sub switchComponent()
Dim selection As String = TreeView1.SelectedNode.Text
Panel1.Controls.Clear()
Dim query = From cont In serverDic(selection).Controls
Select cont
For Each copier As Control In query
Panel1.Controls.Add(copier)
Next
End Sub
serverDic is defined as:
Dim serverDic As New Dictionary(Of String, frmServer)
When stepping through the code, serverDic(selection).Controls has 12 elements, but only 6 of them get copied. Next time this gets called, only 3 get copied. Does Panel1.Controls.clear() somehow kill the references?
EDIT: Just to show that there are infact 12 elements in the collection:
The problem here is that you are iterating over a collection that you are changing. When you add a Control to an container it is implicitly removed from it's previous parent and hence query. This is why you see exactly half of the items get removed.
With most collections this would be more apparent because they would throw if modified during an enumeration. The primary source of query here though is ControlCollection which does allow for modifications while enumerating.
To fix this problem just add the following line before the For Each loop.
query = query.ToList()