weixin_48051348 2020-07-04 16:30 采纳率: 0%
浏览 129

VC6提示下面程序(网上来学习找的)有误,请大神该改,有大神可以赐教吗?

namespace RichTextBoxTest
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    #region Windows 窗体设计器生成的代码

    /// <summary>
    /// 设计器支持所需的方法 - 不要
    /// 使用代码编辑器修改此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
        this.btnBold = new System.Windows.Forms.Button();
        this.btnItalic = new System.Windows.Forms.Button();
        this.btnUnderline = new System.Windows.Forms.Button();
        this.btnCenter = new System.Windows.Forms.Button();
        this.lblSize = new System.Windows.Forms.Label();
        this.txtSize = new System.Windows.Forms.TextBox();
        this.rtfText = new System.Windows.Forms.RichTextBox();
        this.btnLoad = new System.Windows.Forms.Button();
        this.btnSave = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // btnBold
        // 
        this.btnBold.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.btnBold.Location = new System.Drawing.Point(136, 27);
        this.btnBold.Name = "btnBold";
        this.btnBold.Size = new System.Drawing.Size(75, 23);
        this.btnBold.TabIndex = 0;
        this.btnBold.Text = "Bold";
        this.btnBold.UseVisualStyleBackColor = true;
        this.btnBold.Click += new System.EventHandler(this.btnBold_Click);
        // 
        // btnItalic
        // 
        this.btnItalic.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.btnItalic.Location = new System.Drawing.Point(252, 27);
        this.btnItalic.Name = "btnItalic";
        this.btnItalic.Size = new System.Drawing.Size(75, 23);
        this.btnItalic.TabIndex = 1;
        this.btnItalic.Text = "Italic";
        this.btnItalic.UseVisualStyleBackColor = true;
        this.btnItalic.Click += new System.EventHandler(this.btnItalic_Click);
        // 
        // btnUnderline
        // 
        this.btnUnderline.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.btnUnderline.Location = new System.Drawing.Point(365, 26);
        this.btnUnderline.Name = "btnUnderline";
        this.btnUnderline.Size = new System.Drawing.Size(75, 23);
        this.btnUnderline.TabIndex = 2;
        this.btnUnderline.Text = "Underline";
        this.btnUnderline.UseVisualStyleBackColor = true;
        this.btnUnderline.Click += new System.EventHandler(this.btnUnderline_Click);
        // 
        // btnCenter
        // 
        this.btnCenter.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.btnCenter.Location = new System.Drawing.Point(483, 26);
        this.btnCenter.Name = "btnCenter";
        this.btnCenter.Size = new System.Drawing.Size(75, 23);
        this.btnCenter.TabIndex = 3;
        this.btnCenter.Text = "Center";
        this.btnCenter.UseVisualStyleBackColor = true;
        this.btnCenter.Click += new System.EventHandler(this.btnCenter_Click);
        // 
        // lblSize
        // 
        this.lblSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.lblSize.AutoSize = true;
        this.lblSize.Location = new System.Drawing.Point(208, 74);
        this.lblSize.Name = "lblSize";
        this.lblSize.Size = new System.Drawing.Size(47, 12);
        this.lblSize.TabIndex = 4;
        this.lblSize.Text = "lblSize";
        // 
        // txtSize
        // 
        this.txtSize.Anchor = System.Windows.Forms.AnchorStyles.Top;
        this.txtSize.Location = new System.Drawing.Point(261, 65);
        this.txtSize.Name = "txtSize";
        this.txtSize.Size = new System.Drawing.Size(38, 21);
        this.txtSize.TabIndex = 5;
        this.txtSize.Text = "10";
        // 
        // rtfText
        // 
        this.rtfText.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                    | System.Windows.Forms.AnchorStyles.Left)
                    | System.Windows.Forms.AnchorStyles.Right)));
        this.rtfText.Location = new System.Drawing.Point(24, 101);
        this.rtfText.Name = "rtfText";
        this.rtfText.Size = new System.Drawing.Size(677, 283);
        this.rtfText.TabIndex = 6;
        this.rtfText.Text = "";
        // 
        // btnLoad
        // 
        this.btnLoad.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
        this.btnLoad.Location = new System.Drawing.Point(224, 420);
        this.btnLoad.Name = "btnLoad";
        this.btnLoad.Size = new System.Drawing.Size(75, 23);
        this.btnLoad.TabIndex = 7;
        this.btnLoad.Text = "Load";
        this.btnLoad.UseVisualStyleBackColor = true;
        this.btnLoad.Click += new System.EventHandler(this.btnLoad_Click);
        // 
        // btnSave
        // 
        this.btnSave.Anchor = System.Windows.Forms.AnchorStyles.Bottom;
        this.btnSave.Location = new System.Drawing.Point(407, 420);
        this.btnSave.Name = "btnSave";
        this.btnSave.Size = new System.Drawing.Size(75, 23);
        this.btnSave.TabIndex = 8;
        this.btnSave.Text = "Save";
        this.btnSave.UseVisualStyleBackColor = true;
        this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
        // 
        // Form1
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(729, 476);
        this.Controls.Add(this.btnSave);
        this.Controls.Add(this.btnLoad);
        this.Controls.Add(this.rtfText);
        this.Controls.Add(this.txtSize);
        this.Controls.Add(this.lblSize);
        this.Controls.Add(this.btnCenter);
        this.Controls.Add(this.btnUnderline);
        this.Controls.Add(this.btnItalic);
        this.Controls.Add(this.btnBold);
        this.MinimumSize = new System.Drawing.Size(737, 510);
        this.Name = "Form1";
        this.Text = "Form1";
        this.ResumeLayout(false);
        this.PerformLayout();

    }

    #endregion

    private System.Windows.Forms.Button btnBold;
    private System.Windows.Forms.Button btnItalic;
    private System.Windows.Forms.Button btnUnderline;
    private System.Windows.Forms.Button btnCenter;
    private System.Windows.Forms.Label lblSize;
    private System.Windows.Forms.TextBox txtSize;
    private System.Windows.Forms.RichTextBox rtfText;
    private System.Windows.Forms.Button btnLoad;
    private System.Windows.Forms.Button btnSave;
}

}

实现代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace RichTextBoxTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();

       //订阅事件处理程序
        this.txtSize.KeyPress += 
            new KeyPressEventHandler(txtSize_KeyPress);
        this.txtSize.Validating += 
            new CancelEventHandler(txtSize_Validating);
        this.rtfText.LinkClicked += 
            new LinkClickedEventHandler(rtfText_LinkClicked);

    }


    /// <summary>
    /// Bold按钮的响应事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnBold_Click(object sender, EventArgs e)
    {
        Font oldFont;
        Font newFont;

        oldFont = this.rtfText.SelectionFont;

        if (oldFont.Bold)
        {
            newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold);//取消黑体格式
        }
        else
        {
            newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold); //添加黑体格式
        }

        this.rtfText.SelectionFont = newFont;
        this.rtfText.Focus();
    }


    /// <summary>
    /// Italic按钮的响应事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnItalic_Click(object sender, EventArgs e)
    {
        Font oldFont;
        Font newFont;

        oldFont = this.rtfText.SelectionFont;

        if (oldFont.Italic)
        {
            newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic);
        }
        else
        {
            newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic);
        }

        this.rtfText.SelectionFont = newFont;
        this.rtfText.Focus();
    }


    /// <summary>
    /// Underline的响应事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnUnderline_Click(object sender, EventArgs e)
    {
        Font oldFont;
        Font newFont;

        oldFont = this.rtfText.SelectionFont;

        if (oldFont.Underline)
        {
            newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline);
        }
        else
        {
            newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline);
        }

        this.rtfText.SelectionFont = newFont;
        this.rtfText.Focus();
    }

    /// <summary>
    /// Center的响应事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void btnCenter_Click(object sender, EventArgs e)
    {
        if (this.rtfText.SelectionAlignment == HorizontalAlignment.Center)
        {
            this.rtfText.SelectionAlignment = HorizontalAlignment.Left;
        }
        else
        {
            this.rtfText.SelectionAlignment = HorizontalAlignment.Center;
        }

        this.rtfText.Focus();
    }


    /// <summary>
    /// 键盘的键按下的响应事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void txtSize_KeyPress(object sender, KeyPressEventArgs e)
    {
        if ((e.KeyChar < 48 || e.KeyChar > 57)
            && e.KeyChar != 8 && e.KeyChar != 13)
        {
            e.Handled = true;
        }
        else if(e.KeyChar == 13)
        {
            TextBox tb = (TextBox)sender;

            if (tb.Text.Length > 0)
            {
                ApplyTextSize(tb.Text);
            }
            e.Handled = true;
            rtfText.Focus();
        }
    }

    /// <summary>
    /// 字体设置响应事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void txtSize_Validating(object sender, CancelEventArgs e)
    {
        TextBox tb = (TextBox)sender;

        ApplyTextSize(tb.Text);
        this.rtfText.Focus();
    }


    /// <summary>
    /// 实现网页的链接
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    private void rtfText_LinkClicked(object sender, LinkClickedEventArgs e)
    {
        System.Diagnostics.Process.Start(e.LinkText);
    }


    private void ApplyTextSize(string textSize)
    {
        float newSize = Convert.ToSingle(textSize);
        FontFamily currentFontFamily;
        Font newFont;

        currentFontFamily = this.rtfText.SelectionFont.FontFamily;
        newFont = new Font(currentFontFamily, newSize);

        this.rtfText.SelectionFont = newFont;

    }


    //加载文件
    private void btnLoad_Click(object sender, EventArgs e)
    {
        try
        {
            rtfText.LoadFile("../../Test.rtf");
        }
        catch(System.IO.FileNotFoundException)
        {
            MessageBox.Show("NO file to load yet");
        }
    }


    //保存文件  
    private void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            rtfText.SaveFile("../../Test.rtf");
        }
        catch(System.Exception err)
        {
            MessageBox.Show(err.Message);
        }
    }


}

}

  • 写回答

1条回答 默认 最新

  • wlj1234 2020-07-04 16:51
    关注

    这是C#程序,你这入门的路偏的远了点

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料