Solved

Problem with Graphics.DrawString() method...

Posted on 2004-09-10
13
211 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

Title # Comments Views Activity
ASP.NET(C#) Eliminating weekends from a date range 2 54
LINQ Select all records by MAX data syntax help 1 28
Name Space error VS2015 1 37
"The resource cannot be found" 1 24
Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

749 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