|
Sorry, I'll be more descriptive.
When a windows forms application is run in the command prompt, you hit enter and you instantly see another standard command prompt line because the application has opened in the background (or at least it has been opened and told the command prompt that it's not needed any more).
What I want is for my application to not instantly ignore the command prompt but to do a few things and output a few lines to it before going off and being a windows forms application.
I think that's still worded badly, but hopefully you can figure out what I mean.
|
|
|
|
|
There are two parts - the application and the form that application displays.
Normally all the applicaiton does is display the form but you can do ther work before that.
Does this make it clearer?
|
|
|
|
|
Mmm... slightly. How do I do work before the form is displayed? I put my lines of code right at the beginning of the main() method and that doesn't work. Unless there's somewhere else I should be adding the code?
|
|
|
|
|
In an application i am working on there is program.cs with the following code:
namespace StreamTest
{
static class Program
{
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
As you can see the Application.Run method is what triggers the UI so put your code before that. You can change the form definition to pass parameters if you want, I have done that before.
|
|
|
|
|
WubbleU wrote: I put my lines of code right at the beginning of the main() method and that doesn't work.
Yes, but what exactly are you trying to do and what is the result?
Your program should look something like
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new RotaForm());
}
You should be able to perform any other functionality before the three lines above to achieve the result you are after. However, it is somewhat difficult to understand exactly what you are trying to do, maybe if you post some of your code it will become clearer.
It's time for a new signature.
|
|
|
|
|
As far as I can tell, you want the project to be a Console project.
Perform your "before" stuff.
(Maybe wait for the user to press RETURN.)
Then call FreeConsole to remove the console window.
Then Run the Form.
[
System.Runtime.InteropServices.DllImportAttribute
(
"Kernel32"
,
SetLastError=true
,
EntryPoint="FreeConsole"
)
]
private static extern bool
API_FreeConsole() ;
/**
<summary>
Uses pinvoke to call the Windows FreeConsole method
</summary>
<returns>
The result of FreeConsole
</returns>
*/
public static bool
FreeConsole
(
)
{
return ( API_FreeConsole() ) ;
}
|
|
|
|
|
Hi, that's exactly what I needed! Thanks!
The problem I have now is that the form isn't closing when I hit the X button. Any ideas?
Thanks again!
|
|
|
|
|
Should I be running the form differently? Instead of Application.Run(..) ?
|
|
|
|
|
Hi,
On using data type StringBuilder, I find that sufficient instances of data type string is also getting
allocated!
I saw this information when I used VSTS profiler to understand memory allocation.
Isn't StringBuilder and string totally different data types or is it that
internally stringbuilder is nothing but of the type string?
My code is very straightforward
StringBuilder str = new StringBuilder;
for (int i =0; i < 10; i++)
{
str.Append("Net");
}
Regards,
NetQuestions
modified on Sunday, June 27, 2010 4:41 AM
|
|
|
|
|
StringBuilder is a totally diferrent type to string . It is there for efficiency purposes when building a string up from multiple parts (as per your loop). You need to decide when it is efficient to perform your string concatenation directly or through a StringBuilder . Many people suggest always using a StringBuilder , but I disagree. See this excellent article, by John Skeet, who really knows what he is doing http://www.yoda.arachsys.com/csharp/stringbuilder.html[^]
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter.
Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
|
|
|
|
|
A StringBuilder is a mutable version of a string: when you keep appending, you are exhausting the space allocated for the StringBuilder, so it allocates another, bigger one. This happens repeatedly. Try allocating sufficient space to start with:
StringBuilder str = new StringBuilder(40);
The default is 16 bytes, so you will go over that quite soon!
Did you know:
That by counting the rings on a tree trunk, you can tell how many other trees it has slept with.
|
|
|
|
|
StringBuilder is different, but it is also a "build-in" type, not just a List<char> or something like that.
See here for in-depth information: Strings UNDOCUMENTED[^]
|
|
|
|
|
That article is wrong, there's nothing magic about StringBuilder. It just uses unsafe code to write to the built-in string data type. Your code could do that, too!
|
|
|
|
|
Interesting, I just assumed that the article would be right..
Now I looked with the Reflector of course and it appears that you are right instead. It just calls String.AppendInPlace which just uses some unsafe code.
|
|
|
|
|
"You can't observe a system without affecting it." -- Heisenberg's Uncertainty Principle
|
|
|
|
|
i need to print data with dot matrix printer(tvs msp 250 champion),and printer is connected with usb port not LPT port .i tried bellow code but
it is not working.Please suggest me better way .
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (DialogResult.OK == ofd.ShowDialog(this))
{
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
RawPrinterHelper.SendFileToPrinter(pd.PrinterSettings.PrinterName, ofd.FileName);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string s = "Hello";
PrintDialog pd = new PrintDialog();
pd.PrinterSettings = new PrinterSettings();
if (DialogResult.OK == pd.ShowDialog(this))
{
RawPrinterHelper.SendStringToPrinter(pd.PrinterSettings.PrinterName, s);
}
}
public class RawPrinterHelper
{
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)] public string pDocName;
[MarshalAs(UnmanagedType.LPStr)] public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)] public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint="OpenPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);
[DllImport("winspool.Drv", EntryPoint="ClosePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="StartDocPrinterA", SetLastError=true, CharSet=CharSet.Ansi, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool StartDocPrinter( IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);
[DllImport("winspool.Drv", EntryPoint="EndDocPrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="StartPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="EndPagePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);
[DllImport("winspool.Drv", EntryPoint="WritePrinter", SetLastError=true, ExactSpelling=true, CallingConvention=CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten );
public static bool SendBytesToPrinter( string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false;
di.pDocName = "My C#.NET RAW Document";
di.pDataType = "RAW";
if( OpenPrinter( szPrinterName.Normalize(), out hPrinter, IntPtr.Zero ) )
{
if( StartDocPrinter(hPrinter, 1, di) )
{
if( StartPagePrinter(hPrinter) )
{
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
if( bSuccess == false )
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}
public static bool SendFileToPrinter( string szPrinterName, string szFileName )
{
FileStream fs = new FileStream(szFileName, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
Byte []bytes = new Byte[fs.Length];
bool bSuccess = false;
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;
nLength = Convert.ToInt32(fs.Length);
bytes = br.ReadBytes( nLength );
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}
public static bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
dwCount = szString.Length;
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}
Thanks & Regards,
Vishnu.
|
|
|
|
|
|
In a VS 2010 application I've been instructed to set a breakpoint and do a Quick Watch.
Can anyone tell me what a Quick Watch is and how you do it?
Thanks
Darrall
|
|
|
|
|
It's a way to watch a variable. When you hit the breakpoint, click Debug>QuickWatch... (or press Ctrl+D, Q ) and the QuickWatch dialog will appear which displays the value of the variable at that point.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Thanks Pete
|
|
|
|
|
You're welcome.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
If you google for 'quick watch vs tutorial' youls find lots of people who can tell you this and more.
Regards
David R
---------------------------------------------------------------
"Every program eventually becomes rococo, and then rubble." - Alan Perlis
The only valid measurement of code quality: WTFs/minute.
|
|
|
|
|
|
hi
i am using to form in my application each form have a datagridview i want to transfer all the rows of one datagridview to other form datagridview how?
|
|
|
|
|
You can bound both the datagridview with the same datasource.
- Praveen
|
|
|
|
|