|
I am trying to figure out how to set up a listview control so I can draw the items myself. Can anyone help me with this?
Thanks in advance for taking time to help me out!
dpb
Darryl Borden
Principal IT Analyst
darryl.borden@elpaso.com
|
|
|
|
|
|
I use this C# code to read a text file line-by-line, process each line and then write it back to disk. It works beautifully, if the file contains only the first 127 ASCII chars. The chars 128-255 are simply skipped. How can I fix this?
StreamReader sr = new StreamReader(m_sSrcFileName);
StreamWriter sw = new StreamWriter(m_sDestFileName);
try {
long savedBytes = 0;
do {
string oldline = sr.ReadLine();
string newline = ProcessLine(oldline);
savedBytes += oldline.Length - newline.Length;
sw.WriteLine(newline);
} while (sr.Peek() != -1);
Console.WriteLine(savedBytes + " Bytes saved");
return savedBytes;
}
Remember that the input files are not 2-bytes-unicode, but 1-byte-extended-ASCII.
Thank you in advance.
Luca Leonardo Scorcia
http://zip.to/kojak (only in Italian)
|
|
|
|
|
Have you tried System.Text.Encoding.ASCII ?
eg. StreamReader sr = new StreamReader(m_sSrcFileName, System.Text.Encoding.ASCII);
Paul
|
|
|
|
|
Yep I tried your suggestion and now it does not get skipped, however it writes back the wrong char to the file: for example the ù char becomes ?... I also tried to set the same encoding for the StreamWriter but with no success... do you have other suggestions?
Luca Leonardo Scorcia
http://zip.to/kojak (only in Italian)
|
|
|
|
|
Have you tried the encodings such as UTF8?
Give them a chance! Do it for the kittens, dear God, the kittens!
As seen on MS File Transfer: Please enter an integer between 1 and 2.
|
|
|
|
|
Luca Leonardo Scorcia wrote:
ù char becomes ?...
Ooops, sorry, saw "ASCII", didn't see "Extended".
Hmmm... supported codes:
Encoding.ASCII
Encoding.Unicode
Encoding.BigEndianUnicode
Encoding.UTF7
Encoding.UTF8
While the U in UTF is Unicode, I'd lay good money that they are 7 and 8 bit versions, so I'm guessing leppie is right to suggest UTF8.
Paul
|
|
|
|
|
Nope. I tried every encoding, apart from BigEndianUnicode... and it does not work. Now I don't have time enough to do a deep debug session, but I promise that tomorrow I'll try again...
Luca Leonardo Scorcia
http://zip.to/kojak (only in Italian)
|
|
|
|
|
Seems Encoding doesn't support Extended ASCII - now that is bizarre.
You might be well advised to open a standard stream, rather than a text stream, read the whole lot into a byte array and convert that to a string (possibly using System.Text.Encoding.GetChars, at worst using a for loop).
Paul
|
|
|
|
|
1) How do I cause a certain tab in my tab control to be automatically selected when a user does the follow:
a) Clicks an option in the Tree Control.
b) Clicks an option in from the Menu on the top toolbar.
Many Thanks,
Derek Smigelski
|
|
|
|
|
pls help this problem,
As you know, you can't delete minimize/maximize button in MDI child form
even if you set the FormBorder to None.
So I override resize method of childform.
But After that, I can't put button in groupbox of child form 'cuz
application lost focus when child form appears.
Here is very simple example,
Form1.cs<br />
-------------------------------<br />
using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
using System.Data;<br />
<br />
namespace SimpleTest<br />
{<br />
public class Form1 : System.Windows.Forms.Form<br />
{<br />
private System.Windows.Forms.MainMenu mainMenu1;<br />
private System.Windows.Forms.MenuItem menuItem1;<br />
private System.ComponentModel.Container components = null;<br />
<br />
public Form1()<br />
{<br />
InitializeComponent();<br />
}<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if (components != null)<br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
private void InitializeComponent()<br />
{<br />
this.mainMenu1 = new System.Windows.Forms.MainMenu();<br />
this.menuItem1 = new System.Windows.Forms.MenuItem();<br />
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {<br />
this.menuItem1});<br />
this.menuItem1.Index = 0;<br />
this.menuItem1.Text = "New Form";<br />
this.menuItem1.Click += new System.EventHandler(this.menuItem1_Click);<br />
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<br />
this.ClientSize = new System.Drawing.Size(292, 273);<br />
this.IsMdiContainer = true;<br />
this.Menu = this.mainMenu1;<br />
this.Name = "Form1";<br />
this.Text = "Form1";<br />
<br />
}<br />
<br />
static void Main()<br />
{<br />
Application.Run(new Form1());<br />
}<br />
<br />
private void menuItem1_Click(object sender, System.EventArgs e)<br />
{<br />
Form2 childForm;<br />
<br />
childForm = new Form2();<br />
childForm.MdiParent = this;<br />
childForm.Show();<br />
<br />
}<br />
}<br />
}<br />
<br />
<br />
Form2.cs<br />
--------------------------------<br />
using System;<br />
using System.Drawing;<br />
using System.Collections;<br />
using System.ComponentModel;<br />
using System.Windows.Forms;<br />
<br />
namespace SimpleTest<br />
{<br />
public class Form2 : System.Windows.Forms.Form<br />
{<br />
private System.Windows.Forms.GroupBox groupBox1;<br />
private System.Windows.Forms.Button button1;<br />
private System.ComponentModel.Container components = null;<br />
<br />
public Form2()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
protected override void Dispose( bool disposing )<br />
{<br />
if( disposing )<br />
{<br />
if(components != null)<br />
{<br />
components.Dispose();<br />
}<br />
}<br />
base.Dispose( disposing );<br />
}<br />
<br />
private void InitializeComponent()<br />
{<br />
this.groupBox1 = new System.Windows.Forms.GroupBox();<br />
this.button1 = new System.Windows.Forms.Button();<br />
this.groupBox1.SuspendLayout();<br />
this.SuspendLayout();<br />
<br />
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] {<br />
this.button1});<br />
this.groupBox1.Location = new System.Drawing.Point(64, 64);<br />
this.groupBox1.Name = "groupBox1";<br />
this.groupBox1.Size = new System.Drawing.Size(192, 128);<br />
this.groupBox1.TabIndex = 0;<br />
this.groupBox1.TabStop = false;<br />
this.groupBox1.Text = "groupBox1";<br />
<br />
this.button1.Location = new System.Drawing.Point(40, 40);<br />
this.button1.Name = "button1";<br />
this.button1.Size = new System.Drawing.Size(80, 40);<br />
this.button1.TabIndex = 0;<br />
this.button1.Text = "button1";<br />
<br />
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);<br />
this.ClientSize = new System.Drawing.Size(292, 273);<br />
this.ControlBox = false;<br />
this.Controls.AddRange(new System.Windows.Forms.Control[] {<br />
this.groupBox1});<br />
this.MaximizeBox = false;<br />
this.MinimizeBox = false;<br />
this.Name = "Form2";<br />
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;<br />
this.Resize += new System.EventHandler(this.Form2_Resize);<br />
this.groupBox1.ResumeLayout(false);<br />
this.ResumeLayout(false);<br />
<br />
}<br />
<br />
private void Form2_Resize(object sender, System.EventArgs e)<br />
{<br />
this.MaximizeBox = false;<br />
this.WindowState = FormWindowState.Maximized;<br />
}<br />
}<br />
}<br />
<br />
-------------------------------
Anyone has same problem with me?
I think this is another bug of MDI in .NET......
- Zaharang
|
|
|
|
|
Hello,
I write program which uses some functions from legacy dll written in Delphi.
The problem is that Delphi have type "shortstring" which is very specific for this language and I can't marshal this type.
Pascal code:
<br />
...<br />
type<br />
TDLLIn = record<br />
....<br />
Typekey: shortstring;<br />
end;<br />
<br />
<br />
function Init(dllIn: TDLLIn): integer;<br />
begin<br />
....<br />
end;<br />
C# code:
<br />
public struct TDLLIn<br />
{<br />
....<br />
<big>[MarshalAs(UnmanagedType.AnsiBStr)]</big><br />
public string Typekey;<br />
}<br />
<br />
public class Wrapper<br />
{<br />
....<br />
[DllImport("DelphiDLL.dll", CallingConvention=CallingConvention.StdCall, EntryPoint="Init")]<br />
static extern int Init(TDLLIn dllIn);<br />
}<br />
I've tried marshal as AnsiBStr, as byte array (MarshalAs(UnmanagedType.ByValArray, SizeConst = 256, ArraySubType = UnmanagedType.U1)) and many others ways but still no success.
Marshaling Pascal shortstring is impossible for me.
If anyone found a way to do this please inform me.
Best regards
Adam Plucinski
|
|
|
|
|
Tried [MarshalAs(UnmanagedType.LPStr)] ?
MSDN[^].
You may also implement a custom marshaler (ICustomMarshaler). That's an advanced topic, but standard marshaling is supposed to work for standard types only...
sometimes it helps to look at the IL generated code
a MS guy on develop.com "answering" .NET issues
|
|
|
|
|
[MarshalAs(UnmanagedType.LPStr)] is rather for "PChar" Delphi type
I've tried all possibilities - custom marshaler it is last possibility but I must resolve internal structure of shortstring, maybe Borland will help
|
|
|
|
|
If you can get a pointer on the actual structure of shortstring, you can hack it, that would avoid relying on Borland.
Btw, I am pretty sure lotsa people have or will have this problem too if shortstring is a lot used in applications.
Another possibility is to avoid direct use of a shortstring. For instance, if the DLL (or intermediate DLL) passes a char*, it could be the actual Delphi's dll job to convert this char* to shortstring. At this point, I don't know sh*t about Delphi.
sometimes it helps to look at the IL generated code
a MS guy on develop.com "answering" .NET issues
|
|
|
|
|
I'm sure too.
Many Delphi programmers use shortstrings because if they use normal strings, which size is dynamically counted they must use Borland Memory Manager dll which of course must be instaled in system. They don't use PChar but they could.
>Another possibility is to avoid direct use of a shortstring. For instance, if the DLL (or
>intermediate DLL) passes a char*, it could be the actual Delphi's dll job to convert this
>char* to shortstring
You're right but if you are not author of this Delphi DLL you can't do this.
I was Delphi developer - but I switched to VS .NET
|
|
|
|
|
Adam Plucinski wrote:
You're right but if you are not author of this Delphi DLL you can't do this.
Use an intermediate DLL.
VS.NET String -> marshalled as char * -> DLL -> DLL function exposes a char * as input parameter -> the implementation creates a shortstring thanks to the constructor (delphi dependency here) -> this DLL calls the initial DLL, passing a shortstring as input parameter -> you're done.
sometimes it helps to look at the IL generated code
a MS guy on develop.com "answering" .NET issues
|
|
|
|
|
Yes - with one dll it is no problem to do this.
..but academic problem still exists
when you have 10 dll's making 10 wrapper dll's is kinda boring
|
|
|
|
|
No, you need only one.
I am surprised that there is no article or existing workaround, if that's a real issue a lot of Delphi.NET developers have.
If that's a real issue, then don't miss the opportunity of writing a CP article. And get known!
It usually takes more than three weeks to prepare a good impromptu speech. Mark Twain
|
|
|
|
|
Does something look wrong with this?
ArrayList al = new ArrayList();
al.Add("Nick");
al.Add("Megan");
for(int i = 0; i < al.Count; i++)
Response.Write(al.Item[i].ToString());
I get this error, and I am looking at MSDN right now and there is an Item property.
<br />
forum.aspx.cs(54,20): error CS0117: 'System.Collections.ArrayList' does not<br />
contain a definition for 'Item'<br />
Nick Parker
The goal of Computer Science is to build something that will last at least until we've finished building it. - Unknown
|
|
|
|
|
Item[] is a special property, only [] is public in the end. So it is : al[i].ToString() .
sometimes it helps to look at the IL generated code
a MS guy on develop.com "answering" .NET issues
|
|
|
|
|
Actually, Item() is a VB.NET thing only. The equivalent in C# is the indexer which is what the [] operator means.
Norm Almond: I seen some GUI's in my life but WTF is this mess
Leppie: I made an app for my sister and she wouldnt use it till it was colorful enough
Norm:good point leppie, from that statement I can only deduce that this GUI must be aimed at children
Leppie:My sister is 25
-Norm on the MailMagic GUI
|
|
|
|
|
StephaneRodriguez wrote:
Item[] is a special property, only [] is public in the end.
First off thanks Stephane, however would you not agree with me that is misleading on MS's part?
Nick Parker
The goal of Computer Science is to build something that will last at least until we've finished building it. - Unknown
|
|
|
|
|
Nick Parker wrote:
First off thanks Stephane, however would you not agree with me that is misleading on MS's part?
It would be if Stephane were telling the truth
.Item() is the VB.NET implementation of a Collection indexer; [] is the C# implementation. If you consider how things were done in VB and C++ then it sort of makes a lot of sense.
It does become more confusing because a lot of collections in the .NET Framework are called "Items" AND Item is often the default property (another VB.NET concept), so you get used to seeing Items[], Items() and Item() but there's not much need for Item[].
... Or something like that
Paul
|
|
|
|
|
Okay, I think I've almost cracked this one. I got some more help by looking at Carlos Perez's explorer tree control which also uses some interfaces from Shell32. This is what I now have:
using System;
using Microsoft;
using Microsoft.Win32;
using System.Runtime.InteropServices;
namespace ShellTypeLib
{
// Component structure for IActiveDeskTop
[StructLayout(LayoutKind.Sequential)]
struct COMPONENT
{
uint dwSize;
uint dwID;
int iComponentType;
bool fChecked;
bool fDirty;
bool fNoScroll;
COMPPOS cpPos;
char[] wszFriendlyName;
char[] wszSource;
}
[StructLayout(LayoutKind.Sequential)]
struct COMPPOS
{
uint dwSize;
int iLeft;
int iTop;
uint dwWidth;
uint dwHeight;
int izIndex;
bool fCanResize;
bool fCanResizeX;
bool fCanResizeY;
int iPreferredLeftPercent;
int iPreferredTopPercent;
}
[StructLayout(LayoutKind.Sequential)]
struct COMPONENTSOPT
{
uint dwSize;
bool fEnableComponents;
bool fActiveDesktop;
}
[StructLayout(LayoutKind.Sequential)]
struct WALLPAPEROPT
{
uint dwSize;
uint dwStyle;
}
// Declare IActiveDesktop interface
[Guid("F490EB00-1240-11D1-9888-006097DEACF9")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IActiveDesktop
{
[PreserveSig]
uint ApplyChanges(uint dwFlags);
[PreserveSig]
uint GetWallpaper([MarshalAs(UnmanagedType.LPWStr)]out string pwszWallpaper, uint cchWallpaper, uint dwReserved);
[PreserveSig]
uint SetWallpaper([MarshalAs(UnmanagedType.LPWStr)] string pwszWallpaper, uint dwReserved);
[PreserveSig]
uint GetWallpaperOptions(ref WALLPAPEROPT pwpo,uint dwReserved);
[PreserveSig]
uint SetWallpaperOptions(ref WALLPAPEROPT pwpo, uint dwReserved);
[PreserveSig]
uint GetPattern([MarshalAs(UnmanagedType.LPWStr)] string pwszPattern, uint cchPattern, uint dwReserved);
[PreserveSig]
uint SetPattern([MarshalAs(UnmanagedType.LPWStr)] string pwszPattern, uint dwReserved);
[PreserveSig]
uint GetDesktopItemOptions(ref COMPONENTSOPT pco, uint dwReserved);
[PreserveSig]
uint SetDesktopItemOptions(ref COMPONENTSOPT pcomp, uint dwReserved);
[PreserveSig]
uint AddDesktopItem(ref COMPONENT pcomp, uint dwReserved);
[PreserveSig]
uint AddDesktopItemWithUI(IntPtr hwnd, ref COMPONENT pcomp, uint dwReserved);
[PreserveSig]
uint ModifyDesktopItem(ref COMPONENT pcomp, uint dwFlags);
[PreserveSig]
uint RemoveDesktopItem(ref COMPONENT pcomp, uint dwReserved);
[PreserveSig]
uint GetDesktopItemCount(out int lpiCount, [MarshalAs(UnmanagedType.U4)]uint dwReserved);
[PreserveSig]
uint GetDesktopItem(int nComponent, ref COMPONENT pcomp, uint dwReserved);
[PreserveSig]
uint GetDesktopItemByID(uint dwID, ref COMPONENT pcomp, uint dwReserved);
[PreserveSig]
uint GenerateDesktopItemHtml([MarshalAs(UnmanagedType.LPWStr)] string pwszFileName, ref COMPONENT pcomp, uint dwReserved);
[PreserveSig]
uint AddUrl(IntPtr hwnd, [MarshalAs(UnmanagedType.LPWStr)] string pszSource, ref COMPONENT pcomp, uint dwFlags);
[PreserveSig]
uint GetDesktopItemBySource([MarshalAs(UnmanagedType.LPWStr)] string pszSource, ref COMPONENT pcomp, uint dwReserved);
}
// Declare ActiveDesktop as a COM coclass
[ComImport, Guid("75048700-EF1F-11D0-9888-006097DEACF9")]
class ActiveDesktop
{
}
}
namespace Wallpaper_test
{
///
/// Summary description for Class1.
///
class Class1
{
///
/// The main entry point for the application.
///
[STAThread]
static void Main(string[] args)
{
System.Console.WriteLine("Windows Wallpaper Tester\n");
ShellTypeLib.ActiveDesktop MyDesktop = new ShellTypeLib.ActiveDesktop();
Type objType = typeof(ShellTypeLib.IActiveDesktop);
System.Console.WriteLine(objType.IsInstanceOfType(MyDesktop));
ShellTypeLib.IActiveDesktop IDesk = (ShellTypeLib.IActiveDesktop) MyDesktop;
uint hresult;
hresult = IDesk.SetWallpaper(@"C:\Documents and Settings\Wjousts\My Documents\My Pictures\Test.jpg",0);
Console.WriteLine("Result from SetWallpaper: " + hresult.ToString("X"));
uint flags = 4;
hresult = IDesk.ApplyChanges(flags);
Console.WriteLine("Result from ApplyChanges: " + hresult.ToString("X"));
int count = 10;
uint reserved = 0;
hresult = IDesk.GetDesktopItemCount(out count,reserved);
System.Console.WriteLine(count);
Console.WriteLine("Result from GetDesktopItemCount: " + hresult.ToString("X"));
}
}
}
So now all three of the IActiveDesktop methods that I call return 0, which is good. Also GetDesktopItemCount is now returning the correct value. SetWallpaper returns zero and if I look at my registry with regedit I can see it has changed the ConvertedWallpaper key, but, and this is a big but, the wallpaper hasn't actually changed! What's more I could have changed that key a lot easier with Registry class in .NET. So I still have the problem of how do I get windows to actually act on the updated registry and CHANGE THE FREAKING WALLPAPER!!!
|
|
|
|