|
|
I have a form that I need to set a vlaue in from another form. I know I can do this with a property. the problem is that form I want to set the property on is already active. The olny way I know to set the property is to do somthing like
formname f = new formname();
f.propName = my vlaue;
f.show();
this would create a new form not use the one I already have and I am at a lost as to how to set the prop in the form that is already loaded.
thanks for any help.
|
|
|
|
|
in your main form clas create a member variable (reference)
formname f;
in the load event of the main form :
f = new formname();
then in your code:
f.propName = my vlaue;<br />
f.show();
this way you always use the same formname instance..
i wish this can help..
|
|
|
|
|
dose this not create a new instance of the form and not reffrence the one I curently have open.
I was able to make it work by making it static. Is this a good way of doing it or did I gat lucky
|
|
|
|
|
I think my way (member variable) is more clear nad reasonable.
|
|
|
|
|
So, what do you want?
Do you want to set value to Properties which is already defined in Form class or your own property defined in your customized Form class?
|
|
|
|
|
I am a novice developer and am trying to do this:
I have created a custom event which I want to fire from within the constructor of MyClass. Now when it tired to fire I get a runtime error which says "Null Reference", possibly pointing at "this"-
Here is the snippet:
public delegate void MyLogEventHandler(object sender, LogEventArgs e);
public class BotNode : GoGeneralNode
{
public event MyLogEventHandler BotCreatedEvent;
public BotNode()
{
LogEventArgs myLogEventArgs = new LogEventArgs("Bot Node Created");
BotCreatedEvent(null, myLogEventArgs);
}
Question: Is it impossible to pass "this" as a parameter to an event within the constructor of a class? At this point, can I only use this within the class, or can I safely pass it as part of an event invocation.
If so:
1) Why can't I use it that way within the constructor?
2) How do I solve this?
Maybe I am just making a stupid mistake. Apologies for my ignorance.
HEELP!
Andy
|
|
|
|
|
You can't fire an event in the constructor. Here's why (and the answer to your problem). Events in .NET are based on the listener principal. Each even has a collection of listeners associated with it (by default). If there are no listeners, the event list is null. That's why you're getting the NullReference exception. You can't do this in your constructor because until your constructor finishes and an instance is created, callers must wait before attaching an event handler to the event. If you want to fire an event while your object is being created, then you should use static events which work the same way:
public class MyClass
{
public static event EventHandler Created;
public MyClass()
{
if (Created != null) Created(this, EventArgs.Empty);
}
} See how I checked if the event was null (i.e., the event handler collection is empty)? There is a more common practice for this, especially with instance events. The .NET BCL (base class library) does it throughout and it's a good idea to do in your code, that way derived classes don't have to listen to the event which is a little slower. You use a protected virtual method that raises the event and simply call that:
public class MyClass
{
public MyClass()
{
}
public event EventHandler MyEvent;
protected virtual void OnMyEvent(EventArgs e)
{
if (MyEvent != null) MyEvent(this, e);
}
}
public class MyClass2 : MyClass
{
public MyClass2()
{
}
protected override void OnMyEvent(EventArgs e)
{
if (e != null) DoSomething();
base.OnMyEvent(e);
}
}
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
1) Very good response
2) It works!
3) I learned something important.
and finally ...
4) You're my hero!
Thanks for the very good and easy to understand explanation including sample code (!). Can't ask for more than that.
Andy
|
|
|
|
|
andrewstan wrote:
3) I learned something important.
I'm glad to hear it! I may come off as a bit of an ass sometimes to some people, but this is all I'm trying to foster - education. I'm glad I could help!
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
Heath Stewart wrote:
I may come off as a bit of an ass sometimes to some people
Damn the torpedoes, full steam ahead.
Andy
|
|
|
|
|
Hi.
How do i drag and move a window using the mouse, when the window do not have a titlebar???
I thought of using MouseDown, MouseMove and MouseUp to move it, but i can't seem to get it to work
As their an easier way??
|
|
|
|
|
may be this code can help you
add mousemove event and form_load event to your form and three variables x,y,click,first to your class
check this code is true or not.
private int x,y;<br />
bool click=false;<br />
bool first=false;<br />
private void Form1_MouseMove(object sender,System.Windows.Forms.MouseEventArgs e)<br />
{<br />
if(e.Button==MouseButtons.Left)<br />
{<br />
if(click&&first)<br />
this.Location=new Point(this.Location.X+e.X- x,this.Location.Y+e.Y-y);<br />
x=e.X;<br />
y=e.Y;<br />
click=true;<br />
first=!first;<br />
}<br />
else<br />
click=false;<br />
<br />
}<br />
<br />
private void Form1_Load(object sender, System.EventArgs e)<br />
{<br />
x=this.Location.X;<br />
y=this.Location.Y;<br />
}
|
|
|
|
|
Great! that was exatly what i needed! thanks a bunch.
|
|
|
|
|
Back in the days of MFC, this was as simple as adding a handler for the WM_NCHITTEST (0x0084) message and returning HTCAPTION (2).
I just tried doing the equivalent of that on a C# project of mine but it didn't work!
protected override void WndProc(ref Message m)
{
if (m.Msg == 0x0084)
m.Result = (IntPtr)2;
base.WndProc(ref m);
}
What's also weird is that m.Result always comes in with a value of 0, no matter where I place the cursor on the form.
By the way, how did you create a form without a titlebar?
Regards,
Alvaro
He who laughs last, thinks slowest.
|
|
|
|
|
I think I've seen someone doing it in C# using Windows message processing like you show...
Forms can be created without titlebars via
myForm.FormBorderStyle = FormBorderStyle.None
The graveyards are filled with indispensible men.
|
|
|
|
|
Hi.
I have a batch-file that creates a logfile.
My C# Programm should read this file even if this will still be updated.
The (wonderful) editor "Textpad" can easily open that file ...
my C# Program can't .. I get 'file is used by another process' Error.
I already tried :
FileStream output = new FileStream(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read, 4196, true);
but I still get the error.
Can anyone help me PLEASE ??
|
|
|
|
|
Try using:
File myFile = File.Open(...)
using the version that takes a FileShare enumeration, and then create the FileStream from that.
|
|
|
|
|
Hi Eric.
I tried, like you said --- but this results in an error (File.Open returns a FileStream !).
He can't convert from FileStream to File .. so I assumed you meant :
FileStream myfile = File.Open(FILE_NAME, FileMode.Open, FileAccess.Read, FileShare.Read);
But this also causes the exception as before.
Or did I misunderstood you ?
|
|
|
|
|
When you specify sharing, you need to match the sharing that the file was opened with. In my tests, using FileShare.ReadWrite worked.
|
|
|
|
|
Like you said : It works !
I misunderstood the FileShare .. I thought that was for my access.
Thank you very much !
|
|
|
|
|
What's wrong with this code?
I'm trying to create a new Win32_NetworkAdapterConfiguration object using
C# and copy an older object into it. I get the "Provider is not capable
of the attempted operation" exception at the CopyTo
path = "Win32_NetworkAdapterConfiguration=1";
ManagementObject MyOldConf = new ManagementObject(path);
MyOldConf.Get();
path = "Win32_NetworkAdapterConfiguration";
ManagementClass MyClass = new ManagementClass(path);
MyClass.Get();
ManagementObject MyNewConf = MyClass.CreateInstance();
MyClass.Put();
tr("MyNewConf "+ MyNewConf.ToString());
MyOldConf.CopyTo(MyNewConf.Path); //ERROR!!!
Console.WriteLine("new conf : " + MyNewConf["Description"]);
|
|
|
|
|
How can write a program that dose not need any install or setup and all of dll or libreries be with program(for example only copy program in destination and progarm work)
|
|
|
|
|
You're gonna have to redistribute the .Net framework, largely because MS hasn't really pushed it to end-users yet.
There is an app I downloaded a day or two ago that allowed you to take only the portions of the .Net framework that your app uses and bundle it inside your executable (along with any other dlls needed by your app). Can't remember the name of it offhand...maybe someone else knows it.
The graveyards are filled with indispensible men.
|
|
|
|
|
Besides the .NET Framework (which MUST be installed), you can use touchless deployment by writing your application correctly, i.e. rely on .config files only (no registry).
The application I architected runs both from local or LAN machines, or from across the Internet or an intranet with no change. The assembly binding uses the current codebase of the application plus any private paths your config files specifies (see the <runtime> section documentation in the .NET Framework SDK).
You also need to worry about code access permissions, though. In .NET 1.0, the default Internet Zone allows no code to run. In .NET 1.1 some basic permissions are granted to code running from the Internet. You can change or add these code policies, though. There is plenty of documentation about code access security in the .NET SDK Documentation. Just look for "code access security". I'm also working on an extensive article or chain of articles but it'll be some time.
It's important to read, though, because you really must understand them before using them. You don't want to open someone's machine to attach by accident, and you want to make your application work with different permissions granted, i.e. some are required and some are optional. Take a look at the System.Security.Permissions namespace classes for more information about this stuff, as well as the documentation for code access security.
If you don't need to run from a remote source, you don't necessarily have to worry about it (although it's still a good idea to check permissions in your app before executing an action that requires them, otherwise a SecurityException is thrown). Just use COPY/XCOPY to copy your apps and dlls to the local machine and make sure they are in the right place. You can leave all the exes and dlls together with no problem. You can use the application configuration file to specify additional paths, similar to what ASP.NET Web Matrix does.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|