Solved

Problem with Graphics.DrawString() method...

Posted on 2004-09-10
13
206 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
 
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now