GUI in C# without Visual Studio - c#

Okay, I'm a newbie in C# but I need to create a simple GUI, but I don't have Visual Studio (I use Geany and Mono).
The problem is, when I tried the following code that I found by Google:
using System;
using System.Windows.Forms;
using System.ComponentModel;
using System.Drawing;
public class FirstForm : Form
{
private Container components;
private Label howdyLabel;
public FirstForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
components = new Container ();
howdyLabel = new Label ();
howdyLabel.Location = new Point (12, 116);
howdyLabel.Text = "Howdy, Partner!";
howdyLabel.Size = new Size (267, 40);
howdyLabel.AutoSize = true;
howdyLabel.Font = new Font (
"Microsoft Sans Serif",
26, System.
Drawing.FontStyle.Bold);
howdyLabel.TabIndex = 0;
howdyLabel.Anchor = AnchorStyles.None;
howdyLabel.TextAlign = ContentAlignment.MiddleCenter;
Text = "First Form";
Controls.Add (howdyLabel);
}
public static void Main()
{
Application.Run(new FirstForm());
}
}
I just get these errors when trying to compile:
C:\C#\test2.cs(2,14): error CS0234: The type or namespace name 'Windows' does not exist in the namespace 'System'. Are you missing an assembly reference?
C:\C#\test2.cs(4,14): error CS0234: The type or namespace name 'Drawing' does not exist in the namespace 'System'. Are you missing an assembly reference?
C:\C#\test2.cs(9,11): error CS0234: The type or namespace name 'Label' could not be found. Are you missing a using directive or an assembly reference?
Compilation failed: 3 error(s), 0 warnings
I downloaded both DLL's, but I don't know what to do next.
Link to the code: http://www.informit.com/articles/article.aspx?p=27316

You're using the Microsoft WinForms UI library, which Mono does not include.
You need to use a Mono-compatible UI library, such as GTK#.
You can also use the Mono port of WinForms.

You are using WinForm on mono, maybe will be better use GTK#, however if you want use WinForms on mono it is possible. Take a look at this documentation for more information .
You have to use gmcs in this way to compile your program :
gmcs Program.cs -r:System.Windows.Forms.dll

Related

Compiling c# in linux using mcs compiler gives error : The type or namespace name `Forms' does not exist in the namespace `System.Windows'?

I am trying to compile some c# code in linux, i can compile the same code in Windows using the csc command in Visual Studio command line. I also have already installed mono-devel.
this is the exact error :
mycode.cs(8,22): error CS0234: The type or namespace name `Forms' does not exist in the namespace `System.Windows'. Are you missing `System.Windows.Forms' assembly reference?
mycode.cs(70,27): error CS0246: The type or namespace name `Form' could not be found. Are you missing an assembly reference?
Why is this happening and how to fix it?
this is the command I'm using to compile it in linux :
mcs mycode.cs
and inside the code, Windows forms is already included :
using System.Windows.Forms;
also simple hello world code does get compiled without any error, i basically followed this blogpost about how to compile c# code in linux to set everything up :
https://jonsson.xyz/2016/11/23/csharp-linux/
Have you tried compiling with -r:System.Windows.Forms.dll ?
for example:
wc.cs:
using System.Windows.Forms;
public class Program
{
[STAThread]
public static void Main()
{
var f = new Form();
f.Text = "Hello World";
Application.Run(f);
}
}
then run:
$ mono-csc wf.cs -r:System.Windows.Forms.dll
and you get wf.exe

The namespace name 'SyntaxTree' could not be found

I have am developing in Unity 2019.2.3f1. I am trying to write a script that can Customize project files created by VSTU. In case the link ever gets removed, I have included a script very similar to the one from the link.
#if ENABLE_VSTU
using System.IO;
using System.Text;
using System.Xml.Linq;
using UnityEditor;
using SyntaxTree.VisualStudio.Unity.Bridge;
[InitializeOnLoad]
public class ProjectFileHook
{
private class Utf8StringWriter : StringWriter
{
public override Encoding Encoding => Encoding.UTF8;
}
static ProjectFileHook()
{
ProjectFilesGenerator.ProjectFileGeneration += (string name, string content) =>
{
// parse the document and make some changes
XDocument document = XDocument.Parse(content);
document.Root?.Add(new XComment("FIX ME"));
// save the changes using the Utf8StringWriter
Utf8StringWriter str = new Utf8StringWriter();
document.Save(str);
return str.ToString();
};
}
}
#endif
The issue is, using SyntaxTree.VisualStudio.Unity.Bridge; fails to compile due to the error error CS0246: The type or namespace name 'SyntaxTree' could not be found (are you missing a using directive or an assembly reference?).
I have checked both Unity and Visual Studio that the Visual Studio Tools for Unity are installed and enabled.
Why is the code failing to compile? Am I missing something that is preventing it from compiling?
Just incase anyone else has this issue.
This issue was being caused by the scripts position. The script has to be under the Editor folder, otherwise the script won't work as expected.
Credits to therealjohn for pointing it out here.

Installed NuGet package not in namespace?

I installed flawless this package:
https://www.nuget.org/packages/ini-parser/
https://github.com/rickyah/ini-parser
It shows up in my project dependencies and i am using the directive suggested
by the author:
using IniParser;
using IniParser.Model;
But the "code" is not seen in my Form1.cs where i put the directive ?
Error CS0246.
I am a beginner so you know.
you may need to understand how to apply [using][1] and differences between Directive and static Directive and normally diagnostic with fully qualified type name to understand better.
I installed the nuget and put the using directives in and could instantly use it
namespace WindowsFormsApp1
{
using System.Windows.Forms;
using IniParser;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
var parser = new FileIniDataParser();
var data = parser.ReadFile("Configuration.ini");
var useFullScreenStr = data["UI"]["fullscreen"];
var useFullScreen = bool.Parse(useFullScreenStr);
data["UI"]["fullscreen"] = "true";
parser.WriteFile("Configuration.ini", data);
}
}
}
It was an easy solution. I had to refresh the library or package dependencies and it instantly found it. Thank you for the answers.

using namespace in C# from CLI/C++ class

This might be a C#-noob question...
Assume I have the following CLI/C++ header:
namespace DotNet {
namespace V1 {
namespace X {
public ref class AClass {
public:
AClass() {}
void foo() {}
static void bar() {}
};
}
}
}
Then from C# I do the following:
using DotNet;
V1.X.AClass a = new V1.X.AClass();
I get:
Program.cs(18,7): error CS0246: The type or namespace name 'V1' could
not be found (are you missing a using directive or an assembly
reference?)
Same with:
using DotNet.V1;
X.AClass a = new X.AClass();
Program.cs(18,7): error CS0246: The type or namespace name 'X' could
not be found (are you missing a using directive or an assembly
reference?)
What works is:
DotNet.V1.X.AClass a = new DotNet.V1.X.AClass();
Or
using DotNet.V1.X;
AClass a = new AClass();
So either I have to use the full namespace path, or I have to open all of the path to access the class. Is there anything I can do about this?
I would like to be able to open only a part of it.
So either I have to use the full namespace path, or I have to open all of the path to access the class.
Yes.
Is there anything I can do about this?
Not that I'm aware of. Why would you not just want a using directive for the whole namespace? Isn't that the simplest approach?
Note that this has nothing to do with C++/CLI. The same is true whatever the source language is - so you can see it with C# as well:
namespace DotNet.V1.X
{
public class AClass {}
}
As Jon Skeet stated you have to include the full namespace in either the using statement or each time you reference it in code. I recommend just placing the full namespace in the using statement.
You can, however, achieve that syntax with a using alias, but it does not add anything of value outside of syntax.
using V1 = DotNet.V1;
...
V1.X.AClass a = new V1.X.AClass();
Also if you are using C# version 3.0 or higher the var keyword will only require you type out the full namespace on the right side of the assignment.
var a = new DotNet.V1.X.AClass();

How to Use XDocument class in SilverLight Project (C#)

I'm trying to create a Silverlight application (for the first time) that involves parsing XML from a site and displaying information. To do this I am using Visual Studio 2008 on Windows XP Service Pack 3. I also have .NET Framework 3.5 SP1 installed.
My problem is that no XML-parser I have seen on the internet works. The top of my code I have both lines I believe are necessary (using "System.xml;" and using "System.linq;") but XDocument, XMLReader, XMLDocument, and any others I have found do not work, returning the error that the type or namespace cannot be found. I do have .NET Framework.
I have turned absolutely nothing up on the internet regarding this problem. Does anyone have any ideas?
EDIT: I just discovered that when I open the file outside of the context of a Silverlight project, it is able to use XDocument. It is only when I open the entire project that my problem occurs
Here is some sample code showing the problem:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq; //Error 1 (See below)
namespace LastfmAmazon
{
public partial class Page : UserControl
{
public Page()
{
InitializeComponent();
}
public void DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XDocument doc = XDocument.Parse(e.Result); //Error 2: see below
}
public void Button_Click(object sender, RoutedEventArgs e)
{
if (uname.Text != String.Empty)
{
App app = (App)Application.Current;
app.UserName = uname.Text;
String getTopArtists = "http://ws.audioscrobbler.com/2.0/?method=user.gettopartists&user=" + app.UserName + "&api_key=d2d620af554a60f228faed8d502c4936";
uname.Text = "Try Another One!";
WebClient web = new WebClient();
WebClient client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DownloadStringCompleted);
client.DownloadStringAsync(new Uri(getTopArtists));
}
}
}
}
Error 1: This line contains the following error: The type or namespace name 'Linq' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)
Error 2: This line contains the following error: The type or namespace name 'XDocument' does not exist in the namespace 'System.Xml' (are you missing an assembly reference?)
EDIT 2: Once I Googled what it meant to "add a reference" to a library, Anthony's answer solved the problem.
By default a Silverlight project will contain the System.Xml dll however XDcoument is contained in the System.Xml.Linq dll, this you will have to add to your project.
Make sure you add a reference to the appropriate XML library
For XMLDocument, XMLReader, etc ...: System.Xml.Dll
For XDocument, XNode, etc ...: System.Xml.Linq.dll

Categories