小编Nat*_*han的帖子

在C#中显示打印预览

现在,我正在尝试在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)

.net c# printing printdocument winforms

8
推荐指数
1
解决办法
3万
查看次数

右对齐印刷文本

我现在正在打印收据,但我无法弄清楚如何在图形模式下正确对齐文本.我尝试了几种不同的东西,但它们要么效率低下,要么在我的情况下不起作用.有没有办法可以轻松地将文字对齐到右边?这是我现在的代码.

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)

.net c# winforms visual-studio-2012

7
推荐指数
2
解决办法
1万
查看次数

运算符不能应用于'Method Group'和'int'类型的操作数

我正在尝试获取此字符串数组中的元素数量,但它不会让我离开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'类型的操作数.

什么是正确的方法来做到这一点?

c# arrays for-loop

6
推荐指数
3
解决办法
2万
查看次数

在空格处拆分长串

在我的程序中,如果它太长,我需要将一个字符串拆分成多行.现在我正在使用这种方法:

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个字符,但如果可以的话,请回到最近的空格?我已经习惯了上面的算法,但我似乎无法得到它.

谢谢!

.net c# string

5
推荐指数
1
解决办法
2470
查看次数

当我按下文本框中的输入时发出恼人的嘟嘟声

当用户按下文本框中的输入时,我正试图让某些事情发生,并且它可以工作,但是当我这样做时,它会产生一个非常烦人的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)

难道我做错了什么?这是其他人说你必须要做的事情,但由于某些原因它并不适合我...

谢谢!

.net c# windows visual-studio-2010 winforms

5
推荐指数
1
解决办法
1297
查看次数

使用变量与幻数的性能影响

我经常对此感到困惑.我一直被教导用经常使用变量或常量命名的数字,但如果它降低了程序的效率,我还应该这样做吗?下面是一个例子:

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)

在这个例子中,执行时间的差异是最小的,但是在更大的代码块中呢?

c# magic-numbers

3
推荐指数
2
解决办法
858
查看次数

Visual Studio不更新内部版本

目前,我正在VS Express 2013中创建一个项目,但我注意到我的构建不再包含任何新内容。我的意思是说,它会一次又一次地构建相同的旧项目,而实际上并没有在构建中添加任何内容。当我尝试关闭并重新打开VS时,所有更改仍在设计器和.cs文件中,但构建仍未提供任何更新。我正在使用默认设置和调试构建设置进行构建。

我也尝试过这里提到的内容:Visual Studio继续运行旧版本

但是构建复选框已被选中,因此这不是问题。

有什么线索吗?

谢谢!

c# build visual-studio-2013

3
推荐指数
2
解决办法
7342
查看次数

为什么这段代码运行如此缓慢?

我正在尝试使用这些方法在屏幕的矩形区域中查找颜色。但是有时屏幕上有数百万个像素,而我现在仅每秒实现约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)

java performance awt awtrobot

2
推荐指数
1
解决办法
156
查看次数

混淆/随机化字符串

我如何处理混淆文本,因此读取文本文件的用户无法读取,但我的程序仍然可以读取它?基本上,我会True*True*False*True*False*False*False*True*False*true*在文本文件中有类似的东西,我需要它看起来很疯狂.

我知道如何从文件中获取文本并写入文件和所有内容,我只需要弄清楚如何对字符串进行模糊处理并对其进行去混淆.这可能没有进入所有疯狂的加密东西吗?我认为AES和其他加密方法都是矫枉过正,因为在我的程序中,这些信息并不是绝密或其他什么,它可以在程序中查看.我只是不希望它直接通过文件编辑.

谢谢一堆:D

弥敦道

.net c# encryption obfuscation winforms

1
推荐指数
1
解决办法
1754
查看次数

DateTime很奇怪

怎么会

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)

.net c# datetime date

1
推荐指数
1
解决办法
115
查看次数