This question already has answers here:
Why is there a default instance of every form in VB.Net but not in C#?
(2 answers)
Closed 2 years ago.
In VB.NET, I can have multiple forms and show one from the another by using the following code:
Form2.Show() ' Form2 is the class name itself!! not an instance..
How it that allowed?
Your project uses a Forms object in a namespace called My. In there, Visual Basic creates default instances of the forms:
https://learn.microsoft.com/en-us/dotnet/visual-basic/developing-apps/development-with-my/default-object-instances-provided-by-my-forms-and-my-webservices
This seems to be happening somewhere behind the scenes. I have not been able to locate a file in my project where this is happening, but I see My.Forms being used in my executable.
Related
Hello thanks for reading my question, I've build a user form with controls to control equipment via RS232 and USB. I've also added a separate class module "class.vb" to hold miscellaneous functions like writing to an ini file.
When I'm in the code section of the Form.vb and I type txb for text boxes that I have in the form visual studio pops up a nice list of all things in the form that start with txb.
However, when I do the same thing in my class.vb it does not do this and even if I type the whole thing such as txbModulation and then add the dot after it does not come up with the list of properties.
Does the class.vb have access to Form1.vb controls?
When I'm in the code section of the Form.vb and I type txb for text
boxes that I have in the form visual studio pops up a nice list of all
things in the form that start with txb.
This is because when you start typing a variable name, Visual Studio starts looking for a match. It looks in block scope first (a for or while loop maybe), then at local (function/sub) scope, and finally at class level (a form is a class). It will also look for any matches in Modules if you have them. See Scope in Visual Basic for more info.
Since the Form and your INI Class are different, they cannot see each other unless you pass references or somehow pass values via parameters to subs/functions.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have inherited a VB.NET project from a departed coworker. I have zero experience with VB, .NET, or VB.NET, I am a JS developer so this is all Greek to me.
My supervisor would like me to remove the "MDI-ness" of the application. I.e. we are looking at MDImain.vb and in the previous application (pre-conversion) there is no MDImain. This project was converted from VB6 to VB.NET and we are struggling to get things working again; the supe's opinion is that eliminating the MDI-ness of the application will help straighten it out. So the question is: is there a straightforward way of making an MDI VB.NET application into a non-MDI one?
Note: I have seen other similar threads with people recommending that the application simply be rewritten from scratch. I wholeheartedly agree, and would like to do that, but our manager refuses this option as it he believes it will take too long.
Note from comments: the original application was not MDI, this can be confirmed, for example, by running the existing executable and seeing that it is not an MDI application. The VB6-to-VB.NET conversion in VS 2008 somehow made the old VB6 application into an MDI application without asking. (I have shadowed a more recent conversion attempt and seen that adding MDI functionality is not part of the process as we encountered it.) We are trying to figure out how to either prevent this MDI addition in another conversion attempt, or to undo it in the result, since we did not want it.
Your Main Mdi Form has a property called IsMdiContainer That is why it is MDI to the operating system. Change that. That form should be like this Search for MyApplication OnCreateMainForm() that is where the app "starts" look at that form to see its IsMdiContainer property.
The other detail is that where you have code that creates and spawns new forms you will have to be aware of the 3 different form display concepts that change when going from MDI to single forms.
Public Sub CodeInForm1()
Dim frm As Form
frm = New Form2()
' Code Concepts for VB Parent Child in MDI and Non MDI scenarios
frm.MdiParent = Me ' MDI Concept
frm.Owner = Me ' No MDI but still linked
frm.Show(Me) ' Anoter way to link Forms when spawning a new one (No MDI)
frm.Show() ' No Specific Parent Assigned
End Sub
The other thing to be aware of is that the Active Menu concept changes between MDI and separate forms.
If you try these changes do them on a copy of the code.
From what you have said so far, it sounds like your VB6 application had an MDI main form (possibly named Main.frm).
This is used as a 'container' to allow the display of child forms (and also somewhere for the menu).
If you have the VB6 IDE installed (Visual Studio 6) you can confirm this.
If this is the case, removing the "MDI-ness" will change the whole concept of any converted code and create more work for you.
I'm a newbie on vb.net, and I was in progress with my first application... and found some example code in the msdn so I just replaced my Form1.vb file content with the content from the MSDN. When I roll back the changes, and tried to compile my old code then hundreds of errors appeared, and when I switch to the Form1[Design] tab I see this:
The class Form1 can be designed, but is not the first class in the
file. Visual Studio requires that designers use the first class in the
file. Move the class code so that it is the first class in the file
and try loading the designer again.
I'm really new on vb.net and the visual studio itself, and I dont know what to do in this case, is my work destroyed or what?
That's because you added some class or other code above the class definition in form1.vb. Remove that.
What worked for me is editing both Form1.vb and Form1.Designer.vb and placing at the beginning of both files: Namespace Whatever and at the end of both files: End Namespace. The "Whatever" can be any name not already used in the program (or the name of an existing Namespace that you're already using).
You added another class in your form and that is the reason of the error. I had ran into same issue. I had added another class in the form and that caused this error. To resolve, I moved the new class to a module(created new module) and then access the class in the required form.
So lets say I have a form1 that opens on startup. Form1 contains a textbox named textbox1 and a button named button1. When I click button1 this is code that is called:
Class form1
Sub CapLetters () 'buttons click event
Dim myObj as new MyObject
MyObj.CapAllLetters
Textbox1.text = MyObj.GetThoseCapLetters
End sub
End class
Now, I know this is kinda dumb, but my main question is, since im creating a new object, the textbox1 will not be available in my object unless I specifically call it like:
Form1.textbox1.text
Is this good practice or is there a better way? Now in my program I have about 10 textbox and comboboxs I need my object to use. I know i could do something like this:
MyObj.CapAllLetters (textbox1.text)
But that doesn't seem like a good idea to pass that many values in a method.
I think I need a way for my object to gather all the info upon initating it?
Thoughts?
You basically seem to be asking whether it's OK to use the default instance of a form. The answer is yes. In recent versions of VB, each form that has a parameterless constructor also has a default instance, which is a single instance that can be accessed anywhere and at any time via the class name. This feature exists because it makes it easier for beginners to access forms from different places in a project without having to worry about passing references to forms around that project.
In the case of the startup form in a project, it is always the default instance of its type, assuming that you haven't disabled the application framework. That means that you can always access your startup form anywhere in your project using the default instance of its type.
Now, you'll find that experienced developers rarely use default instances. That's because they are never required and rarely add value if you know what you're doing. That means that you can always access your application's startup form is reasonable locations throughout your project without using the default instance.
This question already has an answer here:
How to I inject programmatic text into my Coded UI test (as opposed to recorded text) in Visual Studio?
(1 answer)
Closed 7 years ago.
In Coded UI, I have to confirm a particular text is present in text-box.
I am able to reach up to the dialog box and i know how to enter text. However. now i need to read text from the box.
Is there a WinApplication command that can help me to do so?
You can just read the text out in the same way that an assertion reads the text to check its value. One way of learning how to do this on any control is to use the Coded UI recorder to create an assertion method on the text shown and copy its code. Commonly the assertion will be on the DisplayText field. In fact that is a neat way of getting the relevant UI Controls created for you.
The method created for the assertion (in the UIMap.Designed.cs file) will be similar to:
public void AssertMethod1()
{
SomeUIType uITTextItem = this.UISomeWindow.UISomewhere.UITextItem;
Assert.AreEqual(this.AssertMethod1ExpectedValues.UITextItemDisplayText,
uITextItem.DisplayText,
"A message for a failed assertion");
}
Just copy the whole method into the UIMap.cs file, use the commend in the UI Map editor. Then save all the files.
Edit the method that is now in the UIMap.cs file to something like the following:
public string GetTheUITextItem()
{
SomeUIType uITTextItem = this.UISomeWindow.UISomewhere.UITextItem;
return this.AssertMethod1ExpectedValues.UITextItemDisplayText;
}
Now the contents of the test can be obtained by calling GetTheUITextItem().