|
That really depends on details of what the class is, whether it does anything useful and how important it is, so I can't really answer :p.
Sending the value that should be put in a 'control' from the business logic (as part of a FieldDefinition type data class) is fine, as long as what you're specifying is the underlying logical value. For example you shouldn't format up a number as a string, or specify font formatting information, because that's view specific.
A good test is: if you can imagine creating a non-graphical client for the method and the information it returns, it's fine to put it in there. You always need some interface code between layers and that's how you should think of this part.
|
|
|
|
|
You can pass a reference to that label to a method in the class library:
public void SetText(ref Label myLabel, string text)
{
myLabel.Text = text;
}
|
|
|
|
|
and you don't need the ref keyword to do that, better not use it here in fact.
A cleaner way is by passing a delegate, so it is the control's owner who updates the control when informed about a data change.
|
|
|
|
|
I agree with Luc - this is not a good method. The class library should not have or need any knowlege of any UI control. The normal way would be to get the text from the BusinessLayer vie a getter or string GetXXX() method and deal with updating the text in the UI layer.
Luc's suggestion of passing a delegate is an alternative aproach which is also valid and allows the calling of the update code from the BusinessLayer without it needing information about the control - the actual update is still done in the UI layer. Delegates often confuse people but they're pretty simple so here is an example. I prefer other options but here is how it is done:
namespace CommonObjects
{
public delegate void UpdateText(string text);
}
using CommonObjects;
namespace BusinessLayer
{
public class Strings
{
public static void RequestUpdateText(UpdateText updateText)
{
updateText.Invoke("New Text");
}
}
}
using System.Windows.Forms;
using CommonObjects;
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
RequestUpdateFromClassLibrary();
}
private void RequestUpdateFromClassLibrary()
{
BusinessLayer.Strings.RequestUpdateText(new UpdateText(UpdateTextBox));
}
private void UpdateTextBox(string text)
{
textBox.Text = text;
}
}
|
|
|
|
|
There are many ways to establish "dynamic connections" (access, ability to invoke method in, ability to set and get values) between different classes/objects. Injection, passing of references to instances of objects, Interfaces, inheritance, exposure of select values by use of Public Properties, etc.
Many of the responses here (all technically sound) assume, I think, that the UI here is a fixed design: I'm going to play "devil's advocate" and raise the question... since you told us in the OP that the UI definition is created by a method in the "Business Layer" reading from the DataBase:
"This creates the controls dynamically based on some fields in the database"."
Is it possible that the UI is, itself, not a fixed design, and that it may be different with each Application "run" ... because the DataBase fields that determine UI controls are being changed, or vary depending on other processes changing the DataBase.
If this (unlikely ?) hypothetical is false, why are you defining the UI in the DataBase ?
best, Bill
p.s. minor point: it would be a very strange scenario in which "create instance of a method" makes sense: I am pretty sure you mean that you access/invoke the method in the BL which then reads the DataBase and then one way, or another, creates the UI.
"It is the mark of an educated mind to be able to entertain a thought without accepting it." Aristotle
|
|
|
|
|
In Form2()
=========
In Form2 I have one Button and One richtextbox. And from the Button-1 I have created new form called "MyAppFrm" My requirement is while the mouse move away from MyAppFrm, Form2-RichTextBox - has to Fous(). But its not focusing...When I try to click on RichTextBox1, the control shows always in MyAppFrm. So How to achieve this...
private void Form2_Load(object sender, EventArgs e)
{
Form MyAppFrm = new Form();
MyAppFrm.MouseLeave += new EventHandler(this.MyAppFrm_MouseLeave);
}
private void button1_Click(object sender, EventArgs e)
{
MyAppFrm.Size = new System.Drawing.Size(775, 125);
MyAppFrm.StartPosition = FormStartPosition.CenterParent;
MyAppFrm.ShowDialog();
}
public void MyAppFrm_MouseLeave(object sender, System.EventArgs e)
{
richTextBox1.Text = sender.GetType().ToString() + ": MouseLeave";
richTextBox1.Focus();
}
Thanks
|
|
|
|
|
ShowDialog() is a modal operation. Did you mean to call Show() ?
/ravi
|
|
|
|
|
Have you subscribed to the MyAppForm_MouseLeave event in the some part of the code? If not you need to subscribe to it and then you can use a event handler to handle that event.
|
|
|
|
|
The ShowDialog is the problem. While the form is modal and displayed, focus cannot be returned to the parent form.
|
|
|
|
|
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".
|
|
|
|