I have VisualBasic project wih many forms,and one of it is form1.Whenever the initializecimponent method is called,VS stops responding and i have to end it using taskmgr.The bug is inside the form1.designer.vb, because i am able to view and edit other forms and usercontrols in designer.Anoter strange thing is that the stand alone exe runs successfully outside the IDE.I also tried adding the form to another project and that project too crashes.Whenever i build/debug/view form1 in designer, i get two dialogs one after the other:VS2010 has stopped working, VS2010 is restarting( but it never does!)
Create a copy of the form, and begin removing parts of the form until it works. The last thing you removed is what failed. You'll have to do this manually by removing three parts of each control:
The Declaration (at the bottom)
The instantiation (at the top)
The configuration (in the middle). Below is an example of the configuration
'
'LeftLabel
'
Me.LeftLabel.Anchor = System.Windows.Forms.AnchorStyles.Left
Me.LeftLabel.AutoSize = True
Me.LeftLabel.Location = New System.Drawing.Point(3, 3)
Me.LeftLabel.Name = "LeftLabel"
Me.LeftLabel.Size = New System.Drawing.Size(39, 13)
Me.LeftLabel.TabIndex = 1
Me.LeftLabel.Text = "Label2"
You'll also need to remove any references to that control, such as it being added to a container.
Do this one control at a time, then build the project and open the form. If the form does NOT open, restart visual studio and do the next control. if it DOES open, then the last thing you removed was the culprit.
For programmers facing the same problem, here is the solution:
Make sure that your user controls doesn't creates instances of themselves inside themselves.even though your program might run without any error, VS designer will crash.here is an example for such kind of code:
Public class controlx
public sub new()
dim x as new controlx
End sub
End class
you might think its a silly logical error, but such errors can be very much frustrating and time consuming.always breakdown your code to smaller units so that its manageable as Brian said.
Related
I have a MS Access database that displays an error message about a SQL syntax error on launch. "Syntax Error in query. Incomplete query clause." It also shows another error a few seconds after I hit "OK" on the first one.
Here's the two errors: https://imgur.com/a/PesjIFk
But it doesn't tell me where the syntax error is. There are SQL statements in a bunch of different places all over this project. This is a really large project and it wouldn't be practical to just look through all the code hoping that I notice an error someplace. How can I find out where this error is?
EDIT: Ok, so apparently you have to have a keyboard that has a "Break" key on it in order to even find where the error is. Wow. Fortunately I happen to have one. Here's the code that Access takes me to if I press break when I see the error message. This code is for a subform of another form. It highlights the first line (Private Sub Form_Current()).
Private Sub Form_Current()
If NumEnums > 0 Then
CurrentEnum = val(Nz(bit_value_edit.value)) 'Update CurrentEnum to the currently selected enum
Call UpdateEnumsLabel(Me.Parent![enums label]) 'Update label
End If
End Sub
...and here's UpdateEnumsLabel():
Public Sub UpdateEnumsLabel(ByRef label As Control)
If NumEnums > 0 Then
label.Caption = "Enums: " & CurrentEnum & "/" & NumEnums
Else
label.Caption = "Enums: 0"
End If
End Sub
The definition for CurrentEnum:
Public CurrentEnum, CurrentPort, CurrentFile, CurrentGroup As Long
I'm thinking that this error is unrelated to the code in Form_Current(), but Access is highlighting that line because the error happens when the form is opened. But the form doesn't contain anything that uses a query, so I'm confused as to what query Access has a problem with.
When the error Message pops up, Use Control+Break. It will take you to the line causes the issue.
You should also open a module and form the debug option in the VBA editor select "Compile All Modules"
And since it appears to happening on open/load, you can check both the macros and the main modules to find anything that triggers on AutoExec.
Often ctrl-break will hit the line where you errored out. However, in the case of multiple “events”, and code without error handling, then often the error occurs in the routine that called the code, not the actual code that caused the error.
What I would do launch the application (hold down the shift key to prevent any start up code, or forms running).
Now that you launched the application, but without forms or code running, then check for an autoexecc macro (if there is one, check what code it attempts to run).
If there not an autoexec macro in use, then check under file->options->current database. In this view, you can look/see/determine what the name of the start-up form is.
Once you determined the start-up form (or start up module/code) called from autoexec macro, then you can simply add a blank code module, and place in your code the SAME command that is used to start your application.
So let’s assume no autoexec macro, and you determine that the start-up form is frmMain.
So now, we can launch the application (hold down shift key to prevent any start up code from running). Now, type into a new “test” standard code module the following code:
Sub MyGo
Stop
Docmd.OpenForm "frmMain"
End sub
So you type in above code, hit ctrl-s to save the above code. Now with your cursor anyplace inside of the above code, you simply hit F5 to run.
At this point you hit/stop on the “stop” command in the debugger. At this point, you then can hit f8 to step to the next line of code. If the form in question calls other code etc., you still be able to single step, and “eventually” you hit the line which causes the error.
While the application may be large, a simple look at the start up forms (and huts then the start-up code) means that you ONLY really need to trace and look at the start up code – not the whole application.
I've written a form class that runs stand alone and closes itself upon a keystroke. I want to be able to run multiple instances of this form class at the same time and can't figure out how to do so. Most of the examples involving threading involve using 'addressof' pointing to a specific routine, but I want to run multiple instances of the entire form class asynchronously.
The form that runs a screensaver is called 'frmMain' and it operates on a monitor as defined by the public variable "MonitorNumber". Running the code below works fine on whichever monitor I define, and then the form instance closes itself when someone moves the mouse (as it should).
SSInst = New frmMain
SSInst.MonitorNumber = 0
SSInst.ShowDialog()
SSInst.Dispose()
Very simply, here is what I WANT to do though:
SSInst = New frmMain
SSInst2 = New frmMain
SSInst.MonitorNumber = 0
SSInst2.MonitorNumber = 1
SSInst.ShowDialog()
SSInst2.ShowDialog()
SSInst.Dispose()
SSInst2.Dispose()
If I was able to run this, I'm sure it would work as an instance of frmMain is self contained, but of course I can't run it because the code would stop after SSInst.ShowDialog until that form instance closes.
How can I run both instances at the same time???
Thank you in advance
Since I assume the form handles all the required stuff for closing and what not, the following should work for you:
SSInst = New frmMain
SSInst2 = New frmMain
SSInst.MonitorNumber = 0
SSInst2.MonitorNumber = 1
SSInst.Show()
SSInst2.Show()
application.DoEvents
Do While ssInst.Visible AndAlso ssInst2.Visible
Application.DoEvents
Loop
SSInst.Dispose()
SSInst2.Dispose()
This should get you started:
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf sub1)
System.Threading.ThreadPool.QueueUserWorkItem(AddressOf sub2)
I am in the process of converting my working VB 2010 forms application to Visual Studio 2013. I've encountered a nullreferenceexception where one does not occur in the old code. Basically, I start with a fresh instance of the form. The form contains a treeview and the user can navigate down through it and select a node. Based on their selection another portion of the form gets populated (a datagridview) with the associated elements. This is an explore screen.
When the user is done exploring, the application captures the node they were on and saves it. Immediately, the form is closed, disposed and set to nothing (NULL).
' Class variable definition
Private XInvenNode As New TreeNode
If fdiaXInven Is Nothing Then fdiaXInven = New diaExploreInven(True, True)
fdiaXInven.tvInven.SelectedNode = XInvenNode
fdiaXInven.ShowDialog()
XInvenNode = .tvInven.SelectedNode
results = .SelectedItems
fdiaXInven.Close()
fdiaXInven.Dispose()
fdiaXInven = Nothing
If the user returns to the explorer screen in the same session, it is instantiated again and the saved tree node is supposed to be restored so the user can continuing exploring in the same area. This is when the nullreferenceexception occurs.
I found this link and understand what a nullreferenceexception is. I've set breakpoints in the code and analyzed the contents of the variables within fdiaInven. It is not Nothing or NULL! For example, the following code worked perfectly on the first pass through but failed the second time:
fdiaInven.tvInven.SelectedNode = XInvenNode
What am I missing here? Are there some new rules in VB since the 2010 version that would cause this?
I'm trying to build a small application that connects to a couple databases to move data from one to the other, manipulating and verifying data is it moves. I had everything working, but wanted to add the majority of the work to a second thread so that the initial window is still functional.
I have most of it working fine so far, except for one pesky bit. I allow the user to choose the database from a list of databases on a MSSQL Server. Without the threading, the program can read the text of the combo box without issue. As soon as I add in threading, it can't read the text of the combo box. It doesn't have issues reading the text boxes that contain the servername/username/password, just with this one combo box. The code below is in a class that handles all my database functions. The combobox is on the main form (frmMain) that is launched when you run the application. frmMain is the default form for the application.
Without threading I was doing this.
Me._sqlDB = New sqlDatabase
Me._sqlDB.DBServer = frmMain.txtServer.Text
Me._sqlDB.DBUsername = frmMain.txtUser.Text
Me._sqlDB.DBPassword = frmMain.txtPassword.Text
Me._sqlDB.DBDatabase = frmMain.cboSQLDatabase.Text
When I added the thread, it couldn't read from the combo box (along with a host of other issues due to non-threadsafe calls). I did some research and added in some delegates and invokes. This solved the rest of the issues I was running into, but the combobox was still unreadable.
I created a function with a delegate to read the combobox, hoping that it was a thread related issue, but still no luck. The database connection setup code is in the database class I mentioned above and the GetDatabaseName function is in the main form (frmMain).
'Database Connection Setup
Me._sqlDB = New sqlDatabase
Me._sqlDB.DBServer = frmMain.txtServer.Text
Me._sqlDB.DBUsername = frmMain.txtUser.Text
Me._sqlDB.DBPassword = frmMain.txtPassword.Text
Me._sqlDB.DBDatabase = frmMain.GetDatabaseName
'GetDatabaseName Function for Connection String Setup
Delegate Function GetDatabaseNameDel() As String
Public Function GetDatabaseName() As String
Dim DBName As String = ""
If Me.cboSQLDatabase.InvokeRequired Then
DBName = Me.Invoke(New GetDatabaseNameDel(AddressOf GetDatabaseName))
Else
DBName = Me.cboSQLDatabase.Text
End If
Return DBName
End Function
I have tried several of the combobox properties, including SelectedItem, SelectedText, and Text. None of them have any value when the GetDatabaseName function is called. In fact, when run in debug mode and pausing the code execution, Visual Studio shows that the combobox has nothing in it (Count = 0, Items collection is empty).
The combobox is populated using a SqlDataReader that reads the list of database names from the server.
I also tried changing the setup to use a BackgroundWorker, but I get the same results.
Any thoughts on why the combobox is appearing as empty when running as a separate thread?
With a little more research, I found a possible solution, but it seems really janky. If I change the call to the function to use this:
Me._sqlDB.DBDatabase = CType(My.Application.OpenForms.Item("frmMain"), frmMain).GetDatabaseName
It works, but this seems like a very odd workaround for the issue. Is there a better way to refer to the control on the active form?
I am currently trying to create a simple client (Initiator) using QuickFix with a graphical user interface. I'm using Visual Studio 2012 and programming in VB.Net.
Here is my problem :
When I launch my app, I have this error : "An unhandled exception of type 'System.InvalidOperationException' occurred in WindowsApplication1.exe
Additional information: An error occurred creating the form. See Exception.InnerException for details. The error is: The form referred to itself during construction from a default instance, which led to infinite recursion. Within the Form's constructor refer to the form using 'Me.'"
I have two files in my project which are Client GUI.vb (http://pastebin.com/virgVNyS) and MyQuickFixApp.vb (http://pastebin.com/tQ1GXNSx). The second one contains the class that integrates the IApplication, with all the subs.
The error happens when it executes this line : "Dim initiator As New SocketInitiator(myApp, storeFactory, settings, logFactory)" from Client GUI.vb
but the software highlights a line from the file Application.Designer.vb which is :
Protected Overrides Sub OnCreateMainForm()
Me.MainForm = Global.WindowsApplication1.ClientGUI
End Sub
Can you help me and tell me what is wrong ?
Thank you very much !
When dealing with WinForms, the best proceeding to avoid problems is initialise everything (except simple variable assignation, for example: Dim filename As String = "initiator.cfg" is fine) after the GUI has been constructed/loaded (on the _Load method). The reason why you are getting this error is because of referring to the main Form (Me.MainForm =) before it has been actually created.
Move Dim initiator As New SocketInitiator(myApp, storeFactory, settings, logFactory) to ClientGUI_Load (the Load Event method of your main form) and the error will disappear.
NOTE: if you want to access initiator from "anywhere", you should keep the global declaration but move the assignation to the Load event, that is:
Dim initiator As SocketInitiator 'at the Class level, outside any sub/function (as previously)
And
initiator = New SocketInitiator(myApp, storeFactory, settings, logFactory) 'Inside the ClientGUI_Load method.
I had a similar problem; I'm hoping my description of it and the solution I found may provide clarity to others in the future.
I declared two constants for a grid background color. Selected rows should be yellow. I also said that nonselected rows should be the default background color of one of the grids on the form:
Private MatchColor As Color = Color.Yellow
Private NormalColor As Color = MyFormsDataGridView.BackgroundColor ' <<< this line is bad.
This was in the form's declarations section, before any code could run! I got the error and was flummoxed for hours. I even read this post but it didn't sink in.
The problem of course is that I was referring to a property of a grid on a form that had not yet been instantiated!
The solution was:
Private MatchColor As Color = Color.Yellow
Private NormalColor As Color = Color.White ' <<< this is the fix.
Then it loaded just fine!
I hope that helps.