I'm trying to use system.management in a C# windows forms app in VS 2017, but it just won't work.
Here is a code sample:
using System;
using System.Windows.Forms;
using System.Management;
namespace MyWMIapp
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void okButton_Click(object sender, EventArgs e)
{
ManagementScope scope = new ManagementScope(#”\\MyComputerName\root\CIMV2″);
}
}
}
At: "using System.Management;" it tells me: this is not needed/used.
At: "ManagementScope scope = ......" it tells me: not found (maybe a missing using-Directive), and is red underlined.
Both is obviously wrong. I'm loading it and I'm using it. Why don't both lines "see" each other?
Add reference to System.Management.dll to your project.
Related
I found this code in stackOF but it doesn't work at all and i can't fix that.
would you tell me what's wrong with this code?
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Button1_Click(object sender, EventArgs e)
{
Clipboard.SetText(textBox1.Text);
}
}
}
i think your problem because you named your program with same name of class that copy text to clipboard
take look at this code
using System.Windows.Forms;
namespace Clipboard
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Clipboard.SetText(textBox1.Text); This will not work if you named your namespace Clipboard !
System.Windows.Forms.Clipboard.SetText(textBox1.Text); // you should use this way to confirm you need to access to clipboard not your namespace
}
}
}
i named my program with same name of class(Clipboard)
and i have problem now because the compiler confusing between your program and class that copy a text
So the best way is to specify a unique name each time you create a program :)
There is a namespace conflict with your own. You can either explicitly use the exact Clipboard.SetText() method using the full declaration as per #WaleedKhaled's answer:
System.Windows.Forms.Clipboard.SetText(textBox1.Text);
or else the using statement at the top of your example to say something like:
using WinForms = System.Windows.Forms;
then your line would read:
WinForms.Clipboard.SetText(textBox1.Text);
I am putting together an application but I'm getting a strange issue where i can't use any methods from a class i've created with a couple of methods, the methods don't do anything at the moment because I'm just getting the shell of the program in place. I am trying to call from the Form1 class below, specifically from a button click checking a specific operation from radio buttons.
If btnDeviceControlAccept_Click is clicked it checks which of the radio buttons and goes to a method in the DeviceControlMethods class such as Add, Change or Delete VLAN. When i use the object (dc, DeviceControlMethods dc = new DeviceControlMethods();)I created in the Form1 i'm unable to use the methods even if the class is public or if i set the methods to static and use DeviceControlMethods.AddVlan etc.
I'm sure I'm just doing something daft because I've not doing C# in quite a while.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MFT___Configurator
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnDeviceControlAccept_Click(object sender, EventArgs e)
{
DeviceControlMethods dc = new DeviceControlMethods();
if (rbAddDevice.Checked == true)
{
dc.CreateVlan() // the method is not found
resutlBox.Clear();
}
else if (rbChange.Checked == true)
{
resutlBox.Clear();
}
else if (rbDelete.Checked == true)
{
resutlBox.Clear();
}
else
{
resutlBox.Clear();
resutlBox.Text = "Select a valid operation; Add, Change or Delete.";
}
}
Class with the methods i want to call;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MFT___Configurator
{
public class DeviceControlMethods
{
static DeviceControlMethods()
{
string CreateVlan()
{
Console.WriteLine("ggg");
return "";
}
string ChangeVlan()
{
return "";
}
void DeleteVlan()
{
}
}
}
}
I see only private methods, you need to make them public explicitly, not only the class. See the docs about access modifiers
public
The type or member can be accessed by any other code in the same assembly or another assembly that references it.
private
The type or member can be accessed only by code in the same class or struct.
protected
The type or member can be accessed only by code in the same class, or in a class that is derived from that class.
internal
The type or member can be accessed by any code in the same assembly, but not from another assembly.
protected internal
The type or member can be accessed by any code in the assembly in which it is declared, or from within a derived class in another assembly.
private protected
The type or member can be accessed only within its declaring assembly, by code in the same class or in a type that is derived from that class.
Edit
And, as other comments state as well, methods defined in the static constructor won't be accessible either.
You have scoping issues with your class. Read through this article to learn more about scoping in C#. https://msdn.microsoft.com/en-us/library/ms973875.aspx
But to solve your issue, change your class to be as follows:
public class DeviceControlMethods
{
public string CreateVlan()
{
Console.WriteLine("ggg");
return "";
}
public string ChangeVlan()
{
return "";
}
public void DeleteVlan()
{
}
}
I like to connect the DLL (which contains namespace Chart) and call the subroutine public Simple() from the second class (namespace WinApp2). There is a error:
couldn't find type or namespace name Simple
Although I consider a reference and put using Chart and created a instance in the second namespace.
namespace Chart
{
public partial class Simple : Form
{
Manage _mProject;
public Simple()
{
InitializeComponent();
_mProject = new Manage();
_mProject.Add(new Task() { Name = "New Task" });
_mChart.Init(_mProject);
}
}
}
using Chart;
namespace WinApp2
{
public partial class Form4 : Form
{
Form2 fh;
public Form4(Form2 aufrufer)
{
fh = aufrufer;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var instance = new Simple(); (error !!!)
instance.Simple();
}
}
}
What do you mean by "consider a reference and put using Chart" ? Did you actually include the dll in References section, or did you put using Chart and think that will reference the relevant dll?
The proper steps are:
Right click References section in VS
Choose Add Reference
Browse to location of relevant dll, choose OK
Another cause may be that Chart is a defined class in System.Web.DataVisualization.dll, so VS is confused. Try changing your Chart namespace to something else, e.g., MyChart
Hope this helps..
I'm terribly new so I might be completely off track overall with what I'm trying to do.
I don't really know how to ask the question, my english is a bit rocky.
But I have 2 files one containing this:
frmMain.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
Class1 cls = new Class1();
cls.Visibility();
}
}
}
And another file containing this:
Class1.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1
{
public partial class Class1
{
public void Visibility()
{
frmMain c = new frmMain();
c.label1.Visible = false;
}
}
}
What I'm trying to get is that when I'm running the program and clicking label1 I want it to disappear.
But it doesn't. I don't get any errors or anything.
Any help is appreciated :).
Thanks in advance.
First: Why are you trying to let the label on your mainform dissappear by using another class?
I would suggest the following:
private void label1_Click(object sender, EventArgs e)
{
label1.Visible = false;
}
I think the reason why your code isn't working is that inside the function Visibility() of Class1 you are creating a new frmMain and on that frmMain you are setting the visible property of label1 to false. So you are actually working with a different form.
You are instantiating a new, separate form. This means the label is being hidden.. but on a hidden form you have created.
You need to pass the current form instance into your other class:
public void Visibility(frmMain mainForm) {
mainForm.label1.Visible = false;
}
Then call it like this:
new Class1().Visibility(this);
What you're doing is you're creating a second instance of your window (which might not be obvious to you, as you're not displaying it). Then you are hiding the label in your second window. Probably not what you intended in the first place.
What you need to do is to pass a reference to your original form to the method you're calling, or (depending what you want to do) a reference to the control you need to hide:
in Class1:
public void Visibility(Control controlToHide)
{
controlToHide.Visible = false;
}
in frmMain.cs
new Class1.Visibility(this.label1)
few more comments:
Naming: do not use names like Class1, label1; I appreciate this is probably
just 'play around with' kind of code, but such names are completely
unreadable when you try to come back to your code later (or get
someone else to have a look)
Naming 2: try to name your methods to describe what they will do - HideControl, or HideLabel is much better than Visiblity
You may want to read some basic C# tutorials to learn about references, instances, parameters, etc.
Other than that, happy C#-ing :)
You do not want to let Class1 know about frmMain. Change it to something like this:
public class Class1
{
public bool GetVisibility()
{
return false;
}
}
And from your form, call it like this:
private void label1_Click(object sender, EventArgs e)
{
Class1 cls = new Class1();
this.Label1.Visible = cls.GetVisibility();
}
Your current implementation of Class1 initializes a new frmMain, hides that form's Label1, does not do anything with that instance (e.g. it does not Show() it) and then returns, not affecting the already instantiated and shown frmMain instance (the one you instantiate Class1 from).
You can change this by passing the label or even the form into Class1, but that is just bad design.
You may change your code this way:
public partial class FrmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
new Class1().Visibility(this);
}
public void Go()
{
this.label1.Visible = false;
}
}
Then
public partial class Class1
{
public void Visibility(FrmMain form)
{
form.Go();
}
}
You are setting the visibility of the label of another form (one that's not being displayed.
this line of code in the Visibility method creates a new object
frmMain c = new frmMain();
It's of the same type as the form being display, but it's a different object, that's not displayed. If you insert a line after the above
c.Show();
you should see the newly created form and also see that the label disappears
However there's a straight forward fix to your problem. Change the event handler to this
private void label1_Click(object sender, EventArgs e)
{
((Label)sender).Visible = false;
}
The sender object is the control that was clicked, and since the event is attached to the label it self. All you need to do is cast the sender to the type of a Label and then you can access the visible property. Alternatively you could do this:
private void label1_Click(object sender, EventArgs e)
{
this.label1.Visible = false;
}
That uses the current object (aka this) and gets the label from that object.
I have some issues displaying a second form after clicking a specific button in my first form. The question might sound silly, but I am a newbie to programming...
I have added a new Windows form into my project (Form2), but still, when I use
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Report()
{
InitializeComponent();
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 Report = new Form2();
Report.Show()
}
}
}
I get the following error
Error 1 The type or namespace name 'Form2' could not be found (are you missing a using directive or an assembly reference?)
d:\Projects C#\The Bizzy D\The Bizzy D\Form1.cs 661 13 The Bizzy D
What am I doing wrong then? Any ideas would be appreciated. Thanks!
You forgot a using directive as the error message states. The problem is, that even if you created this second form, the connection between the type name Form2 and the file it contains isn't clear to C#.
Check if your Form1 and Form2 are in the same namespace.
If you Want To Open Form2 On Form1 Button Click Event.
Follow the Below Steps:
Right Click on Project.
Add->Windows Form...
Enter the Form Name : Form2.cs
Write the following Button Click Event Code in Form1.
private void btnShowForm2_Click(object sender, EventArgs e)
{
new Form2().Show();
}