Convert some VB6 to C#

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 5540 Views - Last Post: 15 October 2013 - 01:43 PM Rate Topic: -----

#1 km3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-May 09

Convert some VB6 to C#

Posted 25 May 2009 - 07:07 AM

Hi all

I am making a C# program and need some help with some VB6 code I have to convert to C#. In C# there is no Printer or CurrentX,CurrentY or TextWidth in C#. How do you do the same thing in C#? I have found that you can do something with e.DrawSring() but I am not sure how to do it.

Thanks

  Public Sub PrintAlignedTextC(ByRef PosX As Short, ByRef PosY As Short, ByRef s As String)
		
		Printer.CurrentX = PosX - (TextWidth(s) \ 2)
	
		Printer.CurrentY = PosY
		
		Printer.Print(s)
		End Sub																															 


Is This A Good Question/Topic? 0
  • +

Replies To: Convert some VB6 to C#

#2 frostyraver  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 13
  • View blog
  • Posts: 195
  • Joined: 21-April 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 07:39 AM

Take a look at this:
http://www.developer...t/vb-to-csharp/


public void PrintAlignedTextC(ref short PosX, ref short PosY, ref string s)
{
	
	 Console.CurrentX = PosX - (TextWidth(s) / 2);
	
	 Console.CurrentY = PosY;
	
	 Console.Print(s);
}





Not sure what that printer thing I thought it maybe Console.WriteLine, hmmm. Is your c# ina console app or winform?
Was This Post Helpful? 0
  • +
  • -

#3 beatles1692  Icon User is offline

  • D.I.C Head

Reputation: 13
  • View blog
  • Posts: 62
  • Joined: 03-December 08

Re: Convert some VB6 to C#

Posted 25 May 2009 - 07:42 AM

There's no printer object in .net framework (well as far as I know) but there's a PrintDocument object that you can use to print . You should handle its PrintPage event to do the printing job.This event gets you a Graphics object and you can use GDI+ functions to print using a printer.
Graphics has a DrawString method that let you print a string .
Here you can find an example in MSDN.
Was This Post Helpful? 0
  • +
  • -

#4 km3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-May 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 07:57 AM

View Postfrostyraver, on 25 May, 2009 - 06:39 AM, said:

Take a look at this:
http://www.developer...t/vb-to-csharp/


public void PrintAlignedTextC(ref short PosX, ref short PosY, ref string s)
{
	
	 Console.CurrentX = PosX - (TextWidth(s) / 2);
	
	 Console.CurrentY = PosY;
	
	 Console.Print(s);
}





Not sure what that printer thing I thought it maybe Console.WriteLine, hmmm. Is your c# ina console app or winform?

It is a Winform. I have two textboxes one to type in the left margin in millimeters and one to type in topmargin in millimeters. These two numbers are added to PosX and PosY when I call the function. I know how to print, that is working with printdocument and printpage. But I want make something equivalent with e.DrawString so I need to know how to do this in C#. And the convert tool is not working correctly.
Was This Post Helpful? 0
  • +
  • -

#5 km3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-May 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 08:57 AM

In my C# winform I have a panel where 64 textboxes are generated on at runtime. Then I have a combobox with two items, millimeter and inch. If I choose the first item millimeter there are two textboxes to type in the leftmargin and topmargin in millimeters of where on the paper to print out the textboxes. I want to get it working in C#. Can anyone help me with this problem.

   float LeftMgn = 0;
			float TopMgn = 0;

			if (txtRight.Text != "")
			{
				if (cboMess.SelectedIndex == 0)
				{
					LeftMgn = Convert.ToSingle((txtRight.Text).ToString());
				}
				else
					LeftMgn = System.Convert.ToSingle((txtRight.Text).ToString()) * 25.4;
			}
			else
				LeftMgn = 0;
			if (txtTop.Text != "")
			{
				if (cboMess.SelectedIndex == 0)
				{
					TopMgn = System.Convert.ToSingle((txtRight.Text).ToString());

				}
				else
					TopMgn = System.Convert.ToSingle((txtRight.Text).ToString()) * 25.4;

			}
			else
				TopMgn = 0;																																			






the above code is for typing in a leftmargin and topmargin in the textboxes to set position of the textboxes when printed to paper. And this code works. But I need some help with how to use MeasureString. Because Measurestring is the equivalent of TextWidth() in the VB6 code listed above.

 Public Sub PrintAlignedTextC(ByRef PosX As Short, ByRef PosY As Short, ByRef s As String)
		
		Printer.CurrentX = PosX - (TextWidth(s) \ 2)
		
		Printer.CurrentY = PosY
		
		Printer.Print(s)
	End Sub																																									  


So now I just need to have converted this vb6 function to e.DrawString.
My own suggestion is: e.DrawString(textbox[0].Text, font, Brushes.Black, 79 + LeftMgn, 90 + TopMgn);
Something like this but it is maybe not the wright way to do it. But how do I get MeasureString putted in the code sentence?
Was This Post Helpful? 0
  • +
  • -

#6 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 08:58 AM

*Edit*Nevermind*Edit*

This post has been edited by lesPaul456: 25 May 2009 - 08:59 AM

Was This Post Helpful? 0
  • +
  • -

#7 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 09:14 AM

View Postkm3, on 25 May, 2009 - 09:57 AM, said:

In my C# winform I have a panel where 64 textboxes are generated on at runtime. Then I have a combobox with two items, millimeter and inch. If I choose the first item millimeter there are two textboxes to type in the leftmargin and topmargin in millimeters of where on the paper to print out the textboxes. I want to get it working in C#. Can anyone help me with this problem.

   float LeftMgn = 0;
			float TopMgn = 0;

			if (txtRight.Text != "")
			{
				if (cboMess.SelectedIndex == 0)
				{
					LeftMgn = Convert.ToSingle((txtRight.Text).ToString());
				}
				else
					LeftMgn = System.Convert.ToSingle((txtRight.Text).ToString()) * 25.4;
			}
			else
				LeftMgn = 0;
			if (txtTop.Text != "")
			{
				if (cboMess.SelectedIndex == 0)
				{
					TopMgn = System.Convert.ToSingle((txtRight.Text).ToString());

				}
				else
					TopMgn = System.Convert.ToSingle((txtRight.Text).ToString()) * 25.4;

			}
			else
				TopMgn = 0;																																			






the above code is for typing in a leftmargin and topmargin in the textboxes to set position of the textboxes when printed to paper. And this code works. But I need some help with how to use MeasureString. Because Measurestring is the equivalent of TextWidth() in the VB6 code listed above.

 Public Sub PrintAlignedTextC(ByRef PosX As Short, ByRef PosY As Short, ByRef s As String)
		
		Printer.CurrentX = PosX - (TextWidth(s) \ 2)
		
		Printer.CurrentY = PosY
		
		Printer.Print(s)
	End Sub																																									  


So now I just need to have converted this vb6 function to e.DrawString.
My own suggestion is: e.DrawString(textbox[0].Text, font, Brushes.Black, 79 + LeftMgn, 90 + TopMgn);
Something like this but it is maybe not the wright way to do it. But how do I get MeasureString putted in the code sentence?


To get the width of the string using "e.Graphics.MeasureString()", you need the string, and the font. To add it into your code, just store the result in a float:

float textWidth = e.Graphics.MeasureString(textBox.Text, textBox.Font).Width;



Now just insert that into you code, wherever you want it.

Hope this helps!

This post has been edited by lesPaul456: 25 May 2009 - 09:16 AM

Was This Post Helpful? 0
  • +
  • -

#8 km3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-May 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 09:32 AM

Thanks that was just the code I needed. But I have 64 textboxes and when I make my e.DrawString sentence i have to make 64 of them because the positions of the textboxes change so I cannot call it in a loop. But do I have to make 64 float variables to store the measurestring. It would be fine to just write it once and just change the textbox everytime in a loop or so. Can that be done?
Was This Post Helpful? 0
  • +
  • -

#9 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 09:53 AM

View Postkm3, on 25 May, 2009 - 10:32 AM, said:

Thanks that was just the code I needed. But I have 64 textboxes and when I make my e.DrawString sentence i have to make 64 of them because the positions of the textboxes change so I cannot call it in a loop. But do I have to make 64 float variables to store the measurestring. It would be fine to just write it once and just change the textbox everytime in a loop or so. Can that be done?


You said the textBoxes are on a panel right? So, grap the textBoxes of the panel like this:

foreach (TextBox textBox in panel1.Controls.OfType<TextBox>())
{
	float textWidth = e.Graphics.MeasureString(textBox.text, textBox.Font).Width;
	// code to print
}



I haven't tested this code, so let me know about any problems.

This post has been edited by lesPaul456: 25 May 2009 - 10:29 AM

Was This Post Helpful? 0
  • +
  • -

#10 km3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-May 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 10:31 AM

The Compiler says that the OfType does not exist. What is the correct way to write it?
Was This Post Helpful? 0
  • +
  • -

#11 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 10:41 AM

View Postkm3, on 25 May, 2009 - 11:31 AM, said:

The Compiler says that the OfType does not exist. What is the correct way to write it?


It should be fine. How did you write your code?

Another way woud be to store the textBoxes in a list or an array, and then loop through them.
Was This Post Helpful? 0
  • +
  • -

#12 km3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-May 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 10:55 AM

My textboxes are generated from a array of textboxes. But when I wrote your foreach statement the compiler said that OfType is not C#. But how do I make the list?

 List<TextBox> lst = new List<TextBox>(); 


Do you add a namespace so you can write OfType after panel1.Controls? I get an errror.
Was This Post Helpful? 0
  • +
  • -

#13 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 11:08 AM

View Postkm3, on 25 May, 2009 - 11:55 AM, said:

My textboxes are generated from a array of textboxes. But when I wrote your foreach statement the compiler said that OfType is not C#. But how do I make the list?

 List<TextBox> lst = new List<TextBox>(); 


Do you add a namespace so you can write OfType after panel1.Controls? I get an errror.


Sorry, I forgot that you must add "using System.Linq;" to your namespaces.

If you have the textBoxes in a list, though, this is not necessary. Just loop through the list.
Was This Post Helpful? 0
  • +
  • -

#14 km3  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 26
  • Joined: 25-May 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 11:20 AM

I am developing on version vs 2005 so I cannot use System.Linq i think. But my textboxes are stored in a array of textboxes, not a list. they are stored in TextBox[] textbox = new TextBox[63]; Can I just loop through this array in the same way. Can you show me how I should do it.
Was This Post Helpful? 0
  • +
  • -

#15 lesPaul456  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 175
  • View blog
  • Posts: 729
  • Joined: 16-April 09

Re: Convert some VB6 to C#

Posted 25 May 2009 - 11:33 AM

View Postkm3, on 25 May, 2009 - 12:20 PM, said:

I am developing on version vs 2005 so I cannot use System.Linq i think. But my textboxes are stored in a array of textboxes, not a list. they are stored in TextBox[] textbox = new TextBox[63]; Can I just loop through this array in the same way. Can you show me how I should do it.


Yes you can loop through it the same way:

foreach (TextBox text in textbox)
{
	 float textWidth = e.Graphics.MeasureString(text.Text, text.Font).Width;
	// code to print
}



Hope this helps!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2