Solved

Problem with Graphics.DrawString() method...

Posted on 2004-09-10
13
210 Views
Last Modified: 2008-02-01
Hi Experts,
   
         I am facing a problem with Graphics.DrawString() method.
My Requirment is to write numbers which are right aligned..
e.g.
Items      Price
Item1           34.00
Item2         551.00
Item3        5678.00
Item4            5.00
Item5       3000.00
Item6       4567.00

But I am not able to print it this way, I tried with most of the combination of StringFormat class
e.g.

StringFormat objSF = new StringFormat(StringFormatFlags.DirectionRightToLeft);
objSF.Alignment = StringAlignment.Far;  
                  
Can somebody tell me how to takle this problem.


Thanks
Bharat




0
Comment
Question by:bharat_mane
  • 7
  • 5
13 Comments
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12024299
Try this:

StringFormat objSF = new StringFormat();
objSF.Alignment = Alignment.Far;

This should be all.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12024302
Sorry, a typo:

StringFormat objSF = new StringFormat();
objSF.Alignment = StringAlignment.Far;
0
 

Author Comment

by:bharat_mane
ID: 12024330
Thanks TheAvenger,
  but i have already tried this.
  It is not coming the way i am looking for,.
o/p comes like this

                     2
                     5
          555.00  
       5555.00            
       5555.00            
            55.00
            55.00
            55.00


can you tell me why this must be happning?

Thanks
Bharat

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 20

Expert Comment

by:TheAvenger
ID: 12024343
Do you give the same right coordinate of the rectangle in the draw string?

I think that draw string is not the best function :-(( It really aligns things in its own way..... But for this I think it should work....
0
 

Author Comment

by:bharat_mane
ID: 12024368
Hi TheAvenger
I am specifying like this
x=50
for (i = 0; i <quantity.Length; i++)
{

      y += 20;
      g.DrawString(quantity[i],fntAddress,br1,x + 600,y,objSF);
      
}

or is there any other way i can work with is....?

Thanks
Bharat
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12024387
OK, here's the problem: you give a POINT and not a RECTANGLE. Make it like this:

x=50
int width = 100;
int height = 20;
for (i = 0; i <quantity.Length; i++)
{

     y += height;
     g.DrawString(quantity[i],fntAddress,br1, new RectangleF (x + 600, y, width, height), objSF);
}
0
 

Author Comment

by:bharat_mane
ID: 12025753
Thanks TheAvenger,
    Even this i had tried but no luck.
Still it is not coming, the way it should as right aligned. Here only difference is spacing is changing between the numbers.
I don't know why it is changing the spacing.

Is there any other way....?


Thanks
Bharat
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 50 total points
ID: 12025890
I made a small test with the following code:

SolidBrush textBrush = new SolidBrush (this.ForeColor)
string[] quantity = new string[] {"2", "5", "555.00", "5555.00", "5555.00", "55.00"};
int x=0;
int w = 100;
int h = 20;
int y = 0;
StringFormat objSF = new StringFormat();
objSF.Alignment = StringAlignment.Far;
for (int i = 0; i <quantity.Length; i++)
{

      y += h;
      e.Graphics.DrawString(quantity[i],this.Font,textBrush, new RectangleF (x, y, w, h), objSF);
}

I aligns the text perfectly on the right.
0
 

Author Comment

by:bharat_mane
ID: 12026161
Hi TheAvenger
thank you for your immediate replies.
Sorry to disturb you again and again.
But the problem is I am using third party component and it has derived class from graphics class. So I have to use that only.
By looking at your code I am bit confused from where are you referring this e.Graphics.DrawString()

Can you please tell me what is this e is it System.EventArgs e or something else.

Let me explain you clear scenario. I am trying to build PDF and for this I am using PDF n more components provided by www.rarefind.com this component has graphics class. If I am not wrong they are using most of the methods as it is.
And few of them must have been modified. I have already told them about this. They are working on it.

If this problem is related to graphics class then I apologies my mistake to take your precious time.

Thank you once again
Bharat

0
 

Author Comment

by:bharat_mane
ID: 12026198
If this problem is related to graphics class then I apologies my mistake to take your precious time.
By this line I mean   www.rarefind.com's  graphics class and not .NET's graphics class.

Thanks
Bharat


0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12026204
The code I posted is from the Paint event of the control and the argument e is of type System.Windows.Forms.PaintEventArgs. It has the current control Graphics object as a property, so I can draw in the control directly using it. So I am drawing on the screen.

Unfortunately I cannot tell you anything about this third party control. I don't know if they use the default Graphics class or have overriden all methods :-((
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 12026210
No problem for the time, that's why we participate in EE, to help each other find the problems and solutions for them.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question