|
Hi, yes that is exactly what I do. Thanks for the help guys.
|
|
|
|
|
Hi everybody!
Maybe there is somebody who can give me a bit of help; I am about to give up:
I need to ‘translate’ in C# the function GetOpenFileName to include a FileOpenDlg in a Dlg template containing other components, but I cannot handle in C# the reference to the Dialog template resource and the call back function.
Below is the code:
[ StructLayout( LayoutKind.Sequential, CharSet=CharSet.Auto )]
public class OpenFileName
{
public int structSize=0;
public IntPtr dlgOwner= IntPtr.Zero;
public IntPtr hInstance=IntPtr.Zero;
public String filter = null;
public String Customfilter= null;
public int maxCustFilter=0;
public int filterIndex=0;
public String file= null;
public int maxFile = 0;
public String fileTitle= null;
public int maxFileTitle= 0;
public String initialDir= null;
public String title = null;
public int flags=0;
public Int16 fileOffset=0;
public Int16 fileExtension=0;
public String defExt= null;
public IntPtr custData=IntPtr.Zero;
public IntPtr fnHook= IntPtr.Zero;
public OFNHookProc fnHook= IntPtr.Zero;
public String TemplateName;//=null;
}
[ DllImport( "Comdlg32.dll", CharSet=CharSet.Auto )]
public static extern bool GetOpenFileName([ In, Out ] OpenFileName ofn );
……..
…….
private void cmdOpenClick(object sender, System.EventArgs e)
{
OpenFileName ofn=new OpenFileName();
ofn.structSize=Marshal.SizeOf(ofn);
ofn.dlgOwner= this.Handle;
ofn.hInstance=IntPtr.Zero;
ofn.filter="Eseguibili (*.exe)\0*.exe\0Text Files (*.txt;*.doc;*.wri)\0*.txt;*.doc;*.wri\0Tutti (*.*)\0*.*\0\0";;
ofn.Customfilter = null;
ofn.maxCustFilter=0;
ofn.filterIndex=0;
ofn.file=new String(new char[_MaxPath]);
ofn.maxFile=ofn.file.Length;
ofn.fileTitle=new String(new char[_MaxFile]);
ofn.maxFileTitle=ofn.fileTitle.Length;
ofn.initialDir="C:\\";
ofn.title="Apri un file";
ofn.flags= OFN_EXPLORER|OFN_ENABLETEMPLATE|OFN_ENABLEHOOK;
ofn.fileOffset=0;
ofn.fileExtension=0;
ofn.defExt= null;
ofn.custData=IntPtr.Zero;
ofn.fnHook=??????????????????????????;
ofn.TemplateName=????????????????????;
bool risp=GetOpenFileName(ofn);
}
Any pretty good ideas? Many tanks in advance!!
mino
mino
|
|
|
|
|
Instead of using P/Invoke, you might read the documentation[^] for the OpenFileDialog component that encapsulates that API and struct. The same is true for the SaveFileDialog .
In .NET, you can set the fnHook to a delegate but you must declare the struct field as a delegate that matches the necessary signature:
struct OPENFILENAME
{
public OfnHookProc lpfnHook;
}
[return: MarshalAs(UnmanagedType.SysUInt)]
delegate IntPtr OfnHookProc(IntPtr hdlg,
[MarshalAs(UnmanagedType.U4)] int uiMsg, IntPtr wParam, IntPtr lParam); If you don't actually need to use a hook proc to handle your own dialog you don't need ot set this.
For the template name, it's quite a bit harder. The easiest way is to create a native DLL with the resource template - if you even need one, which is merely to extend the dialog itself, which is another reason for using a hook proc - and set the hInstance field to an IntPtr for the native DLL, which you can get using LoadLibrary or LoadLibraryEx . Set the lpTemplateName field to the name of the resource in that native DLL.
Again, though - unless you actually have to extend the UI for the default Open or Save file dialogs, just use the OpenFileDialog and SaveFileDialog components. That's why they're there and don't require your assembly to be granted a privilege to call unmanaged code.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello!
I've been programming in C# for about a month now with a book called Beginning Visual C#.NET (Wrox Press). So yesterday I got to the Events chapter and...
I really need your help!
In this book the author sais and explains that events are used with delegates.
So you kind of use both...
But I can't get it: why are they used with delegates? How do you put them together?
I can undestand events like this (used from a thread nearby...) with no problems:
private void AddButtons()
{
Button B1 = new Button();
B1.Text = "OK";
B1.Location = new System.Drawing.Point(8,8);
B1.Click += new System.EventHandler(buttonB1Click);
}
private void buttonB1Click(object sender, System.EventArgs e)
{
MessageBox.Show("Hello");
}
------
I can understand delegates like this (example from my book):
class Class1
{
delegate double processDelegate(double param1, double param2);
static double Multiply(double param1, double param2)
{
return param1 * param2;
}
static double Devide(double param1, double param2)
{
return param1 * param2;
}
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
processDelegate process;
Console.WriteLine("Enter 2 numbers separated with a comma:");
string input = Console.ReadLine();
int commaPos = input.IndexOf(',');
double param1 = Convert.ToDouble(input.Substring(0, commaPos));
double param2 = Convert.ToDouble(input.Substring(commaPos + 1,
input.Length - commaPos - 1));
Console.WriteLine("M for multiply, D for devide:");
input = Console.ReadLine();
if (input == "M")
process = new processDelegate(Multiply);
else
process = new processDelegate(Devide);
Console.WriteLine("Result {0}", process(param1, param2));
}
}
}
-----------------------
But I CANNOT undestand what do they have to do with each other? Why use them, when I can use events like in the example above? What do delegates do that helps with events?
Please, explain thoroughly in a non-article dummy-undestandble way, as I have read a lot of articles on it today, but still it seems that in all of them it's a given...
Please! Because I really need to move on, but I can't since I cannot undestand
the topic!
If you want, I can kindly 'reward' you with a Gmail account, no probs with that! But please help!
Thanks!
|
|
|
|
|
Here's a description of delegates and events:
Events and Delegates on MSDN[^]
Basically, by adding an event handler to an event you subscribe your listener to be informed when the particular event occurs.
Your class is informed by the event source by calling your delegate function when the event occurs.
For the event source class to be able to call your function it has to know what parameters an event handler function expects. That's why delegates have to be declared.
Think of delegates as type safe function pointers.
mav
|
|
|
|
|
Damn... maybe I am just hopelessly stupid today (and yesterday) but I still can't get the hang of it... Everything seems to be perfectly clear from your explanation, but somehing is still missing. Sorry And C# is my first language, so I don't know C++ with function pointers...
Thanks for the reply anyway. Need a Gmail account? Just send me a Email to gbelov@gmail.com and you'll have it!
Anyone else to help a dumb hopeless comedian calling himself a C# programmer?
|
|
|
|
|
Ok, lets see if I can explain myseflt:
Basically, an Event needs a delegate of the method its supposed to call when the event fires.
In your example:
private void AddButtons()
{
Button B1 = new Button();
B1.Text = "OK";
B1.Location = new System.Drawing.Point(8,8);
B1.Click += new System.EventHandler(buttonB1Click);
}
private void buttonB1Click(object sender, System.EventArgs e)
{
MessageBox.Show("Hello");
}
u are using a delegate although you might not be aware of it. The delegate type is:
public delegate void EventHandler(object sender, System.EventArgs e);
and the instance is pointing at buttonB1Click(object sender, System.EventArgs e)
if u dont see it too clearly, use intellisense and create a new instance of the delegate:
EventHandler handler=new EventHandler(.....);
Anyway, u will see this more clearly when u try to create ur own Events for your own classes.
|
|
|
|
|
Hi...
My code look like this...
private void AddButtons()
{
Button B1 = new Button();
B1.Text = "OK";
B1.Location = new System.Drawing.Point(8,8);
B1.Click += new System.EventHandler(buttonB1Click);
}
private void buttonB1Click(object sender, System.EventArgs e)
{
MessageBox.Show("Hello");
}
This works just fine..
But then i want to add some more information when the button is clicked. I want it to look something like this...
private void ButtonB1Click_New(object sender, System.EventArgs e, int something, bool whatever)
{
MessageBox.Show("Hello");
}
Then my question is...
How do I set ButtonB1Click_New to the .Click event?
Thanks
QzRz
QzRz
|
|
|
|
|
I think you have a misconception of what event handlers are.
An event handler is just a callback (look up 'delegate' on MSDN) that is called by the class that publishes the event.
Each event handler has a certain signature, usually containing the object calling the delegate and an additional object holding some additional information for the event.
The Click event of System.Windows.Forms.Button (or rather the matching event handler delegate System.EventHandler ) has such a signature. Because System.Windows.Forms.Button calls the delegate you give it, there's nothing you can do to modify the signature of the delegate there.
So you'd have to derive from Button and override the Click event not to require an event handler of type EventHandler but one of type MyNewEventHandler .
This delegate would be declared like this:
public delegate void MyNewEventHandler(object sender, System.EventArgs e, int something, bool whatever);
In your derived class you'll have to fire your new Click event by calling the event handlers attached to your new button class.
You see, it's not quite as easy as you might expect.
Are you sure your button can (or should) provide the additional information in the Click event? Are something and whatever really different with each click?
If not then you shouldn't change the event handler signature for something as basic as a click event.
Mav
|
|
|
|
|
An easier way is to keep the parameters something and whatever , for example, as fields in your class. In your current example, the handler is executed in the same thread and on the same instance so storing this data as fields on your class isn't a problem. This would be a much simpler way of doing it - and is very common through both the .NET BCL and many other applications - than defining your own event for a Button derivative.
Other than that, mav.northwind is right. A handler must use the same signature as the delegate that defines the event.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
i need to create a Byte array of lenght 300000...
i can't do that:
Byte[300000] myByte;
so how can i do?
|
|
|
|
|
Byte[] bytes = new Byte[300000];
|
|
|
|
|
|
How do I get the current logged in user name of a remote nt machine?
I know there is a way in delphi, but how to master it in C#, may be with an API call?!
Looking forward to some answers.
|
|
|
|
|
Do you mean the user that's logged into a remote machine, or a remote user logged into your machine? How did you do it in Delphi? If you used a native API then you can do the same thing in C#. Read about Consuming Unmanaged DLL Functions[^] in the .NET Framework SDK, and see http://pinvoke.net[^] for many common method signatures.
You can also use WMI, for which classes are defined in the System.Management namespace. Read Accessing Management Information with System.Management[^] also in the .NET Framework SDK.
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hi all!
I'm supposed to create an MMC snap-in in C# for XP/Win2000.
As far as I've seen, there is no support for this in C#. And most of the time people recommend the framewok of IronRingSoftware as a great starting point.
Now I wonder, if I'm adding a reference to Microsoft Management Console 2.0 in the Visual Studio IDE (Add Reference -> COM tab -> Microsoft Management Console 2.0), what am I referencing? By doing this I can write MMC20. in the code window, and a list of interfaces and methods appear.
Is this reference useful for creating snap-ins?
If this is a way of building snap-ins, what is the difference between the MMC reference and the framework from IronRingSoftware?
Thanx in advance
|
|
|
|
|
esjq wrote:
Is this reference useful for creating snap-ins?
It's necessary. MMC is a COM client, and snap-ins are COM servers. .NET can communicate with COM using Runtime Callable Wrappers (RCWs) to consume COM components, and with COM-Callable Wrappers (CCWs) to expose managed component to COM. You're actually doing both. You must implement those interfaces defined in that interop assembly you created, and register your assembly with regasm.exe.
There are rules to exposing your managed components to COM. NEver use auto-generated interfaces, and in this case you wouldn't have to because it's the interfaces defined in that interop assembly that you're implementing. This means, on your classes, you set ClassInterfaceAttribute to ClassInterfaceType.None . Always attribute your classes and interfaces - if you define any - with a unique GuidAttribute that you do not change. Never change published interfaces. These are all rules for writing COM components and must be followed when exposing managed components to COM, otherwise your COM clients (like MMC) will never find your components (because the CLSID, or class ID - a GUID for your classes keeps changing) or because the interfaces IDs (IIDs) keep changing. Read Design Considerations for Interoperability[^] in the .NET Framework SDK, as well as Exposing .NET Components to COM[^] for more information about this topic.
Code Project has some good articles about developing MMC snap-ins, including how to do it in managed code. Creating MMC Snapin using C# (Part I)[^] is one such example. You can search for others.
You should also read the MMC Programmer's Guide[^], which takes you through developing a snap-in (using native COM, but the information is still very much applicable since you'll be implementing the native interfaces in your managed code using the interop assembly).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Thank you! Really appreciate the information and the links.
|
|
|
|
|
this is my code...why it doesn't update changes to my datasource???
void MenuItem3Click(object sender, System.EventArgs e)
{
MyConn = new OdbcConnection(myConnectionString);
string SQL = "SELECT * FROM novice ORDER BY id_novice DESC";
OdbcDataAdapter myCommand = new OdbcDataAdapter(SQL,MyConn);
myCommand.SelectCommand = new OdbcCommand(SQL, MyConn);
DataSet ds = new DataSet();
myCommand.Fill(ds, "novice");
OdbcCommandBuilder custCB = new OdbcCommandBuilder(myCommand);
BindingManagerBase cm = BindingContext[dataGrid1.DataSource] ;
DataRowView drv = (DataRowView )cm.Current ;
((DataRow )drv.Row).Delete () ;
myCommand.Update(ds, "novice");
ds.AcceptChanges();
}
________________________
live and die by the code
|
|
|
|
|
IMO, to call an OdbcDataAdapter instance myCommand is confusing. You should - following suggested naming guidelines in the .NET Framework sDK - use myAdapter at the very least. Something even more descriptive than that, using "adapter" somewhere, would even be better.
Also, while not the problem, calling ds.AcceptChanges() is not necessary. The Update method does this for you upon successful completion of updating the data store (i.e., no exceptions are thrown).
You should try debugging the code to make sure that the OdbcCommandBuilder constructed suitable SQL statements for each of OdbcDataAdapter 's properties besides the SelectCommand which you've already defined (and is required for the command builder).
Are you sure that the DataSet is full and that all the data is in the "novice" DataTable ? At times you have to set up OdbcDataAdapter.TableMappings if the table from which you're selecting doesn't result in the table name you expect. This is a problem with SQL Server, which names it's tables (result sets) "Table", "Table1", "Table2", and so on.
I also assume that the BindingContext code is elsewhere, perhaps as part of a Button.Click handler? Make sure that however you bind your data, you use the same exact approach to get the BindingContext . To eliminate any problems, it's recommended that you use the following:
CurrencyManager cm = (CurrencyManager)dataGrid1.BindingContext[
dataGrid1.DataSource, dataGrid1.DataMember</code>]; This ensures that if you set only DataSource to a DataTable , or set the DataSource to the DataSet and the DataMember to the table name (which is recommended for data navigation or to support multiple table views if binding multiple tables throughout the lifetime of that particular DataGrid ), that either works. The binding context must be exactly the same as how you bound your data, or it will not work.
Finally, debug your application or add some debugging code to make sure that the DataSet actually includes changes (which could be a result of not binding correctly, as I stated above). You can do this easy by using DataSet changes = ds.GetChanges();</pre>That will filter out all the unchanged data so you don't have to sift through so much, since examining <code>DataSet s in the variables windows is difficult (Whidbey, i.e. Visual Studio 2005, will improve this with debugging viewers for various types of objects, which is also extensible for your own objects).
This posting is provided "AS IS" with no warranties, and confers no rights.
Software Design Engineer
Developer Division Sustained Engineering
Microsoft
[My Articles] [My Blog]
|
|
|
|
|
Hello!
I have a problem, and nobody seems to know the remedy...
There are a lot of programmes capable of editing (doing various things) custom file formats, for example game editors.
For instance, there is a fifaFS console application which gives the oppurtunity of editing .big graphic archives of FIFA 2005 from EA Sports. You can pack, unpack files, rebuild etc.
So how did the creator manage to write such software? I mean, there is nothing in Delphi capable of editing .big files (it's written in Deplhi, but I am a C# programmer so I want to know how to do it in C#)... So how?!
I heard that you need to know the structure of the file... Ok, so for example I have the file specification of BMP file format.
But how do I do stuff with it? Please explain! And please give some piece of code... I really need your help!
Please don't tell me that .big is a commercial file format owned by EA, so there's no chance of editing it. I know at least 20 programmes capable of editing .big file.
Thanks!
Regards,
Lars
|
|
|
|
|
I dont htink there is anything magical with this file format. I think its just something where several files are packed into a single file. At the beginning of those files one can clearly the the contents.
You could just dig through one sample file and try to findout yourself how exactly it works. You could also contact someone from one of those 20 programs and ask him if he has any infos conecrning the exact format.
So it doesnt have to do anything with the programming language uyou use, but with the knwoledge you have of the format. If you know it a simple stream is enough to unpack the contents.
|
|
|
|
|
Yeah, well, thanks, but I am kind of a begginer...
So could anyone please give an example code for the available BMP file format, just to demonstrate the technique? Please!
The BMP specification is here: http://www.dcs.ed.ac.uk/home/mxr/gfx/2d/BMP.txt
Or just type 'File formats' in Google, then check out the first site. Choose whichever file you like, and please help me!
Thanks!
|
|
|
|
|
It won't help much if someone shows you how to read file format A (for example, BMP) if you want to modify file format B.
When you get down to it, each file is just a bunch of bytes you can read or write (check BinaryReader or BinaryWriter in the FCL, for example).
But unless you get the specification _what_ the bytes at a certain offset mean, you're out of luck.
You might be able to scan for certain byte patterns, but usually when you try to modify a file without knowing the exact structure you'll end up with a corrupt file.
Once you have the structure you can write classes that read or write the correct files, but not any sooner.
mav
|
|
|
|
|
Ok, thanks! But you didn't get it: I don't want to edit those .big files, I just stated them as an example. I just want to understand how does one edit some file format. And I wanted some simple code about working with those bytes...
|
|
|
|
|