|
we'll do
Thank you
|
|
|
|
|
OK, I've thought about this a little more and experimented. Another (maybe preferable) alternative is to create a singleton. Give the singleton an event that the main form can subscribe to, and a method that your other classes can call which will in turn raise the event. Much better than my previous suggestion and more maintainable.
I'll shut up now
using System;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Singleton.Instance.SingletonEvent += new EventHandler(Instance_SingletonEvent);
Form2 frm2 = new Form2();
frm2.Show();
}
void Instance_SingletonEvent(object sender, EventArgs e)
{
Console.WriteLine("Singleton Event");
}
}
using System;
using System.Windows.Forms;
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Singleton.Instance.SingletonMethod();
}
}
using System;
public class Singleton
{
public event EventHandler SingletonEvent;
static readonly Singleton instance = new Singleton();
static Singleton() { }
Singleton() { }
public static Singleton Instance
{
get { return instance; }
}
public void SingletonMethod()
{
OnSingletonEvent(new EventArgs());
}
protected virtual void OnSingletonEvent(EventArgs e)
{
EventHandler eh = SingletonEvent;
if (eh != null)
eh(this, e);
}
}
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)
|
|
|
|
|
I'll try it
Thanks again
but I have to admit Mika Wendelius answer is the easiest and better
|
|
|
|
|
You're welcome.
Mika's answer is basically the same, except he's making the main form a singleton instead of a seperate object that communicates with the main form.
His way is easiest if the main form is always going to be needed (which it normally is of course). I think a seperate 'Notifier' singleton class that raises events that the main form (or anything else for that matter which is a big plus) can subscribe to may, in the long term, have advantages - and is maybe a little more OO. Just like Mika's solution, it can have properties and methods that will be accessisble to all objects in your application.
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)
|
|
|
|
|
As I mentioned in my post, I didn't like the idea that the logic is scattered throughout the application and the combining element is a form (main form or any other). So I fully agree with you that it would be benefitial to have separate controller class and the form is used only for viewing.
So basically both main form and the controller may be singletons, although I think there would be a point if controller is even static, if possible. Do you see any downsides on that?
And also you're definitely right when saying that it would be more OO
Mika
|
|
|
|
|
your both right, I changed all of the the modifiers in the main form to private, it was public, and I Invoked all of the texts through a delegate without touching the form control, this is better than accessing the components directly, even if we say lets try this you can't access the component unless you change the modifiers to public, internal or protected internal.
by the way, don't think that the program should divided into separate parts (classes) you call each part with a new object and when you done just close it ? beside it is for less memory consumption and faster for the program to run .
if you see a better way for ding this, let me know
in any case Thank you all
|
|
|
|
|
You have already got enough replies. Here is my thoughts.
You need to update a control(textbox or button) on a form from a separate class, right? As others suggested, delegates is good. But for your problem, I think the most obvious method is to hide the control update behind a well defined interface and pass that interface to your class. Following code shows what I meant.
public interface IMainForm
{
void UpdateTextBoxText(string text);
....
}
public class MainForm : Form, IMainForm
{
public void UpdateTextBoxText(string text){
}
void button_click(object sender,EventArgs args)
{
ASeparateClass a = new ASeparateClass(this);
}
}
public class ASeparateClass
{
IMainForm form;
public ASeparateClass(IMainForm form){
this.form = form;
}
} Delegate approach also will work well.
|
|
|
|
|
I want to design my programs so that they can accept plugins that may be installed later. How should I design my program and how should plugins structure be? Should I use reflection?
Thanks,
Artmansoft
|
|
|
|
|
like this
example coder:
[System.Runtime.InteropServices.DllImport("user32.dll", EntryPoint = "GetWindowLong")]<br />
public static extern Int32 GetWindowLong(Int32 hWnd, Int32 nIndex);
"user32.dll" is the filename whitch you want to plugin.
"GetWindowLong" is a method in this file.
|
|
|
|
|
This code is used when we know the name of the plug-in in advance. What if we are not aware of future plugin's name?
|
|
|
|
|
|
|
|
You're right, thanks Greeeg!
|
|
|
|
|
As Greeeg said, you should search CP first. There are many articles.
But I would do it like this:
1. Design a main interface that will allow your app to interact with plugins and viceversa.
2. Add a way for the plugins to be registered with your app (register them using the UI, drop the DLLs in a special folder)
3. Create an attribute (in the .NET way) and use it to mark the main class (which implements the interface from step 1) in the plugin
4. In your app, load all DLLs registered in step 2 and enumerate its types. If any of the types has the attribute from step 3, create an object from it and call the plugin's methods (maybe one to initialize, another to get menu commands, and so on).
I hope this helps,
|
|
|
|
|
It is great! Thanks Luis.
|
|
|
|
|
Hi all,
I am developing an windows applcation using C# .net 2003. I have a list view in my application.I am loading my list view with two columns from the database.
Now in the list view if I press 'A' the appropriate item in the list view starts with 'A' gets highlighted.This highlighting is being done based on the first column but I want it to happen based on the second column.
Its urgent.Please help me out
With Regards,
B.Ananthvivek
|
|
|
|
|
Hi all,
I have written this code in the list view keypress and its now selecting based on the second column but the selection based on the first column also works and it results in multi select i need to supress it. Can any one help me out
for(int i= 0; i < listview1.Items.Count; i++)
{
if(listview1.Items[i].SubItems[1].Text.StartsWith(e.KeyChar.ToString().ToUpper()))
{
RemoveBackColor(listview1); /* Removing back color of previous selecion */
listview1.Items[i].Selected = true;
listview1.Items[i].Focused = true;
listview1.Items[i].BackColor = SystemColors.Highlight;
listview1.Items[i].ForeColor = SystemColors.HighlightText;
break;
}
}
With Regards
B.Ananthvivek
|
|
|
|
|
So the problem is the multiselect?
Have you set: ListView.MultiSelect Property [^] Also before setting the Selected property to true, you can use Clear method on SelectedItems.
|
|
|
|
|
Do you mean that only the first column is selected (highlighted) for a row? You can't make it select the second column, but you can select the whole row by setting FullRowSelect to true .
|
|
|
|
|
I have written this code in the list view keypress and its now selecting based on the second column
private void listview1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
for(int i= 0; i < listview1.Items.Count; i++)
{
if(listview1.Items[i].SubItems[1].Text.StartsWith(e.KeyChar.ToString().ToUpper()))
{
RemoveBackColor(listview1); /* Removing back color of previous selecion */
listview1.Items[i].Selected = true;
listview1.Items[i].Focused = true;
listview1.Items[i].EnsureVisible();
listview1.Items[i].BackColor = SystemColors.Highlight;
listview1.Items[i].ForeColor = SystemColors.HighlightText;
break;
}
}
e.handled = true;
}
/*Remove Back Color*/
private void RemoveBackColor(ListView LlistView)
{
int lintSelIndex = LlistView.SelectedItems.Count;
for(int lintCountIndex =0;lintCountIndex<lintSelIndex;)
{
try
{
LlistView.SelectedItems[lintCountIndex].BackColor = Color.White;
LlistView.SelectedItems[lintCountIndex].ForeColor = Color.Black;
LlistView.SelectedItems[lintCountIndex].Selected = false;
}
catch(Exception lobjException)
{
return;
}
}
}
its works and suppressed the selection based on the first column at all. But the problem now is that When I moved to the new item (based on the typing in the listview) and press Shift and Down key together,all the items from the first item are getting highlighted.
May be dotnet still thinks that the control is in the first item(so it selects from the first item).
how to overcome this one?
|
|
|
|
|
Hello,
What I would do would be to set FullRowSelect to true (so that all the columns are highlighted when an item is selected) and to just select the item (Selected property set to true) when its second column starts with the key pressed:
private void listview1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
for(int i= 0; i < listview1.Items.Count; i++)
{
if(listview1.Items[i].SubItems[1].Text.StartsWith(e.KeyChar.ToString().ToUpper()))
{
listview1.Items[i].Selected = true;
listview1.Items[i].Focused = true;
listview1.Items[i].EnsureVisible();
break;
}
}
e.handled = true;
} One thing you haven't considered is that if an item that starts with L is selected, and you press L again, the next item would be selected, and so on. Your code, like above, will always select the first item that starts with L.
It is not hard to do that. You just have to start at the item after the currently selected one, and wrap around to the first when you get to the end.
|
|
|
|
|
Hi all!
Im using VS 2008 C# . I was make a site E commercer(CC). Now , I have a new site diffrent from other company. My work, I need intergrate 2 site. , Now i need a webservice for check user from that site. If user login from my site . They don't must login on that site.
Thank you
Hello
|
|
|
|
|
Where these databases located? are there on same host?
do you use aspnet authentication builtin of .NET?
if you use aspnet authentication that microsoft implement it on .net, and you can access second site's database in first site,all you must do is that give the address of your sqlprovider to another databases and there is no need to web service.
Human knowledge belongs to the world
|
|
|
|
|
No 2 Database not in same host. a DB is SQL 2000. a DB Oracle 10G.
Hello
|
|
|
|