现在,我正在尝试在PrintDocument上构建我的表单,但是我看到页面上实际显示内容的唯一方法是打印一张纸.它有效,但我需要添加很多东西,而且我宁愿不浪费大量的纸张.现在我有一个打印对话框,但没有打印预览按钮.有没有办法让我出现?谢谢!
public partial class Form4 : System.Windows.Forms.Form
{
private System.ComponentModel.Container components;
private System.Windows.Forms.Button printButton;
public Form4()
{
// The Windows Forms Designer requires the following call.
InitializeComponent();
}
// The Click event is raised when the user clicks the Print button.
private void printButton_Click(object sender, EventArgs e)
{
PrintDocument pd = new PrintDocument();
pd.PrintPage += new PrintPageEventHandler(this.pd_PrintPage);
PrintDialog pdi = new PrintDialog();
pdi.Document = pd;
if (pdi.ShowDialog() == DialogResult.OK)
{
pd.Print();
}
}
// The PrintPage event is raised for …Run Code Online (Sandbox Code Playgroud) 我现在正在打印收据,但我无法弄清楚如何在图形模式下正确对齐文本.我尝试了几种不同的东西,但它们要么效率低下,要么在我的情况下不起作用.有没有办法可以轻松地将文字对齐到右边?这是我现在的代码.
using (Font printFont = new Font("Courier New", 9.0f))
{
e.Graphics.DrawString("Subtotal:", printFont, Brushes.Black, leftMargin + 80, HeightToPrint, new StringFormat());
e.Graphics.DrawString(subtotal.ToString(), printFont, Brushes.Black, leftMargin + 150, HeightToPrint, new StringFormat());
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试获取此字符串数组中的元素数量,但它不会让我离开1 Count.
string [] Quantitys = Program.RecieptList[i].ItemQuantitys.Split(new char[] {'*'});
for (int ii = 0; i <= Quantitys.Count - 1; ii++)
{
}
Run Code Online (Sandbox Code Playgroud)
我收到一条错误消息说明
运算符' - '不能应用于'Method Group'和'Int'类型的操作数.
什么是正确的方法来做到这一点?
在我的程序中,如果它太长,我需要将一个字符串拆分成多行.现在我正在使用这种方法:
private List<string> SliceString(string stringtocut)
{
List<string> parts = new List<string>();
int i = 0;
do
{
parts.Add(stringtocut.Substring(i, System.Math.Min(18, stringtocut.Substring(i).Length)));
i += 18;
} while (i < stringtocut.Length);
return parts;
}
Run Code Online (Sandbox Code Playgroud)
唯一的问题是,如果第19个字符不是空格,我们会将一个字缩小一半,看起来非常糟糕.
例如
字符串:这是一个超过18个字母的长信号.
Sliced string:
This is a long sent
ance with more than
18 letters.
Run Code Online (Sandbox Code Playgroud)
我如何剪切字符串,使其每个部分不超过18个字符,但如果可以的话,请回到最近的空格?我已经习惯了上面的算法,但我似乎无法得到它.
谢谢!
当用户按下文本框中的输入时,我正试图让某些事情发生,并且它可以工作,但是当我这样做时,它会产生一个非常烦人的DING窗口错误声音.我已经查找了我的问题,并且显然e.SuppressKeyPress = true;在这些内容之前添加了内容,然后e.Handled = true;我的程序仍在发出声音.这是我正在使用的代码:
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
if (e.KeyCode == Keys.Enter)
{
// A bunch of stuff goes here that I want to
// happen when the user hits enter
}
e.Handled = true;
}
Run Code Online (Sandbox Code Playgroud)
难道我做错了什么?这是其他人说你必须要做的事情,但由于某些原因它并不适合我...
谢谢!
我经常对此感到困惑.我一直被教导用经常使用变量或常量命名的数字,但如果它降低了程序的效率,我还应该这样做吗?下面是一个例子:
private int CenterText(Font font, PrintPageEventArgs e, string text)
{
int recieptCenter = 125;
int stringLength = Convert.ToInt32(e.Graphics.MeasureString(text, font));
return recieptCenter - stringLength / 2;
}
Run Code Online (Sandbox Code Playgroud)
上面的代码使用的是命名变量,但运行速度比这段代码慢:
private int CenterText(Font font, PrintPageEventArgs e, string text)
{
return 125 - Convert.ToInt32(e.Graphics.MeasureString(text, font) / 2);
}
Run Code Online (Sandbox Code Playgroud)
在这个例子中,执行时间的差异是最小的,但是在更大的代码块中呢?
目前,我正在VS Express 2013中创建一个项目,但我注意到我的构建不再包含任何新内容。我的意思是说,它会一次又一次地构建相同的旧项目,而实际上并没有在构建中添加任何内容。当我尝试关闭并重新打开VS时,所有更改仍在设计器和.cs文件中,但构建仍未提供任何更新。我正在使用默认设置和调试构建设置进行构建。
我也尝试过这里提到的内容:Visual Studio继续运行旧版本
但是构建复选框已被选中,因此这不是问题。
有什么线索吗?
谢谢!
我正在尝试使用这些方法在屏幕的矩形区域中查找颜色。但是有时屏幕上有数百万个像素,而我现在仅每秒实现约35次getColor迭代。我的代码中一定有某些东西导致它运行非常缓慢。
我怎么能比这更快地扫描屏幕?理想情况下,我想在不到一秒钟的时间内扫描整个屏幕以获取一种颜色,而不是现在的8个小时:P
这是我的两种方法。
public static int getColor(int x, int y){
try{
return(robot.getPixelColor(x, y).getRGB() * -1);
}catch(Exception e){
System.out.println("getColor ERROR");
return 0;
}
}
//returns first instance of color,
//Begins top left, works way down to bottom right
public static Point findColor(Box searchArea, int color){
System.out.println("Test");
if(searchArea.x1 > searchArea.x2){
int temp = searchArea.x1;
searchArea.x1 = searchArea.x2;
searchArea.x2 = temp;
}
if(searchArea.y1 > searchArea.y2){
int temp = searchArea.y1;
searchArea.y1 = searchArea.y2;
searchArea.y2 = temp;
}
for(int i = searchArea.x1;i <=searchArea.x2; i++){
for(int …Run Code Online (Sandbox Code Playgroud) 我如何处理混淆文本,因此读取文本文件的用户无法读取,但我的程序仍然可以读取它?基本上,我会True*True*False*True*False*False*False*True*False*true*在文本文件中有类似的东西,我需要它看起来很疯狂.
我知道如何从文件中获取文本并写入文件和所有内容,我只需要弄清楚如何对字符串进行模糊处理并对其进行去混淆.这可能没有进入所有疯狂的加密东西吗?我认为AES和其他加密方法都是矫枉过正,因为在我的程序中,这些信息并不是绝密或其他什么,它可以在程序中查看.我只是不希望它直接通过文件编辑.
谢谢一堆:D
弥敦道
怎么会
w.WriteLine(Program.RegisterList[i].DateTime);
Run Code Online (Sandbox Code Playgroud)
写道: 11/20/2013 01:46:31 PM
但
w.WriteLine(Convert.ToDateTime(Program.RegisterList[i].DateTime, CultureInfo.InvariantCulture).ToString());
Run Code Online (Sandbox Code Playgroud)
写 20/11/2013 1:46:31 PM
?是不是不变的文化应该使它成为MM/DD/YY?我想使用不变的文化方法,以DD/MM/YY格式输入日期.
谢谢!
编辑:我应该提到的Program.RegisterList[i].DateTime是一个字符串.
EDIT2:
MessageBox.Show("11/20/2013 01:46:31 PM");
MessageBox.Show(Convert.ToDateTime("11/20/2013 01:46:31 PM", CultureInfo.InvariantCulture).ToString());
Run Code Online (Sandbox Code Playgroud)