1. Print Documents PrintDocument-- what am I doing wrong? Must be something stupid
I am running out of printing paper trying to debug this...it has to be
trivial, but I cannot figure it out--can you? Why am I not printing
text, but just the initial string "howdy"?
On the screen, when I open a file, the entire contents of the file is
in fact being shown...so why can't I print it later? All of this code
I am getting from a book (Chris Sells) and the net. The solution is
to be found in the fact that stringbuilder is not retaining
information outside the 'using' bracket, despite the fact I made it
'global'.
Keyword search //!!! below to see where I think the problem lies.
RL
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
namespace MyNameSpace1
{
public partial class MyForm : Form
{
string myPrintFilename;
StringBuilder myGlobalStringBuilder; //!!! this is supposed
to be global to the form MyForm, right?
string strModified; // = String.Copy(strOriginal); //not used
public MyForm()
{
InitializeComponent();
myGlobalStringBuilder = new StringBuilder("howdy"); //!!!
the only thing that gets printed is 'howdy'!
}
private void toolStripButton1_Click(object sender, EventArgs
e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open text file";
openFileDialog1.InitialDirectory = @"c:\";
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
files (*.*)|*.*";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) !=
null)
{
using (myStream)
{
StreamReader sr =
File.OpenText(openFileDialog1.FileName);
string s = sr.ReadLine();
StringBuilder sb = new StringBuilder();
while (s != null)
{
sb.Append(s);
s = sr.ReadLine();
myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
appending here?
}
sr.Close();
textBox1.Text = sb.ToString(); //this
works, to show the file text on the screen textBox1
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: could not read file from
disk (myStream); Err: " + ex.Message);
}
}
}
private void printToolStripButton_Click(object sender,
EventArgs e)
{
if (myPrintFilename != "")
{
this.printDocument1.DocumentName =
this.myPrintFilename;
this.printDocument1.Print();
}
}
private void printDocument1_PrintPage(object sender,
System.Drawing.Printing.PrintPageEventArgs e)
{
//p. 292 Chris Sells
//draw to the e.Graphics object that wraps the print
target
Graphics g = e.Graphics;
using (Font font = new Font("Lucida Console", 48) )
{
string mylocalstring;
mylocalstring =
myGlobalStringBuilder.ToString(); //!!! only prints "Howdy"--the
initial string--never the appended string from myGlobalStringBuilder--
why?
if (myGlobalStringBuilder.Length != 0)
{
g.DrawString(mylocalstring, font, Brushes.Blue, 0,
0);
}
}
}
}
}
2. Print Documents PrintDocument-- what am I doing wrong? Must be so - CSharp/C#
3. Print Documents PrintDocument-- what am I doing wrong? Must be so
Set a breakpoint in printDocument1_PrintPage, at the line where
"mylocalstring" is set. I believe you will see that the value is correct for
"myStringBuilder". The lack of carriage returns will hide the fact that the
string is correct when printed. I believe it is going off the page. The
print document functions won't fix newlines, wrapping, etc. Also, you need
to worry about the next page when the text is long. Printing is very
complicated in .net, until you get the hang of it...
"raylopez99" wrote:
> I am running out of printing paper trying to debug this...it has to be
> trivial, but I cannot figure it out--can you? Why am I not printing
> text, but just the initial string "howdy"?
>
> On the screen, when I open a file, the entire contents of the file is
> in fact being shown...so why can't I print it later? All of this code
> I am getting from a book (Chris Sells) and the net. The solution is
> to be found in the fact that stringbuilder is not retaining
> information outside the 'using' bracket, despite the fact I made it
> 'global'.
>
> Keyword search //!!! below to see where I think the problem lies.
>
> RL
>
> using System;
> using System.Collections.Generic;
> using System.ComponentModel;
> using System.Data;
> using System.Drawing;
> using System.Linq;
> using System.Text;
> using System.IO;
> using System.Windows.Forms;
> using System.Diagnostics;
>
>
> namespace MyNameSpace1
> {
> public partial class MyForm : Form
> {
> string myPrintFilename;
> StringBuilder myGlobalStringBuilder; //!!! this is supposed
> to be global to the form MyForm, right?
>
> string strModified; // = String.Copy(strOriginal); //not used
>
> public MyForm()
> {
> InitializeComponent();
> myGlobalStringBuilder = new StringBuilder("howdy"); //!!!
> the only thing that gets printed is 'howdy'!
>
> }
>
> private void toolStripButton1_Click(object sender, EventArgs
> e)
> {
> Stream myStream;
> OpenFileDialog openFileDialog1 = new OpenFileDialog();
> openFileDialog1.Title = "Open text file";
> openFileDialog1.InitialDirectory = @"c:\";
> openFileDialog1.Filter = "txt files (*.txt)|*.txt|All
> files (*.*)|*.*";
>
> if (openFileDialog1.ShowDialog() == DialogResult.OK)
> {
> try
> {
> if ((myStream = openFileDialog1.OpenFile()) !=
> null)
> {
> using (myStream)
> {
> StreamReader sr =
> File.OpenText(openFileDialog1.FileName);
> string s = sr.ReadLine();
> StringBuilder sb = new StringBuilder();
> while (s != null)
> {
> sb.Append(s);
> s = sr.ReadLine();
>
> myGlobalStringBuilder.Append(s); //!!! ??? Why is myGlobal not
> appending here?
> }
> sr.Close();
> textBox1.Text = sb.ToString(); //this
> works, to show the file text on the screen textBox1
>
> }
> }
> }
> catch (Exception ex)
> {
> MessageBox.Show("Error: could not read file from
> disk (myStream); Err: " + ex.Message);
> }
> }
> }
>
> private void printToolStripButton_Click(object sender,
> EventArgs e)
> {
> if (myPrintFilename != "")
> {
> this.printDocument1.DocumentName =
> this.myPrintFilename;
>
> this.printDocument1.Print();
> }
> }
>
> private void printDocument1_PrintPage(object sender,
> System.Drawing.Printing.PrintPageEventArgs e)
> {
> //p. 292 Chris Sells
> //draw to the e.Graphics object that wraps the print
> target
>
> Graphics g = e.Graphics;
> using (Font font = new Font("Lucida Console", 48) )
> {
> string mylocalstring;
>
> mylocalstring =
> myGlobalStringBuilder.ToString(); //!!! only prints "Howdy"--the
> initial string--never the appended string from myGlobalStringBuilder--
> why?
>
> if (myGlobalStringBuilder.Length != 0)
> {
> g.DrawString(mylocalstring, font, Brushes.Blue, 0,
> 0);
> }
> }
>
> }
>
>
> }
> }
>
>
>
>
4. slow exe, am I doing something wrong? - CSharp/C#
5. Am I doing something wrong? (Variables)
Hiya all,
I am developing a windows form application. I am coding in C#. What I have
is two forms/Classes, frmMain & frmAdd and they both have the same default
namespace "Q.A._Testing_Centre". In frmMain I have initialize & instantiated
my variable as "public string userName", where the user input name is
stored. I am trying to get the stored data propertied from my frmMain to my
frmAdd. I thought this is the correct syntex for outputting the info in my
textbox (scratching my head), maybe it's not. So please let me know if you
can help, or where I'm going wrong. Code is as follows:
namespace Q.A._Testing_Centre
{
public class frmMain : System.Windows.Forms.Form
{
public string userName; <<<---- I want the data from here
...
...
...
namespace Q.A._Testing_Centre
{
public class frmAdd : System.Windows.Forms.Form
{
private void SubMainMenu()
{
frmMain Main = new frmMain();
txtBoxSample.Text = Convert.ToString(Main.userName);<<<--- to output in my
textbox here
...
...
...
I have tried a sample textbox with in frmMain and this data is in fact
stored until I exit my program. But I cannot seem to reproduce the same
textbox output in frmAdd. I seem to get a blank textbox area. Alternatively
I have try "txtBoxSample.Text = Main.userName" syntex which does not work as
well.
Or maybe if someone has a better alternative, please feel free to let me
know.
All help is truly appreciated & thank you all in advance.
MikeY
6. Print Documents PrintDocument-- (PrintDocument is much harder than it looks) - CSharp/C#
7. What am I doing wrong with LINQ and XML
I have an xml fragment that looks like this assigned to doc variable:
<charge_details>
<charge>
<type>Maintenance Charge</type>
<currency>GBP</currency>
<percentage>0.35000</percentage>
<frequency>Monthly</frequency>
<term>31</term>
<lower_bound>500000.00</lower_bound>
<upper_bound>999999.99</upper_bound>
</charge>
<charge>
<type>Maintenance Charge</type>
<currency>GBP</currency>
<percentage>0.35000</percentage>
<frequency>Monthly</frequency>
<term>31</term>
<lower_bound>500000.00</lower_bound>
<upper_bound>999999.99</upper_bound>
</charge>
<charge>
<type>Quarterly Admin Charge</type>
<currency>GBP</currency>
<percentage>22.00000</percentage>
<frequency />
</charge>
<charge>
<type>Dealing Settlement Charge</type>
<currency>GBP</currency>
<charge_amount>22.00</charge_amount>
<frequency />
</charge>
</charge_details>
I have a LINQ query that looks like this:
IList<PolicyTerm> policyTerms = (from MMPPolicyTerms in
doc.Elements("charge_details").Elements("charge")
select new PolicyTerm
{
PolicyTermType =
(PolicyTermType)(StringEnum.Parse(typeof(PolicyTermType),
MMPPolicyTerms.Element("type").Value)),
Percentage = (double?)MMPPolicyTerms.Element("percentage") ??
0.00,
Currency = (string)MMPPolicyTerms.Element("currency") ??
string.Empty,
Amount = (double?)MMPPolicyTerms.Element("charge_amount") ??
0.00,
Term = (string)MMPPolicyTerms.Element("term") ?? string.Empty
}).ToList();
And I keep getting an exception being thrown...I'm assuming because some of
the nodes aren't always there?
I thought I would be able to get around it by using the ?? operator.
The class PolicyTerm is asimple one that looks like this:
public class PolicyTerm
{
public PolicyTermType PolicyTermType { get; set; }
public double? Percentage { get; set; }
public double? Amount { get; set; }
public string Currency { get; set; }
public string Term { get; set; }
}
Does anyone have any suggestions as to what I can do to avoid this? I know I
can choose not to use ToList but then I just get the error thrown when I
enumerate when I bind on the front end :(