|
I'm trying to:
1st Create an array containing ref's to labels.
2nd Initiate them with a for loop and add some text to the label.
3d Add them to a panel with controls.add() either in the same loop or afterwards
The problem:
My code below sort of works but it will just add 1 control instead of 4 in my dummy example... what I'm I missing here??
I am aware I'm not setting any location Point prop's in the code below but im sure that isnt the problem...
private void btnExampleClick(object sender, EventArgs e)
{
Label[] lbl = new Label[4];
for (int i = 0; i < 4; i++)
{
lbl[i] = new Label();
lbl[i].Text = i.ToString();
}
foreach (Label x in lbl)
{
Controls.Add(x);
}
}
|
|
|
|
|
Hi,
welcome to CodeProject.
please use PRE tags when showing code, or select the code and use the "code block" widget.
why are you having a separate loop to add the labels to the Controls, you could do that in the first loop.
You did add 4 labels, however you need to set the label's Location, they are all on top of each other.
|
|
|
|
|
I am having a hard time figuring this out. My applications is using WMI to get information about a remote computers disk drives, installed applications, and services (all of this works fine). I can seem to get all this information but I cannot get the computer to stop a remote service.
I keep getting "RPC server is unavailable". How is this possible? I am able to retrieve information from these computers so it can't be unavailable.
At first I kept getting 'Access Denied' errors. I was trying to make it use the windows username and passwords somehow instead of having to enter a username and password.
So now I'm doing this:
ConnectionOptions conn = new ConnectionOptions();
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.Username = "DOMAIN\username";
conn.Password = "password";
conn.EnablePrivileges = true;
Like I said.. I can connect and get all the information in the world I want, but cannot seem to stop a service. I read this other article about settings permissions and I have already tried that by going through computer management on the server.
Oh and by the way I am using the top domain admin user to do this.. so there is not another user in this domain that has more rights.
If you are interested in the whole code it is here: (Thanks to (Windows Management Instrumentation (WMI) Implementation[^]) for most of it)
ServiceInfo service = (ServiceInfo)serviceInfo;
ManagementOperationObserver observer = new ManagementOperationObserver();
MyHandler completionHandlerObj = new MyHandler();
observer.ObjectReady += new ObjectReadyEventHandler(completionHandlerObj.Done);
try
{
ConnectionOptions conn = new ConnectionOptions();
conn.Impersonation = ImpersonationLevel.Impersonate;
conn.Username = "DOMAIN\username";
conn.Password = "password";
conn.EnablePrivileges = true;
ManagementScope scope = new ManagementScope("\\\\" + service.DNSHost + "\\root\\CIMV2", conn);
scope.Connect();
ManagementObjectSearcher mos = new ManagementObjectSearcher(scope, new ObjectQuery("SELECT * FROM Win32_Service WHERE DisplayName='" + service.item.Text + "'"));
foreach (ManagementObject mo in mos.Get())
{
bool Started = Convert.ToBoolean(service.item.SubItems[1].Text);
if (Started)
mo.InvokeMethod(observer, "StopService", null);
else if (!Started)
mo.InvokeMethod(observer, "StartService", null);
int intCount = 0;
while
(!completionHandlerObj.IsComplete)
{
if (intCount > 10)
{
MessageBox.Show("Terminate process timed out.", "Terminate Process Status");
break;
}
System.Threading.Thread.Sleep(500);
intCount++;
}
if (completionHandlerObj.ReturnObject.Properties["returnValue"].Value.ToString() == "0")
{
if (!Started)
{
service.item.SubItems[1].Text = "True";
service.item.SubItems[1].BackColor = Color.LimeGreen;
service.item.SubItems[1].ForeColor = Color.Black;
}
else
{
service.item.SubItems[1].Text = "False";
service.item.SubItems[1].BackColor = Color.Red;
service.item.SubItems[1].ForeColor = Color.White;
}
}
else
{
MessageBox.Show("Failed.", "Start/Stop Service Failure");
}
}
}
catch (Exception ex) { MessageBox.Show("Error starting/stoppping service.\n\n" + ex.Message); }
finally
{
Invoke((Action)(() => { picProgressServices.Visible = false; }));
}
}
|
|
|
|
|
I guess I could always use a ServiceController. I was able to get taht to work great. Would still like to understand more why the wmi is being so difficult
|
|
|
|
|
How can I use the mouse event to move object
|
|
|
|
|
search CodeProject articles for movable controls
|
|
|
|
|
Whenever I try to add new functions or method via Class Diagram,
VS always insert this code:
throw new System.NotImplementedException();
1. What kind of exception is this and what is it's use?
2. How can I configure VS to not generate this code?
Thanks a lot.
|
|
|
|
|
The exception does exactly what it says on the tin. It is used where a method that should be implemented isn't, presumably so that you don't forget to implement the method. I don't know if you can change a setting to make it not show up, in my opinion, doing so could make it very easy to forget to implement a function, leading to potentially hard to diagnose bugs.
|
|
|
|
|
So I will just have to delete it if I'm going to right code inside the function?
|
|
|
|
|
Unless you want your code to throw a NotImplementedException, yes.
|
|
|
|
|
Sorry, I'm kinda new to the term. What do you mean by "implementing the function", using it in a program or adding functionality in it?
|
|
|
|
|
He means: write the code for your function and delete the line:
throw new System.NotImplementedException();
Unless you have a very good reason to keep it there.
|
|
|
|
|
If I write a code for the function, then what will be an example of a good reason to leave that there?
|
|
|
|
|
It's difficult to say, because once you've got the function written, it's been implemented. However, you could be writing a plugin-based application. The plugin base class could look something like this:
public abstract class PluginBase
{
public string Name { get; set; }
public virtual void Activate<T>(T input)
{
throw new System.NotImplementedException("This base class method must be overriden by a plugin");
}
}
|
|
|
|
|
Ok. I understand now. Thanks.
|
|
|
|
|
Of course not. You know how you want your code to be, right? Make it so. If that includes throwing a NotImplementedException then so be it.
|
|
|
|
|
This may not be the best forum to post, but I am using C#.
I add a Special field: »Page N to M«. But I want to have a text in different language: »Stran: N od M«
Thanks in Advance
|
|
|
|
|
I am not sure whether this will work but if you can load this field dynamically from C# (or .NET) do the following:
1) Create a resource file
2) add the field name and text
3) use this in the code instead of the hard coded string
4) Add another resource file with the culture name inbetween the file name and the .resx (e.g. myResource.resx will become myREsource.fr-FR.resx)
5) Copy the exact field to the second file
6) Write it in your language
7) Change the appplication culture to the resx culture
|
|
|
|
|
Thank you, will try later
|
|
|
|
|
Hi all i need helping of ...
how to send a TCP & UDP packets using Socket class........
Regards................................
|
|
|
|
|
I am working in a project implemented in COM (ATL)where , my class derives from app 12-14 classes.
Now while implementing the same class in C#, i am facing problem (as C# not support multiple inheritance).
Can you please provide me the ways we can archive multiple inheritance in .net.
|
|
|
|
|
Message Closed
modified 23-Nov-14 6:58am.
|
|
|
|
|
stancrm wrote: There are no multiple inheritance in C#.
Correct.
stancrm wrote: But this is the answer :
No it isn't.
|
|
|
|
|
|
It sounds like the old application was poorly designed, rewrite it.
|
|
|
|