|
Hi all,
I've written a program where part of it will print a selected file to a printer. This file has tabs in certain places that are crucial to the output not looking wonky.
The output looks wonky. However, if I print it up using something like Notepad, it looks just fine. I'm using a generic filestream/printer handler to do the job. Is there a way for the tabs to be preserved?
Thanks for your time,
Michael Fritzius
OpenFileDialog fdlg = new OpenFileDialog();<br />
fdlg.Title = "C# Corner Open File Dialog";<br />
fdlg.InitialDirectory = @"C:\ ";<br />
fdlg.Filter = "Text files (*.txt | .txt | All files (*.*) | *.*";<br />
fdlg.FilterIndex = 2;<br />
fdlg.RestoreDirectory = true;<br />
if (fdlg.ShowDialog() == DialogResult.OK)<br />
{<br />
textBox1.Text = fdlg.FileName;<br />
}<br />
string filename = textBox1.Text.ToString();<br />
reader = new StreamReader(filename);<br />
verdana10Font = new Font("Arial", 10);<br />
PrintDocument pd = new PrintDocument();<br />
pd.PrintPage += new PrintPageEventHandler(this.PrintTextFileHandler);<br />
pd.DefaultPageSettings.Landscape = true;<br />
<br />
pd.Print();<br />
if (reader != null)<br />
{<br />
reader.Close();<br />
}
-- modified at 14:41 Friday 21st September, 2007
|
|
|
|
|
Well - part of your problem is that you are using the Arial font (which is proportional) and Notepad uses a fixed width font.
Deja View - the feeling that you've seen this post before.
|
|
|
|
|
Pete O`Hanlon wrote: Notepad uses a fixed width font
by default, but you can choose any available font ...
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips:
- make Visual display line numbers: Tools/Options/TextEditor/...
- show exceptions with ToString() to see all information
- before you ask a question here, search CodeProject, then Google
|
|
|
|
|
I'm not sure if that's going to help, because I'm not wanting to even use Notepad to print up these files. The problem is whenever the file is printed, the tabs that would normally keep things spaced out evenly are replaced with whitespaces.
The printer handler I'm getting ready to show basically takes each line from a file and turns it into something that the printer can understand. It does it line by line but makes no provision for tabs. Can you show me how to modify this?
Thanks for your time,
Michael Fritzius
private void PrintTextFileHandler(object sender, PrintPageEventArgs ppeArgs)<br />
{<br />
Graphics g = ppeArgs.Graphics;<br />
float linesPerPage = 0;<br />
float yPos = 0;<br />
int count = 0;<br />
float leftMargin = ppeArgs.MarginBounds.Left;<br />
float topMargin = ppeArgs.MarginBounds.Top;<br />
string line = null;<br />
linesPerPage = ppeArgs.MarginBounds.Height / verdana10Font.GetHeight(g);<br />
while (count < linesPerPage &&<br />
((line = reader.ReadLine()) != null))<br />
{<br />
yPos = topMargin + (count *<br />
verdana10Font.GetHeight(g));<br />
g.DrawString(line, verdana10Font, Brushes.Black,<br />
leftMargin, yPos, new StringFormat());<br />
count++;<br />
}<br />
if (line != null)<br />
{<br />
ppeArgs.HasMorePages = true;<br />
}<br />
else<br />
{<br />
ppeArgs.HasMorePages = false;<br />
}<br />
}
|
|
|
|
|
I have two applications where appA relies on appB. AppB, I do not have the ability to change the source code on appB. What I need to do is make sure appA terminates before appB when the user shuts down the computer. Is there anyway to make sure this happens?
thanks.
|
|
|
|
|
... hmm, lemme check something...
-- modified at 12:26 Friday 21st September, 2007
Okay, to make sure appA closes before appB the easy way. Just make sure that appA opens before appB. Windows closes processes in the order that they were opened. Well, it seems to anyway.
|
|
|
|
|
A shutdown message is broadcast to the top level window of all applications at the same time. It does not shut them down in the order that they were launched. Actually, if there was any order to it at all, it would be the reverse of the launch order. Last launched would get shutdown first because of dependancies.
|
|
|
|
|
Well i don't know, i just had 2 different aplications show a messagebox in the onClosing event. And whichever one i started first, showed its messagebox first. Unless... if both messages appeared center screen, then the one that started first would have closed last, leaving its message on top... Still, I must admit even if it where the case, it is still unreliable.
|
|
|
|
|
There's no way to do that. The message to shutdown goes out to all applications at once, but there is no way to tell, let alone guarantee, appB will wait for your appA to shutdown before it does.
|
|
|
|
|
You might find a solution in SystemEvents.SessionEnding. I haven't played with it, but it seems like that might work, at least for knowing that log off/shutdown is happening.
It was my understanding that Windows shuts apps down in no guaranteed order, though I'm sure someone will chime in if there is a guaranteed order. It might be as the other reply stated, but I wouldn't necessarily rely upon that.
I'd be interested in what you find out.
Good Luck!
It isn't enough to do well in life.
One must do good when and where one can.
Otherwise, what's the point?
|
|
|
|
|
I need to make this DLL work Hopefully someone can help me.
The errors i'm getting are:
"Cannot find Entry Point.. "(unless I explicitly define the entry point as being #1)
If I explicitly define the entry point, I get the error:
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Could it be because the function I'm calling calls other functions? If so, how do I fix that?
C++ DLL header..
<code>
extern "C" _declspec(dllexport) BOOL FindUser2(char *cpUserFile,
char *cpPassword,
char *cpUserName,
char *cpUserPriveledge,
char *cpUserID,
char *cpComments,
BOOL *bUserActive,
char *cpErrorReason);
</code>
C++ DLL implementation..
(BOOL is an int)
<code>
extern "C" BOOL FindUser2(char *cpUserFile,
char *cpPassword,
char *cpUserName,
char *cpUserPriveledge,
char *cpUserID,
char *cpComments,
BOOL *bUserActive,
char *cpErrorReason)
{
...
return TRUE;
}
</code>
C# definition
<code>
...
using System.Runtime.InteropServices;
namespace Material_OIT
{
public static class Authorize
{
[DllImport("WN_USER_SECURITY.dll", EntryPoint="#1")]
static public unsafe extern int FindUser2(ref char[] UserFile,
ref char[] Password,
out char[] UserName,
out char[] UserPrivilege,
out char[] userID,
out char[] Comments,
out int UserActive,
out char[] ErrorReason);
}
}
</code>
C# function call..
<code>
char[] UserFile = new char[256];
char[] Password = new char[256];
char[] UserName = new char[256];
char[] UserPriv = new char[256];
char[] UserID = new char[256];
char[] Comments = new char[256];
int UserActive = 0;
char[] ErrorReason = new char[256];
String userFile = @"C:\BSAH_WN\MATERIAL\CELL\FILES\CONFIG\USER_LIST.INF";
userFile.CopyTo(0, UserFile, 0, 51);
string password = "410572537";
password.CopyTo(0, Password, 0, 9);
Authorize.FindUser2(ref UserFile, ref Password, out UserName, out UserPriv, out UserID, out Comments, out UserActive,out ErrorReason);
</code>
-- modified at 13:46 Friday 21st September, 2007
|
|
|
|
|
What does the .dll's definition file, usually a .def file, say about the function?
"Any sort of work in VB6 is bound to provide several WTF moments." - Christian Graus
|
|
|
|
|
; WN_USER_SECURITY.def : Declares the module parameters for the DLL.
LIBRARY "WN_USER_SECURITY"
DESCRIPTION 'WN_USER_SECURITY Windows Dynamic Link Library'
EXPORTS
FindUser2 @1
|
|
|
|
|
Dio22 wrote: "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
I've run into this before and had to really play around with marshalling it. If I can find the code, I'll let you know how it was done.
"Try asking what you want to know, rather than asking a question whose answer you know." - Christian Graus
|
|
|
|
|
|
...if calling Dispose() results in an implicit call to Close(), why do people say that you shouldn't rely on Dispose to Close your connections? Take this code for example:
try
{
if (table != null)
{
using (OleDbConnection connection = new OleDbConnection())
{
connection.Open();
using (OleDbCommand command = connection.CreateCommand())
{
command.CommandText = "SELECT obj From " + table.Alias;
using (OleDbDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
container.Add(new Feature((xx)reader.Getxx(0)));
}
}
}
}
}
}
catch (Exception)
{
MessageBox.Show("There was a GPS Error");
}
What would have priority over a thrown exception such that it HAD to process before a catch was allowed to handle the Exception? Anything? Dispose?
-- modified at 10:54 Friday 21st September, 2007
|
|
|
|
|
It unwinds the stack. The stack is a standard mechanism in computing since the very early days.
So, in other words, it MUST tidy up everything created after the try before it can process the catch.
So the using statements will call Dispose before it gets to the catch block.
|
|
|
|
|
That's exactly what I thought. I was just trying to leave it as open a question as possible to promote other people to share their thoughts.
|
|
|
|
|
ForkOffandDie wrote: if calling Dispose() results in an implicit call to Close()
This can not be a blanket statement as it isn't always true. In this particular case, the Dispose method on OleDbConnection does actually call Close . There are other instances where this does not occur so you need to read the documentation carefuly or use a tool like Reflector to verify what the code actually does.
Keep in mind that your code is actually changed by the compiler so the using statements are replaced by try/finally blocks. It ends up looking similar to this:
try
{
if (table != null)
{
OleDbConnection connection;
try
{
connection = new OleDbConnection();
connection.Open();
OleDbCommand command;
try
{
command = connection.CreateCommand();
command.CommandText = "SELECT obj From " + table.Alias;
OleDbDataReader reader;
try
{
reader = command.ExecuteReader();
while (reader.Read())
{
container.Add(new Feature((xx)reader.Getxx(0)));
}
}
finally
{
if (reader != null)
{
((IDisposable)reader).Dispose();
}
}
}
finally
{
if (command != null)
{
((IDisposable)command).Dispose();
}
}
}
finally
{
if (connection != null)
{
((IDisposable)connection).Dispose();
}
}
}
}
catch (Exception)
{
MessageBox.Show("There was a GPS Error");
} When an exception occurs, the runtime begins a two-step process:
- The runtime searches the array for the first protected block that:
- Protects a region that includes the currently executing instruction, and
- Contains an exception handler or contains a filter that handles the exception.
- If a match occurs, the runtime creates an Exception object that describes the exception. The runtime then executes all finally or fault statements between the statement where the exception occurred and the statement handling the exception. Note that the order of exception handlers is important: the innermost exception handler is evaluated first. Also note that exception handlers can access the local variables and local memory of the routine that catches the exception, but any intermediate values at the time the exception is thrown are lost.
If no match occurs in the current method, the runtime searches each caller of the current method, and it continues this path all the way up the stack. If no caller has a match, the runtime allows the debugger to access the exception. If the debugger does not attach to the exception, the runtime raises the UnhandledException event. If there are no listeners for the UnhandledException event, the runtime dumps a stack trace and ends the program. (Exceptions Overview (MSDN)[^]
So, effectively what this says is that when an exception occurs, the runtime walks the stack backwards looking for a catch block that can handle that exception until it finds one or reaches to top of the stack and raises an UnhandledException. If it does find one, it then runs any finally blocks it found leading up to the catch block and then runs the catch block.
Sorry for the long-winded answer, but if I understand you correctly, it would seem that your line of reasoning is correct. The finally blocks would run, which would close the objects and then run the catch handler and display the message box.
ForkOffandDie wrote: What would have priority over a thrown exception such that it HAD to process before a catch was allowed to handle the Exception?
I'm not sure this really makes sense. Processing the catch block is handling the exception. The finally blocks really don't have anything to do with what exception was thrown as they will execute no matter what, even if no exceptions were thrown. The Framework does have a concept of Filtered exception handlers, but that isn't supported by C# (only VB and IL, as far as I know).
|
|
|
|
|
Pete O`Hanlon wrote: ms.Position = 0;
@ Pete : With your solution, it has an exception :
File format is not valid.

|
|
|
|
|
Hi guys,
please help!!! I am trying to delete the printtoprinter job that has already been sent to the printer and then reseting the printer. Is this possible? Is it possible to cancel printtoprinter and to capture a confirmation on whether the report was printed?
Please please please help!!!
Sameer
|
|
|
|
|
Perhaps you could use this[^] article as a starting point for your solution?
/ravi
|
|
|
|
|
Thank you Ravi, I really would hate to change my logic from a simple printtoprinter to a dll based process.
Sameer
|
|
|
|
|
hi
i have created this method to display graph on a form and is display but the problem that i have is that i want to save this method as an xml file but i can't get it right.under this method there is code that i am trying to use to save this method as an xml file.
private void DisplayTools()
{
Graph g = Graph("graph");
g.AddNode("Circle");
g.AddNode("Box");
g.AddNode("Ellipse");
g.AddNode("Diamond");
Edge e1 = (Edge)g.AddEdge("Circle", "Box");
Edge e2 = (Edge)g.AddEdge("Box", "Ellipse");
Edge e3 = (Edge)g.AddEdge("Ellipse", "Diamond");
Edge e4 = (Edge)g.AddEdge("Diamond", "Circle");
Node n1 = (Node)g.FindNode("Circle");
n1.Attr.Color = Microsoft.Glee.Drawing.Color.Black;
n1.Attr.Shape = Shape.Circle;
Node n2 = (Node)g.FindNode("Box");
n2.Attr.Color = Microsoft.Glee.Drawing.Color.Black;
n2.Attr.Shape = Shape.Box;
Node n3 = (Node)g.FindNode("Diamond");
n3.Attr.Color = Microsoft.Glee.Drawing.Color.Black;
n3.Attr.Shape = Shape.Diamond;
gViewer1.Graph = g;
here is code that i am trying to use to save as an xml file
FileStream fs = new FileStream("methods.xml", FileMode.Create);
XmlTextWriter w = new XmlTextWriter(fs,Encoding.UTF8);
w.WriteStartDocument();
w.WriteStartElement("methods");
DisplayTools();
w.WriteEndElement();
w.WriteEndDocument();
w.Flush();
fs.Close();
Mamphekgo
|
|
|
|
|
You need to decorate Graph and its contained classes with XML serialization attributes. See this[^] tutorial.
/ravi
|
|
|
|
|