|
Hi,
I write this code in the "CurrentCellChanged" event
private void dataGirdView1_CurrentCellChanged(object sender, EventArgs e)
{
txtName.Text = dataGirdView1[1, dataGirdView1.CurrentRow.Index].Value.ToString();
}
But this exception occur that when DataGirdView fiiling;
System.NullReferenceException: Object reference not set to an instance of an object
|
|
|
|
|
private void dataGirdView1_CurrentCellChanged(object sender, EventArgs e)
{
if(dataGirdView1.CurrentRow!=null)
txtName.Text = dataGirdView1[1,dataGirdView1.CurrentRow.Index].Value.ToString();
}
life is study!!!
|
|
|
|
|
I try dataGirdView1.CurrentRow.Index but its not work. It's big mistake.
Your code is working, thanks a lot...
|
|
|
|
|
my SendMessage project code:
private void button1_Click(object sender, EventArgs e)
{
IntPtr tohandle = FindWindow(null, "Sub Form");
Point pt = new Point(99, 100);
int iSize = Marshal.SizeOf(typeof(Point));
IntPtr lp = Marshal.AllocHGlobal(iSize);
Marshal.StructureToPtr(pt, lp,true );
if ((int)tohandle != 0)
{
SendMessage(tohandle, 789, IntPtr.Zero , lp);
}
}
my recieve msg project code :
protected override void DefWndProc(ref Message m)
{
if (m.Msg == 789)
{
Point pt = (Point)Marshal.PtrToStructure(m.LParam, typeof(Point));
string message = string.Format("{0},{1},{2},{3}",m.Msg ,m.WParam,pt.X ,pt.Y );
this.listBox1.Items.Add(message);
}
base.DefWndProc(ref m);
}
my issue:
m.Msg and m.WParam is ok ,but pt is error data, Why?
|
|
|
|
|
You did not explain much about what it is you want to achieve.
So I am guessing you want to send a user-defined message to a window
that does not belong to the same process ?
if so, it is a rather simple PInvoke exercise as long as no pointers need
to be passed.
When the message is to another process AND it needs a pointer to some data,
then it becomes trickym since that pointer now must make sense in the
destination process, which has a completely separate address space.
You can obtain a piece of memory and address space in some other
process by calling the Win32 function VirtualAllocEx.
So that is where you must start.
Then the caller must write into that special memory, using WriteProcessMemory
If the memory is used to return data, it must be read with ReadProcessMemory
And the last thing you should do, once neither process needs
the special memeory any more, is use VirtualFreeEx to release that memory again.
Here are some code snippets that are part of the solution:
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)]
private static extern IntPtr VirtualAllocEx(int hProcess, int address,
int size, uint allocationType, uint protection);
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)]
private static extern bool VirtualFreeEx(int hProcess, IntPtr address,
int size, uint freeType);
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)]
private static extern bool WriteProcessMemory(int hProcess,
IntPtr otherAddress, IntPtr localAddress, int size,
ref uint bytesWritten);
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)]
private static extern bool ReadProcessMemory(int hProcess,
IntPtr otherAddress, IntPtr localAddress, int size,
ref uint bytesRead);
[DllImport("kernel32.dll", CallingConvention=CallingConvention.StdCall)]
private static extern bool ReadProcessMemory(int hProcess,
IntPtr otherAddress, StringBuilder localAddress, int size,
ref uint bytesRead);
public void Read(object obj, int size, IntPtr ptr) {
uint bytesRead=0;
GCHandle handle=GCHandle.Alloc(obj, GCHandleType.Pinned);
IntPtr objPtr=handle.AddrOfPinnedObject();
if (!ReadProcessMemory(hProcess, ptr, objPtr, size, ref bytesRead)) {
int err=GetLastError();
env.error("Read failed; err="+err+" bytesRead="+bytesRead);
}
handle.Free();
}
public string ReadString(int size, IntPtr ptr) {
StringBuilder sb=new StringBuilder(size);
uint bytesRead=0;
if (!ReadProcessMemory(hProcess, ptr, sb, size, ref bytesRead))
env.error("Read failed; bytesRead="+bytesRead);
return sb.ToString();
}
public void Write(object obj, int size, IntPtr ptr) {
uint bytesWritten=0;
GCHandle handle=GCHandle.Alloc(obj, GCHandleType.Pinned);
IntPtr objPtr=handle.AddrOfPinnedObject();
if (!WriteProcessMemory(hProcess, ptr, objPtr, size, ref bytesWritten))
env.error("Write failed; bytesWritten="+bytesWritten);
handle.Free();
}
Dont mind the env.log and env.error, those are just log functions. You can substitute
your own if required. And you might want to throw an exception when an error occurs.
Hope this helps.
Luc Pattyn
|
|
|
|
|
On the other hand, if you can fit all the required data in lParam (32-bit), then you
make it so that you dont need pointers at all.
Some of the Window messages are known to contain a point in lParam, using the high and
low 16 bits for x and y (or is it y and x, dont recall).
Since I am not familiar with message 789 I can't tell if this is applicable to you.
If you defined it, and are responsible for the code in both processes,
I suggest the simple way described above !
Luc Pattyn
|
|
|
|
|
Hi, I am trying to drive Source Safe from my own front end.
I can't manage to create a new directory in VSS (Not a Directory on my PC)
I am using the Interop.SourceSafeTypeLib would another library be any better?
Thanks
Elephant
|
|
|
|
|
Have you tried VSSItem.NewSubproject ?
|
|
|
|
|
Hi all!
I use your help very urgent!
2 years ago a remote application was created.
The DataService (DataService.dll) contains a IRemoteFactory, which
allows the client to get access to the corresponding interface (here
the implementation):
public sealed class VRemoteFactory : MarshalByRefObject,
IRemoteFactory {
private DataObject m_DataObj = null;
public IDataObject iDataObject {
get {
if (null == m_DataObj) {
m_DataObj = new DataObject();
}
return m_DataObj;
}
}
The class DataObject (implemented as MarshalByRefObject in the
DataService.dll) implements the IDataObject interface, which provides a
property 'Version' and a function to get an object of the class Data
(implemented as sealed Serializable).
Now I have to extend the DataService.
As described in the .NET Remoting book of Ingo, I created a new
interface IExtendedDataObject which inherits from IDataObject. The
DataObject class now implements IExtendedDataObject, with the new
property that returns a class ExtData (which inherits from Data (which
is no longer declared as sealed)). I did not change anything at the
VRemoteFactory.
So every thing works fine as long as I use the new addapted client with
the new DataService (DataServer.exe). But if I try to run my new Client
againts the old DataService (DataServer.exe) I get a
InvalidCastException (Return argument has invalid type), but I do not
know why ??? I get this exception as soon as I call the
IRemoteFactory.iDataObject in the client, but why ???
In a demo project every thing works!!!
Please help me!
PS: I read something about namespace clashes if the old version is
compiled in .NET 1.0 and the new in 1.1. ????
Thanks
|
|
|
|
|
Hello!
I want to be able to change color of tabpages in a tabcontrol. Ive heard it´s got to do with drawmode, but I can´t figure out what to do. How do I achieve this?
|
|
|
|
|
Hi,
You can try to create a new class that inherits from TabPage. In this class you can make an override of the function OnPaint and use the PaintEventArgs parameter of this method to draw.
Then in your tabcontrol, instead of adding tabpages, you must add your new class.
|
|
|
|
|
Hi
You can just set the BackColor like so:
MyTabPage.BackColor = Color.Aquamarine;
HTH HN3
|
|
|
|
|
Set the TabControl's DrawMode to OwnerDraw, and then handle the DrawItem event to draw things yourself.
private void tagpage2_DrawItem(sender object, Eventarg e)
{
//here is place to write ur code using System.graphics
}
|
|
|
|
|
|
Hello,
System.Net.WebClient has method: DownloadDataAsync and event:DownloadDataCompleted
How does it internally work?
So...I assume when I call DownloadDataAsync it starts some private method StartDownload (for example) in a new thread, and from this StartDowload(which runs in the Thread) will be called DownloadDataCompleted(),
BUT they have made it so, that in attached to the event method, i can use all UI, thats mean...that DownloadDataCompleted is called in the main programm thread, how did they did that?
Thanks.
|
|
|
|
|
Delegates.
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Yes, but how? What should i invoke?
So I mean, to invoke a UI control from Thread I should Invoke this control...but I want to be able OnEvent use all controls I want, without Invoking, like it did in System.Net.WebClient...is it possible?
-- modified at 5:41 Wednesday 17th January, 2007
|
|
|
|
|
|
|
Hi all,
I have a little problem with one of my threads now that I switched over to the dotnet 2.0
I have a main form with a toolbar. One button on the toolbar opens a new form, an appointmentbook.
I have a Global boolean variable AppointmentBook to check if it is open or not, then I initialized the form I want to open with the thread during the load of my main form, named appointmentBook. It all worked in .net1.1 but now I get an error at the point where I say
...appointmentBook.WindowState = System.Windows.Forms.FormWindowState.Normal...
What this is suppose to do is if the form was just minimized, and the toolbar button was clicked, it will see that currently there is an open form and then just bring that form up again.
The error I receive is a cross threading error, and says that I can't access the form initialized in another thread.
So I have the following...
case "2":
//start thread to handel new appointment book
if(Brim.Library.GlobalVariables.AppointmentBook)
{
appointmentBook.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
else
{
StartThread();
}
private void StartThread()
{
ThreadStart appointThread = new ThreadStart(InitializeAppointmentBook);
Thread workerThread = new Thread(appointThread);
workerThread.Start();
}
private void InitializeAppointmentBook()
{
Brim.Library.GlobalVariables.AppointmentBook = true;
appointmentBook.ShowDialog();
}
Any help in how to solve this will be appreciated.
Thanks in advance
Last modified: 32mins after originally posted --
He who laughs last is a bit on the slow side
|
|
|
|
|
What's the error ? Global Variables always cause grief.
In .NET 2.0 there's an exception called if you access UI elements across thread. You can either turn this off ( checkforcrossthreadclientsomethingorother is the property name ), or you can fix your code, to not access UI elements between threads ( which is fraught with danger )
Christian Graus - C++ MVP
'Why don't we jump on a fad that hasn't already been widely discredited ?' - Dilbert
|
|
|
|
|
Does <code>StartThread</code> create the <code>appointmentBook</code> object? If not, who creates it? My guess would be that it is created by a different thread and when the UI thread attempts to access the <code>WindowState</code> property, a cross thread exception occurs.
You can solve this problem by creating the form on the UI thread (or whatever thread accesses the <code>WindowState</code>) property. To make a method execute on the UI thread, you can use Control.Invoke[^] or Control.BeginInvoke[^]. This[^] article will explain you the basics behind those methods and why they are needed.
|
|
|
|
|
Thanks for the reply. The main form is on the main thread. The main form contains the toolbar. The appointmentBook is created on the main form...frmAppointmentBook appointmentBook = new frmAppointmentBook, but the appointmentBook.ShowDialog() is only called within the sub thread. Then the call to the WindowState of the appointmentBook is done from the main thread.
Hope that makes sense
He who laughs last is a bit on the slow side
|
|
|
|
|
NeroToxic wrote: ut the appointmentBook.ShowDialog() is only called within the sub thread. Then the call to the WindowState of the appointmentBook is done from the main thread.
There lies your problem. If you're going to show the form on a different thread, create the form on that thread. When trying to acess the <code>WindowState</code> property from the main thread, use Invoke or BeginInvoke like this
void SetWindowStateForAppointmentBook()
{
if (appointmentBook.InvokeRequired)
{
this.Invoke(new MethodInvoker(SetWindowStateForAppointmentBook), null);
}
else
{
appointmentBook.WindowState = System.Windows.Forms.FormWindowState.Normal;
}
} Hope this helps.
|
|
|
|
|
Thanks, works 100%
He who laughs last is a bit on the slow side
|
|
|
|
|