?
Solved

Problem with Graphics.DrawString() method...

Posted on 2004-09-10
13
Medium Priority
?
213 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 200 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 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