|
1. This code as-is will not compile: the new Form 'MyAppFrm' is created inside the Load event: outside of the scope of the Load event it will not exist: thus you cannot 'refer' to it inside the button1 Click EventHandler.
The "fix" for that is easy enough: make MyAppFrm a class-scoped variable defined outside the Load Event:
private Form MyAppForm;
private void Form1_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.Closing += new CancelEventHandler(MyAppFrm_Closing);
} 2. In your original code you can "get away with" accessing MyAppFrm in the MouseLeave event handler: because that EventHandler had a valid reference to the instance of MyAppFrm <i>when it was added in the Load event of Form2</i>. And, after all, you reference it only indirectly by calling 'GetType() on the 'sender parameter of the EventHandler.
3. Note that in the example above I changed the Event in MyAppForm to 'FormClosing, not 'MouseLeave: if you use 'MouseLeave you'll get an initial trigger of that Event just by the call to ShowDialog ... and, as explained below: showing modally excludes "leaving."
There are good reasons, at times, to show a Form using 'ShowDialog: but in your code I can't detect any reason why are you using 'ShowDialog.
When you use 'ShowDialog: the reason to use it is to prevent any "moving away from it," to block the user from changing focus until the user does something that closes/dismisses the modally shown Form.
Suggest you sit down and try and think through and outline your design goals here: what's going to be inside the new Form created (which is just a blank, "vanilla" Form, now).
If this is a homework question that really does ask "How to access the control of a form from diffrent form?:" then you should be pre-defining ... at Design-Time probably ... the new Form that is created in Form2, and put some Control on it, and focus on the issue of how you get access to that Control from Form2 ?
good luck, Bill
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
modified 10-Jan-12 14:23pm.
|
|
|
|
|
Thankyou Bill, I get clear...very nice reply...
|
|
|
|
|
Hello,
I want to create a remote support application which will aneble user to connect to a remote pc and take control of it (just like logmein), I need it in vc# .net or vb .Net. I am searching articles or web articles to guide me. Also for multiple desktop sharing (be able to connect to multiple pcs).
Thank you
Kyriakos
Kyriakos
|
|
|
|
|
|
|
Hi, I'm making a C# program that will need to load a .DDS image file ('direct draw surface'), the ones I'm using will only ever have a simple header and uncompressed image data.
There are ways to handle .DDS's using XNA and a few other free to use libraries, but since the files I'm using are so simple and the structure so well documented I thought I would make my own 'reader' so as to be as lightweight as possible.
I have made a method that reads the file header simply enough, giving me the height and width of the image, amongst other things. The image data itself follows the header as simple uncompressed Uint32's (sets of 4 bytes) giving the R,G,B and A of each pixel. What would be the most efficient/effective way to get this information into an Image/Bitmap object so I can display it on screen, and overlay it over other images using its Alpha values.
Although the Image class has a 'FromStream' method, I'm pretty sure it needs to read in a known header as well as the image data or it would not know how to deal with it. It certainly seems to fail if i try reading in pure data from my files.
Any thoughts or help would be greatfully recieved.
|
|
|
|
|
This MSDN page[^] is what you want; make sure to read the Remarks section too.
|
|
|
|
|
That remark makes it more than a little annoying, wouldn't it be better to create a new Bitmap(width, height), lock the bits, write data, unlock bits, as usual?
|
|
|
|
|
what it means is nothing gets stored twice, so if the source data disappears so does the image.
If you are willing to temporarily have the data twice in memory, use that constructor, then copy the bitmap with Bitmap bm2=new Bitmap(bm1); and now bm1 (and its data) is no longer needed, so Dispose of it and forget the remark.
|
|
|
|
|
IMO it means trouble. Suddenly the lifetime of one thing affects the validity of an other. Very .. manual memory management-like. Completely contrary to the entire idea of garbage collection.
I suppose you could do that too yes, but what's the advantage of that over writing it into an actual bitmap manually?
|
|
|
|
|
Nothing special here, you're passing a pointer into that constructor, hence the data isn't managed by .NET; it is inherently unsafe, just like passing pointers to native code is.
And it is somewhat related to Image.FromFile locking the file for as long as the image lives, which is troublesome as it isn't documented well and the code itself does not provide a clue. The same copy-to-a-second-bitmap trick solves that one too of course.
|
|
|
|
|
Yes I thought of that one.. very annoying. And Image.FromStream wants the stream to be kept open, and on top of that resets the Position to zero. It's actually in the documentation (at least it is now, was it always there?) but I discovered it the hard way, when the first image kept loading over and over and then all bitmaps mysteriously died..
|
|
|
|
|
I agree, I'd do that. Holding the file open for the lifetime of the image definitely seems like the wrong answer and, if you have to load it into memory anyway, I think it's better to very temporarily have two copies and then let the Bitmap own the data.
Also, depending on the order of the data in the file, you may have to reorder it so that it's in a valid 32 bit pixel format for dropping into a bitmap. I think you need ARGB but it might be BGRA in terms of the byte order in the stream.
|
|
|
|
|
Hi Amit,
I tried to run your code but i am not getting any output.
Can you please tell me if i have to do any settings for running the project?My friend told me that project runs in Windows 7 and doesnt require any settings.
I am using Windows XP and Visual Studio 2005.
Thanks in advance.
|
|
|
|
|
Which project and who is Amit? This site has millions of members so it could be anyone. If you are referring to some article then you should use the forum below the article to contact the author.
Unrequited desire is character building. OriginalGriff
I'm sitting here giving you a standing ovation - Len Goodman
|
|
|
|
|
Hi,
Sorry for posting the query at wrong place.
I was referring to the article "Search Engine for LAN".
|
|
|
|
|
Post this question against the article / tip so that the author can take a look at it.
You mighe be able to get an answer.
|
|
|
|
|
Hello,
I want to search text in a WPF richTextBox. My problem is the text is formatted...
For a normal text I use this:
string wordToSearch = "word";
TextRange tr = new TextRange(this.rtb.Document.ContentStart,
int startPos = tr.Text.IndexOf(wordToSearch);
this.rtb.Document.ContentEnd);
if (startPos > -1)
{
int l = this.GetLinesCount(tr.Text, startPosition);
TextPointer startRtb = this.rtb.Document.ContentStart;
TextPointer start = startRtb.GetPositionAtOffset(startPos + l);
TextPointer end = start.GetPositionAtOffset(wordToSearch.Length);
this.rtb.Selection.Select(start, end);
this.rtb.Selection.ApplyPropertyValue(
TextElement.BackgroundProperty,
this.rtb.SelectionBrush);
}
public int GetLinesCount(string str, int startPosition)
{
int lines = str.Take(startPosition).Count(k =>
k.Equals((char)13) || k.Equals((char)10));
return lines + 2;
}
But, for a formatted text (other color for example), I see a difference between my "word" and my "selection" of X characters.
If somebody has an idea?!?
Thank you for help me...
Bye.
|
|
|
|
|
|
|
i want to built a remote desktop tools or software that can connect and share the desktop.. the graphics quality should be good..
can anyone help me... providing the details of the project .
|
|
|
|
|
Sure I can. In the search box at the top, type in remote desktop c# and make sure the search in articles radiobutton is selected, and Bob's your transgender auntie.
|
|
|
|
|
Palantir - Remote Desktop Manager[^] is always a good article to consult when looking at writing your own desktop program.
Too much of heaven can bring you underground
Heaven can always turn around
Too much of heaven, our life is all hell bound
Heaven, the kill that makes no sound
|
|
|
|
|
I got a question about TDD and unit test using Visual Studio 2008 Pro. One of the TDD requirements reads something like "All tests should fail initially when there is no coding yet!" However, when I generate a test project from VS 2008 Pro, all tests can pass before any coding. Do I have to add code to the test project, in order to make the test fail initially? Or am I missing something here?
Best,
Jun
|
|
|
|
|
Of course you need to write code. The unit test is created with defaults; its up to you, the developer, to actually make it relevant.
No comment
|
|
|
|