Calling a C# from a VB project in one solution - c#

I have 2 projects in one solution.
The main is a VB.net project and the secondary is a C# project. I want to call a form from the C# project from the VB.net project at the click of a button but I am unsure how.
I have read it is possible if I create the C# .dll and reference it but I cannot find a guide for this. Would anybody give me a step by step on how one would accomplish this please?

It's a fairly simple and easy thing to do, and also common. Saves duplicating code across projects if you have multiple projects which all have a function which does the same thing.
Right Click the VB project and click Add -> Reference...
Press the Projects node on the left.
Now hover over the C# project and click the checkbox that appears.
You've now added the reference. Beware that you cannot then reference the Vb project from the C# as Visual Studio will not allow this because you're creating a circular reference.
To call a form to show up, you can do the following.
First, you'll need to make a reference somewhere of the new form. For example; (OtherProject being the name of the other project, and FormName being the name of the form in the project. Depending on what you're doing, you might want to do this when you start your VB app, or you might only need it once. It entirely depends on your setup.
Dim OtherProjectForm as New OtherProject.FormName
When you've done that, just go ahead and do
OtherProjectForm.Show()
or of course, again, depending what you're doing,
OtherProjectForm.ShowDialog()
You'll also be able to access and public members of the form. By default, every control on the form's access mode is "Friend", which means just objects which are part of the same assembly can access them, but if you need to, you can make them public. Or you can just make methods to interact with them.

Related

How to open a windows form app, from another windows forms app

Hi im kinda new to programing and im stuck on a simple problem.
I have two windows forms projects: WindowsFormsApp1 and WindowsFormsApp2, and i need to open the second one from a button in one,keeping in mind that they both have multiple forms, i need to pass variables from one to the other, and when i close 2, i need to open 1 again.
But i really have no idea on how to do so, and i can't seem to find anything online.
You can start other application by using Process.Start("OtherApplication.exe").
Once your second application is compiled, enter the path of the .exe and you are all set.
Here's the documentation for Process.Start(): https://learn.microsoft.com/en-us/dotnet/api/system.diagnostics.process.start?redirectedfrom=MSDN&view=netframework-4.7.2#overloads
Reference the secondary application project from your main project.
The button in your main project can then close or hide your main project's window(s) and call a method from your second project. You may want to provide a specific method for that purpose, to make it easier to pass variables.
When that method returns, reopen (or unhide) the main UI.
Note: if this needs to go both ways, this won't work - you can't have circular references. In such a case, you might have to start the applications' binaries, probably using Process.Start. The drawback is that you then need to arrange the passing of data between them via command line arguments.
Why not make them under one solution? You can do that by right clicking on your solution in the "Solution Explorer" then "Add" and "New Project" or you can try with "Existing Project" you must select the ".csproj" file in the directory of your second project. After adding it it should appear in your "Solution Explorer" with all the files like a different project. After that you can find "References" under your project in the "Solution Explorer". Right click on it and select "Add Reference" then under "Project" you will find your second project and then "Add" it. Then by typing
"using SecondProjectName;" you will reference it and then you can use classes from your second project! Hopefully this helped! But if your form class is "internal" you will not be able to use code from it, because "internal" makes it so the code can be used only in the specific project.
Another way is to use "Process.Start(#"SecondProjectExeFilePath");". The # sign means that the string has escaping characters like /,\ etc. You can read more at "docs.micosoft.com" :)

Workflow with C# in Visual Studio

I normally code with PHP, I am used to opening up my editor of choice and going away at it, coding classes,methods, etc. It is fairly easy as there is no GUI to worry about.
Last night I spent the whole night following a couple tutorials with C# in Visual Studio, it's turning out to be harder then I thought it would be. Once thing that I am not use to is, all the tutorials have you add a form object like a text box or button, then have you double clikc it to get to the code part, you then enter some code for that method. Then back to the form and re-peat
This seems very hard as you are never really working on "just the code" so 1 question is, is it always like that or just because i'm new and following tutorials?
Another question, when I see source code online to do certain functions, say I see a class I would like to try using, how can I use that class in the existing form class created by VS, do you somehow import other classes or do you add them right to the form code you are working on?
I'm sure that didn't make much sense but hopefully it does to someone, i'll try wording it better if not.
I should add that this was with WPF, also I feel like you have to learn 2 languages, the C# which has very similar syntax to PHP so that doesn't seem too difficult and the for GUI that's like a whole diff language
You can download the classes you are interested into.
Then you go to the Solution Explorer panel and you add existing items.
This will COPY the files to your project.
In order to use those classes you need to declare that you wan to use them.
So, what you have to do is to say something like
using FooNamespace;
Then you are ready to use the classes.
The name space is declared right before any class. You can go edit it.
Now about the forms. Each form is a Class and it consists of three files
ClassForm.cs
ClassForm.designer.cs
ClassForm.resx
You ONLY need the first one. Right click and view code. You can go there and use it.
Many questions, Many answers
Difficulty and Repetition
you can add form objects via the designer or you can hit the source button (CTRL-PgDn). From there you can edit elements in asp and html just like any php IDE. I do most of the work in source. I am a real programmer so I can never do the drag and drop. With intelligence and time you learn the properties and what to do.
to make complex pages you just have to know what you are doing.
What I started with VS I had the same feelings as you, but i have gotten into the flow of it.
As far as the code behind, you are just hooking methods up to the asp elements that get called by the built in code. You can add your own classes, functions, everything in the code behind or in separate files, just like c++, php, whatever.
Hope that helps, VS is really powerful and runs smooth when you learn where things are, been using it for years now and I'm still learning. Bottom line, never use drag and drop and just play with it.
unfortunately the .net world love drag-drop controls. so most tutorials are designed around this concept. drag a textbox on the to form. drag a button onto the form. double click button image to get the click handler.
it's not needed, it's just the approach for most people using visual studio. being that this is a WPF project everything can be done from code, or xaml markup. you don't need the WYSIWYG editor.
as for adding/referencing classes first you need to reference the assembly the class is located in. your core .net types (part of the BCL, base class library) are automatically included as references. then you add a using statement to the appropriate namespace. then you can instantiate the object.
There are ways to have a C# interactive window; see this question. Alternatively, you don't need to use a form, but you could also create a command-line application.
As for the second question, you can add a new class to your project and then use it in your form. There's really no additional step, except that if the namespaces are different, then it is easier if you import that namespace (via using).
Partly, yes, because you're new and using tutorials.
Partly, no, because you're working with forms, and you really don't want to hand-code those by hand.
If you just want to play with C#, and not concern yourself with forms and display, look for information on Console application. Instead of worrying about buttons and textboxes, your worst nightmare will be Console.WriteLine();
Here are some console-based C# tutorials:
C# Station tutorial
C# Yellow Book - it's a PDF. It's good.
Yes, it is exactly because you are following the video tutorials which are almost always tailored for beginners... Most of us making a living working in VS, developing WPF solutions do not even use the visual editor but instead work directly with XAML to build our UI and have very little or no code in the code behind files following the MVVM pattern.
To answer your second question, most of your classes that "do stuff" which is not directly intertwined with the UI should be in a separate class library (dll file) and should not even be referenced directly by your main UI project (in order to facilitate loose coupling) but instead accessed using some form of Dependency Injection, typically utilizing Interfaces.
The code that responds to user interaction should be in your ViewModel classess which are typically a data context for your views and these VM classes are typically using service agents which implement different Interfaces in order to use code stored in the class libraries mentioned in the previous paragraph.
Now, it is possible to just double click on a button and write all your code in that method created for you in the code behind file just like with Winforms, but just like in the Winforms world that leads to code that is hard to maintain, that is tightly coupled to your user interface and very difficult to test so try to resist that instant gratification and invest some time in learning the MVVM pattern, DI and OO design patterns which facilitate code reuse, decoupling and testability...
Hope this helps...
It really depends on what you are trying to learn. I don't think I would start off with WPF if I was using C#. I would start off with a console application to get the basics of the language down, then move down to a simple WinForms application, and finally to WPF where you started.
But yes, your questions about how the editor works is correct. It's how that platform works.

Adding controls to a form when I only have the reference to it’s EnvDTE

I am writing na Add-In for Visual Studio 2010, and I want it to add controls to an existing Form in an existing Project in an existing Solution, and I already have references to all of them.
As I have the reference to the Project Item that represents the file of the form, I want a reference to the Form per se, then I’ll be able to do anything to it (changing properties, and adding controls).
I have tried some approaches, though I must admit I haven’t ran out of tries. But since this is quite an interesting subject, instead of keeping on beating around the bush, I decided to write this question, so it would be faster for me, and would be registered for future similar doubts from anyone else.
Summarizing:
I have:
EnvDTE.ProjectItem myPrjItemForm
And I want to have:
System.Windows.Forms.Form myFormObject
Have a look at these articles:
HOWTO: Add a control to a Windows form from a Visual Studio add-in
HOWTO: Manipulating controls of Windows forms from Visual Studio .NET add-ins
It's VB code, but as far as I can tell, it illustrates the steps you need to perform.

Creating Multiple Forms By Combining Multiple Projects

Assume that I have a project that contains two buttons on it. Each buttons generate a custom form. I wrote both forms manually. But I wonder, is there a way that I can make these forms by using toolbox in different projects and combine them in one solution. I mean, when I press a button I want to call another project.
And finally if such a solution exists, is it proper way to make programs like this , or is it better to create forms manually ?
You can call a form from another project (if we are talking about VS project) if it is public. You need to add reference to the project where this form is "placed" to the project where the call is made.
Of course, you can create forms manually, but you can as well create new windows form and add controls to it via designer.
And about the proper way. If you need the same form to be called from different projects, then, yes, keeping in in some third project is alright.
Yes you can invoke the forms even if they are in different projects. But you need to have references to that projects and to have defined a proper using statements.

Is there a utility that can monitor open windows/ in .net winforms?

This is a general question, but I'll explain my specific need at the moment:
I want to find the framework class that enables one to choose an image at design-time. I can find the editor that is used at run-time - its the Drawing.Design.ImageEditor. At design time, however, a different editor pops up which allows one to choose an image from resources.
I'm guessing I could run some kind of program, then open up the image editor, from the property grid, and see what new windows/classes have been created?
Thanks
Yes, you can see what's being used by using another instance of Visual Studio and use Tools + Attach to Process (managed) to look at the call stack. It is a Microsoft.VisualStudio.Windows.Forms.ResourcePickerDialog. That is not something you can use in your own code, the Visual Studio designer assemblies are not re-distributable. Nor would they be useful, they monkey with the design-time state of the project.
Making you own isn't that hard, just use Reflection to iterate the properties of Properties.Resources and find the ones that have the Bitmap or Icon type. Display them in a ListView to allow the user to pick one. Adding resources at runtime isn't an option.
A tool with similar functionality to what you mention is Spy++ which you can find in your Visual Studio folder on the start menu (under the sub menu Visual Studio Tools).
However, if I understand you correctly, I don't think the design time editor you're talking about is written in managed code and even if it was, I'm fairly sure it's not in the framework. It's just part of Visual Studio itself and as far as I know you can't get hold of the source code for that.

Categories

Resources