VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • 用C#写一个记事本 三

6.编辑:
根据输入是否输入内容控制python基础教程
是否启用功能

private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
{
    剪切ToolStripMenuItem.Enabled = txtBox.Modified;
    if (txtBox.SelectedText == "")
    {
        剪切ToolStripMenuItem.Enabled = false;
        复制ToolStripMenuItem.Enabled = false;
        删除ToolStripMenuItem.Enabled = false;
    }
    else
    {
        剪切ToolStripMenuItem.Enabled = true;
        复制ToolStripMenuItem.Enabled = true;
        删除ToolStripMenuItem.Enabled = true;
    }
    if (txtBox.Text == "")
    {
        查找ToolStripMenuItem.Enabled = false;
        查找下一个ToolStripMenuItem.Enabled = false;
        查找上一个ToolStripMenuItem.Enabled = false;
        替换ToolStripMenuItem.Enabled = false;
    }
    else
    {
        查找ToolStripMenuItem.Enabled = true;
        查找下一个ToolStripMenuItem.Enabled = true;
        查找上一个ToolStripMenuItem.Enabled = true;
        替换ToolStripMenuItem.Enabled = true;
    }
    if (Clipboard.GetText() == "")
        粘贴ToolStripMenuItem.Enabled = false;
    else
        粘贴ToolStripMenuItem.Enabled = true;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

7.查找: 查找功能不够完善,混用查找上一项和查找下一项效果不理想

  TextBox txtInput = new TextBox()
    {
        Font = new Font("宋体", 10)
    };
    TextBox txtInputReplace = new TextBox()
    {
        Font = new Font("宋体", 10)
    };
    Label lblSearch = new Label
    {
        Text = "查找内容:",
        Size = new Size(65, 25),
        Location = new Point(5, 22)
    };
    Label lblDirection = new Label
    {
        Text = "查找方向:",
        Size = new Size(65, 25),
        Location = new Point(5, 58)
    };
    Button FindNext = new Button
    {
        Name = "btnFindNext",
        Text = "查找下一项",
        Size = new Size(80, 25),
        Location = new Point(265, 15)
    };
    Button Cancel = new Button
    {
        Name = "btnCancel",
        Text = "取消",
        Size = new Size(80, 25),
        Location = new Point(265, 50)
    };
    RadioButton down = new RadioButton
    {
        Name = "radDown",
        Text = "向下",
        Size = new Size(55, 25),
        Location = new Point(70, 53),
        Checked = true
    };
    RadioButton upward = new RadioButton
    {
        Name = "radUpward",
        Text = "向上",
        Size = new Size(55, 25),
        Location = new Point(140, 53),
        Checked = false
    };
    new Form FindForm = new Form
    {
        Text = "查找文本",
        FormBorderStyle = FormBorderStyle.FixedSingle,
        MaximizeBox = false,
        MinimizeBox = false
    };
    private void 查找ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //显示查找对话框
        txtInput.Size = new Size(190, 33);
        txtInput.Location = new Point(70, 15);
        txtInput.Multiline = true;

        FindNext.Click += new EventHandler(Direction_Click);
        //FindNext.Click += new EventHandler(Visible_Click);

        Cancel.Click += new EventHandler(Cancel_Click);

        FindForm.Controls.Add(lblSearch);
        FindForm.Controls.Add(lblDirection);
        FindForm.Controls.Add(txtInput);
        FindForm.Controls.Add(down);
        FindForm.Controls.Add(upward);
        FindForm.Controls.Add(FindNext);
        FindForm.Controls.Add(Cancel);
        FindForm.Top = this.Top + 50;
        FindForm.Left = this.Left + 50;
        FindForm.Height = 120;
        FindForm.Width = 380;
        FindForm.StartPosition = FormStartPosition.CenterParent;
        FindForm.ShowDialog();
    }
    private void Cancel_Click(object sender, EventArgs e)
    {
        //关闭对话框
        FindForm.Close();
        ReplaceForm.Close();
    }
    private void Direction_Click(object sender, EventArgs e)
    {
        //选择字符查找方向
        if (down.Checked == true)
        {
            Find_Click(sender, e);
        }
        else if (upward.Checked == true)
        {
            FindLast_Click(sender, e);
        }
    }
            int nextPosition, firstPosition;
    string word;
    Boolean IF = false;
    private void Find_Click(object sender, EventArgs e)
    {
        txtBox.Focus();
        FindWords(txtInput.Text);
    }
    private void FindWords(string words)
    {
        //向下查找字符
        if (nextPosition >= txtBox.Text.Length)
            nextPosition = 0;
        firstPosition = txtBox.Text.IndexOf(words, nextPosition);
        if (firstPosition == -1)
            nextPosition = 0;
        else
        {
            txtBox.Select(firstPosition, words.Length);
            nextPosition = firstPosition + 1;
        }
        word = words;
        IF = true;
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125

在这里插入图片描述

  1. 查找下一c#教程项 :
 private void 查找下一个ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //查找下一项,如果未查找过,则显示查找对话框
        down.Checked = true;
        upward.Checked = false;
        try
        {
            FindWords(word);
        }
        catch
        {
            查找ToolStripMenuItem_Click(sender, e);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

9.查找上一项:

  private void FindWordsLast(string words)
    {
        //向上查找字符
        if (IF == false)
            nextPosition = txtBox.Text.Length;
        if (nextPosition < 0)
            nextPosition = txtBox.Text.Length;

        firstPosition = txtBox.Text.LastIndexOf(words, nextPosition);

        if (firstPosition == -1)
            nextPosition = txtBox.Text.Length;
        else
        {
            txtBox.Select(firstPosition, words.Length);
            nextPosition = firstPosition - 1;
        }
        word = words;
        IF = true;
    }
    private void 查找上一个ToolStripMenuItem_Click(object sender, EventArgs e)
    {
        //查找上一项,如果未查找过,则显示查找对话框
        upward.Checked = true;
        down.Checked = false;
        try
        {
            FindWordsLast(word);
        }
        catch
        {
            查找ToolStripMenuItem_Click(sender, e);
        }
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

10.替换:

Label LblReplace = new Label
{
    Name = "lblReplace",
    Text = "替换:",
    Size = new Size(55, 25),
    Location = new Point(15, 50)
};
Form ReplaceForm = new Form
{
    Text = "替换文本",
    FormBorderStyle = FormBorderStyle.FixedSingle,
    MaximizeBox = false,
    MinimizeBox = false
};
private void 替换ToolStripMenuItem_Click(object sender, EventArgs e)
{
    txtInput.Size = new Size(190, 30);
    txtInput.Location = new Point(70, 12);
    txtInput.Multiline = true;

    txtInputReplace.Size = new Size(190, 30);
    txtInputReplace.Location = new Point(70, 47);
    txtInputReplace.Multiline = true;

    Button Replace = new Button
    {
        Name = "btnReplace",
        Text = "替换",
        Size = new Size(80, 25),
        Location = new Point(265, 15)
    };
    Replace.Click += new EventHandler(Replace_Click);
    Cancel.Click += new EventHandler(Cancel_Click);

    ReplaceForm.Controls.Add(lblSearch);
    ReplaceForm.Controls.Add(LblReplace);
    ReplaceForm.Controls.Add(txtInput);
    ReplaceForm.Controls.Add(txtInputReplace);
    ReplaceForm.Controls.Add(Replace);
    ReplaceForm.Controls.Add(Cancel);
    ReplaceForm.Top = this.Top + 50;
    ReplaceForm.Left = this.Left + 50;
    ReplaceForm.Height = 140;
    ReplaceForm.Width = 380;
    ReplaceForm.StartPosition = FormStartPosition.CenterParent;
    ReplaceForm.ShowDialog();
}
private void Replace_Click(object sender, EventArgs e)
{
    txtBox.Text = txtBox.Text.Replace(txtInput.Text, txtInputReplace.Text);
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51

在这里插入图片描述


相关教程