|
|
I love one-liners. This was exactly what I was looking for.
Now to dissect it and toss the rest <evilsmiley>
Thanka you,
Michael Fritzius
|
|
|
|
|
Hi all:
I met a problem about communication between a Buisness layer and UI layer in a three-tier application.
In my project the UI layer invoke a method in Business layer named DoSomething(), DoSomething() method need to finish three tasks: (1)read data from database. (2)handle these data. (3)write them to excel file. However sometimes it need interact with the customer according to the data in step (2), so I should notify the UI layer to create related interation window. Here comes the question: how should I pass the message to UI layer?
One way to solve this problem is to use delegate, but if I use delegate I should know the method need to be invoked in UI layer first, that reqire Buisness layer import the UI layer dll, but you know that disobeys the import rule of a three-tier application.
What I need is the Business layer notify the UI layer: here we need the interaction with customer, just notify.
Can some one tell me the way to solve this question? Thanks.
|
|
|
|
|
Create an event in your Business Layer like AdditionalInformationRequested, which the BusinessLayer will invoke in step 2 if needed, and the UI can handle the event. You can have a custom EventArgs object that the UI will fill that will provide the information needed for the Business Layer to proceed with step 2.
Keep It Simple Stupid! (KISS)
|
|
|
|
|
|
Probably really simple, but I'm stuck and its been driving me nuts for the whole week.
The Problem.
Creating a dynamic number of Control.Image at runtime that holds various pictures load from a folder.
Each Image shares the same context menu
the problem is
when i click on one of the context Menus MenuItems say "enlarge" I have set it to all go the the same event handler
i.e = new system.eventhandler(enlarge_click)
how do i pass the argument of which image control I open the context menu from?
i.e
new system.eventhandler(enlarge_click uielement Image[x])
Thanks a lot
I looked into delegates but i must be be missing something, because i cant see how this would solve my problem.
thanks
|
|
|
|
|
If I am right, you have created some image objects. All of them have same clickevent handler and you want to identify which image was clicked. Right?
If I am right, then sender will have the information. Cast it to image type and get which image fired the event. You can use tag property to differentiate between the objects.
C isn't that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." - Martin Golding
|
|
|
|
|
the enlarge_click method will need to have a parameter and then you can pass the image control to that parameter.
It would normally have two parameters, (object sender, EventArgs e) in which case you would pass Image(x), EventArgs.Empty
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
This works for me, but I am just an electrical engineer . By the way what is a Control.Image? What is the namespace?
WPF, Sorry . Same concept just use the ContextMenuOpening event instead of the clickevent.
…
private string SelectedControl = "";
…
public MainForm()
{
InitializeComponent();
…
foreach (ImageControl tmp in this.ImageControlList)
{
tmp.Click += new EventHandler(ClickEvent);
}
…
}
…
void ClickEvent(object sender, EventArgs e)
{
Control tmpCtrl = (Control)sender;
SelectedControl = tmpCtrl.Name;
}
…
private void mnuEnlarge_Click(object sender, EventArgs e)
{
}
ARon
modified on Friday, December 12, 2008 2:10 PM
|
|
|
|
|
Cool that answer gets me closers cheers
here my code so far. just testing with a messagebox and image.tag = I etc
I have it working like the above, but every time I right click the image control. choose the the menu item from the context menu , it just keeps returning the last issued image.tag and not the from the image i have called it from?
do i have to create a seperate contextmenu for each image control, i thougt that would be bloating it?
Thanks again, i owe you guys a pint of tennants
System.Windows.Controls.ContextMenu ImageCM = new System.Windows.Controls.ContextMenu();
System.Windows.Controls.MenuItem ImageMN1 = new System.Windows.Controls.MenuItem();
ImageCM.Items.Add(ImageMN1);
ImageMN1.Header = "Bring to Front";
Random rand = new Random();
BitmapImage[] bmp = new BitmapImage[100];
int I = 0;
//new System.Windows.RoutedEventHandler(front_Click);
//foreach (string d in Directory.GetDirectories(sDir))
//{
foreach (string f in Directory.GetFiles(@MyDir, "*.jpg"))
{
I = I + 1;
bmp[I] = new BitmapImage(new Uri(f));
ImageX[I] = new Image();
ImageX[I].Source = bmp[I];
ImageX[I].Height = 200;
ImageX[I].Width = 200;
ImageX[I].Tag = I;
ImageX[I].ContextMenu = ImageCM;
dragCanvas.Children.Add(ImageX[I]);
ImageX[I].SetValue(Canvas.LeftProperty, RandomPost(dragCanvas.ActualWidth - 10.0));
ImageX[I].SetValue(Canvas.TopProperty, RandomPost(dragCanvas.ActualHeight - 100.0));
//listBox1.Items.Add(f);
}
ImageMN1.Click += delegate { front_Click(ImageX[I].Tag); };
}
catch (System.Exception excpt)
{
Console.WriteLine(excpt.Message);
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
FolderBrowserDialog FD = new FolderBrowserDialog();
FD.RootFolder = Environment.SpecialFolder.Desktop;
FD.ShowDialog();
textBox1.Text = FD.SelectedPath;
DirSearch(textBox1.Text);
}
private double RandomPost(Double Boundry)
{
return Rand.NextDouble() * Boundry;
}
private void button1_Click_1(object sender, RoutedEventArgs e)
{
//Controls.ImpImage Test = new Controls.ImpImage();
//dragCanvas.Children.Add(Test);
}
public void front_Click(object sender)
{
System.Windows.MessageBox.Show(Convert.ToString(sender));
}
}
}
|
|
|
|
|
I hoped I helpled.
Comments.
This is out side the foeach loop so you only adding once.
ImageMN1.Click += delegate { front_Click(ImageX[I].Tag); };
You need a common method that each Image can call. Check out the ContextMenuOpeningEvent. Subscribe to each Images’ ContextMenuOpeningEvent and then if one of your Image Controls raises the event the function could assign sender to the ImageMN1.tag.
I do WinForm not WPF so I not sure how you subscribe to events. If it was WinForms I could do this.
foreach (string f in Directory.GetFiles(@MyDir, "*.jpg"))
{
...
ImageX[i].OnContextMenuOpening += new EventHandlee(MyOnContextMenuOpening);
...
}
...
void MyOnContextMenuOpening(ContextMenuEventArgs e)
{
this.ImageMN1.Tag = e.sender;
}
ARon
|
|
|
|
|
Hi All,
I think I know the answer to this question but I'll throw it out anyway.
I need a custom error handler for my application which can display exception information and also write exceptions to a log file. Currently, I'm using a static class which works fine but, since I can't declare an instance member in a static class, I'm not able to specify the log folder except to pass it to the static method each time it's called or read it from a config file each time it's needed.
I'm coming from Delphi where, if you needed a public object, you could create a non-static object in the project file (.dpr) and it would be visible throughout the application without having to pass it to each new object created.
Is there a way in C# to create an object, assign properties to it and use it throughout your application without having to pass it to each newly created object?
Thanks,
Steve
|
|
|
|
|
Not shure if this is what you want but you can declare an object pulbic shared.
The object can then be accessed from anywhere and is always the same one.
|
|
|
|
|
shared is a vb keyword - the same as static in c#
DaveBTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)Visual Basic is not used by normal people so we're not covering it here. (Uncyclopedia)
|
|
|
|
|
Awch got my languages mixed up too much vb latly not enough c#
|
|
|
|
|
Create a static reference to the object. The reference is available through out the project.
|
|
|
|
|
I'm not sure what you mean. I'm currently using a static class but that doesn't do exactly what I need. Can you provide a snippet?
|
|
|
|
|
|
If I were you, since I will be using this in many apps, I would put it into a .dll of its own and then import it into any application needed. You can specify the folder by passing it to the static class's static method or property. I would use a property like so:
public string FilePath
{
set
{
// Check make sure filepath is valid and then set it
_filePath = value;
}
}
private static string _filePath;
You need to reference your .dll and you can add a using to every class so you don't have to qualify it.
|
|
|
|
|
One common way that I have seen to do this is with the Singleton pattern. It looks something like this:
public class Logger
{
//make the constructor private to keep other classes
//from creating an instance
private Logger() {}
private static Logger _instance = new Logger();
public static Logger Instance()
{
get { return _instance; }
}
//put instance properties and methods here
}
You can then access the one common instance of the Logger class all through the project as Logger.Instance
The other option would be to make all properties and methods static (like the Path class)
|
|
|
|
|
This is perfect! Thanks very much...
|
|
|
|
|
You could use a property. Or an Initialize method.
But I question whether or not you really want it to be static or singleton at all.
My logging class is just a regular class so the applications that use it can decide whether to have just one or several (most use only one, but one of them uses two).
|
|
|
|
|
hi ppl
i need to use sqrt(),
for this i tried to import math namespace
but i couldn't get this namespace,
but i get math namespace in vb.net..
how could i get math namespace in c#
self confidence+hard work=SUCCESS
|
|
|
|
|
Its not a namespace its a class. Use fully qualified name ie System.Math.
C isn't that hard: void (*(*f[])())() defines f as an array of unspecified size, of pointers to functions that return pointers to functions that return void
"Always program as if the person who will be maintaining your program is a violent psychopath that knows where you live." - Martin Golding
|
|
|
|
|
Check the Math class in System namespace.
More specifically Math.Sqrt() static method.
Regards,
Lev
|
|
|
|