标题:写了一个记事本
只看楼主
元帅小白
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2019-4-22
结帖率:100%
 问题点数:0 回复次数:1 
写了一个记事本
错误地方已加粗   求大神帮助处理
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 记事本
{
    public partial class frmMain : Form
    {

        public string fname = "";
        public bool modified = false;
        string fileDialongFilter = "文本文件|*.cs;*.htm;*.html;*.vb|所有文件|*.*";
        private object fongname;
        private readonly int lenfth;

        public frmMain()
        {
            InitializeComponent();
        }
        private void Open()
        {
            openFileDialog1.Title = "打开";
            openFileDialog1.FileName = "";
            openFileDialog1.Filter = this.fileDialongFilter;
            openFileDialog1.FilterIndex = 1;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                fname = openFileDialog1.FileName;
                rtb1.LoadFile(fname, RichTextBoxStreamType.PlainText);
                modified = false;
            }
        }
        private DialogResult NeedSave()
        {
            DialogResult result = DialogResult.None;

            if (modified)
            {
                result = MessageBox.Show("是否保存数据", "提示", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (result == DialogResult.Yes)
                {
                    Save();
                }
            }
            return result;
        }
        private void Save()
        {
            if (fname == "")
            {
                SaveAs();
            }
            else
            {
                rtb1.SaveFile(fname, RichTextBoxStreamType.PlainText);
                modified = false;
            }
        }
        private void SaveAs()
        {
            saveFileDialog1.Title = "另存为";
            this.saveFileDialog1.Filter = "文本文件|*.txt;";

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                if (saveFileDialog1.FileName != "")
                {
                    rtb1.SaveFile(saveFileDialog1.FileName, RichTextBoxStreamType.PlainText);
                    fname = saveFileDialog1.FileName;
                    modified = false;
                }
            }
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

            cbxFontName.Items.Clear();

            foreach (FontFamily ff in FontFamily.Families)
            {
                if (ff.IsStyleAvailable(FontStyle.Regular & FontStyle.Underline & FontStyle.Bold & FontStyle.Italic & FontStyle.Strikeout))
                {
                    cbxFontName.Items.Add(ff.Name);
                }
            }
            cbxFontName.Text = rtb1.Font.Name;
            for (int i = 22; i < 72; i++)
                cbxFontSize.Items.Add(i);
            cbxFontName.Text = rtb1.Font.Name;
            for (int i = 5; i <= 20; i++)
                cbxFontSize.Items.Add(i);
            for (int i = 22; i < 72; i += 2)
                cbxFontSize.Items.Add(i);
            cbxFontName.Text = rtb1.Font.Name;
            cbxFontSize.Text = ((int)(rtb1.Font.Size)).ToString();
            MenuItemNew.PerformClick();

        }

        private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (NeedSave() == DialogResult.Cancel)
                e.Cancel = true;
        }

        private void MenuItemNew_Click(object sender, EventArgs e)
        {
            if(NeedSave()!=DialogResult.Cancel)
            {
                rtb1.ResetText();
                modified = false;
                fname = "";
            }
        }

        private void MenuItemOpen_Click(object sender, EventArgs e)
        {
            Open();
        }

        private void MenuItemSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void MenuItemSaveAs_Click(object sender, EventArgs e)
        {
            SaveAs();
        }

        private void MenuItemExit_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void MenuItemUndo_Click(object sender, EventArgs e)
        {
            rtb1.Undo();
        }

        private void MenuItemWoedWrap_Click(object sender, EventArgs e)
        {
            rtb1.WordWrap = MenuItemWoedWrap.Checked;
        }

        private void MenuItemRedo_Click(object sender, EventArgs e)
        {
            rtb1.Redo();
        }

        private void MenuItemCut_Click(object sender, EventArgs e)
        {
            rtb1.Cut();
        }

        private void MenuItemCopy_Click(object sender, EventArgs e)
        {
            rtb1.Copy();
        }

        private void MenuItemPaste_Click(object sender, EventArgs e)
        {
            rtb1.Paste();
        }

        private void MenuItemAll_Click(object sender, EventArgs e)
        {
            rtb1.SelectAll();
        }

        private void MenuItemFind_Click(object sender, EventArgs e)
        {
            rtb1.FindForm();
        }

        private void MenuItemReplace_Click(object sender, EventArgs e)
        {
            rtb1.Replace();
        }

        private void MenuItemFont_Click(object sender, EventArgs e)
        {
            if(fontDialog1.ShowDialog()==DialogResult.OK)
            {
                rtb1.SelectionFont = fontDialog1.Font;
            }
        }

        private void MenuItemBackColor_Click(object sender, EventArgs e)
        {

        }

        private void toolNew_Click(object sender, EventArgs e)
        {
            MenuItemNew_Click(null, null);
        }

        private void toolOpen_Click(object sender, EventArgs e)
        {
            MenuItemOpen_PerformClick(sender,e);

        }

        private void MenuItemOpen_PerformClick(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

        private void toolSave_Click(object sender, EventArgs e)
        {
            MenuItemSave_Click(sender, e);
        }

        private void toolCopy_Click(object sender, EventArgs e)
        {
            MenuItemCopy_Click();
        }

        private void MenuItemCopy_Click()
        {
            throw new NotImplementedException();
        }

        private void toolCut_Click(object sender, EventArgs e)
        {
            MenuItemCut_Click();
        }

        private void MenuItemCut_Click()
        {
            throw new NotImplementedException();
        }

        private void toolPaste_Click(object sender, EventArgs e)
        {
            MenuItemPaste_Click();
        }

        private void MenuItemPaste_Click()
        {
            throw new NotImplementedException();
        }

        private void toolUndo_Click(object sender, EventArgs e)
        {
            MenuItemRedo_Click();
        }

        private void MenuItemRedo_Click()
        {
            throw new NotImplementedException();
        }

        private void toolRedo_Click(object sender, EventArgs e)
        {
            MenuItemRedo_Click();
        }

        private void toolSlecletAll_Click(object sender, EventArgs e)
        {
            MenuItemSlecletall_Click();
        }

        private void MenuItemSlecletall_Click()
        {
            throw new NotImplementedException();
        }

        private void cbxFontName_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.SetSelectionTxetFont(cbxFontName.Text.Trim());
        }

        private void SetSelectionTxetFont(string v)
        {
            throw new NotImplementedException();
        }

        private void cbxFontSize_SelectedIndexChanged(object sender, EventArgs e)
        {
            float size;
            
            if(float.TryParse(cbxFontSize.Text,out size))
            {
                if (size >= 5.0)
                    this.SetSelectionTextFont(size);
            }
        }

        private void toolBold_Click(object sender, EventArgs e)
        {
            this.SetSelectionTextFont(FontStyle.Bold);
        }

        private void toolItalic_Click(object sender, EventArgs e)
        {
            this.SetSelectionTextFont(FontStyle.Italic);
        }

        private void toolStripButton12_Click(object sender, EventArgs e)
        {
            this.SetSelectionTextFont(FontStyle.Underline);
        }
        private void SetSelectionTextFont(RichTextBox rtb, string fontname, float fontsize, FontStyle fontstyle)
        {
            int currentPosition = rtb.SelectionStart;
            int length = rtb.SelectionLength;
            RichTextBox temp = newRichTextBox();

            temp.Rtf = rtb.SelectedRtf;

            for (int i = 0; i < length; i++)
            {
                temp.Select(i, 1);
                temp.SelectionFont = newFont(fongname == null || fontname == "" ? temp.SelectionFont.Name : fontname,
                    fontsize < 5 ? temp.SelectionFont.Size : fontsize, temp.SelectionFont.Style ^ fontstyle);
            }
            temp.Select(0, lenfth);
            rtb.SelectedRtf = temp.SelectedRtf;

            rtb.SelectionStart = currentPosition;
            rtb.Select(currentPosition, length);
            rtb.Focus();
        }

        private Font newFont(string v1, float v2, object p)
        {
            throw new NotImplementedException();
        }

        private RichTextBox newRichTextBox()
        {
            throw new NotImplementedException();
        }

        private void SetSelectionTextFont(string fontname)
        {
            this.SetSelectionTextFont(rtb1, fontname, 0, FontStyle.Regular);

        }
        private void SetSelectionTextFont(float fontsize)
        {
            this.SetSelectionTextFont(rtb1, "", fontsize, FontStyle.Regular);
        }
        private void SetSelectionTextFont(FontStyle fontstyle)
        {
            this.SetSelectionTxetFont(rtb1, "", fontstyle);
        }

        private void SetSelectionTxetFont(RichTextBox rtb1, string v, FontStyle fontstyle)
        {
            throw new NotImplementedException();
        }

        private void rtb1_TextChanged(object sender, EventArgs e)
        {
            modified = true;
        }

        private void rtb1_SelectionChanged(object sender, EventArgs e)
        {
            int lineNum = rtb1.GetLineFromCharIndex(rtb1.SelectionStart);
            int colNum = rtb1.SelectionStart - rtb1.GetFirstCharIndexFromLine(lineNum);

            try
            {
                msg1.Text = String.Format("行:{0},列:{1}", lineNum, colNum);

            }
            catch(Exception x)
            {
                msg1.Text = x.Message;
            }
            if(rtb1.SelectionType==RichTextBoxSelectionTypes.Text)
            {
                cbxFontName.Text = rtb1.SelectionFont.Name;
                cbxFontSize.Text = rtb1.SelectionFont.Size.ToString();
            }
        }
    }
}

搜索更多相关主题的帖子: private void sender object Text 
2019-11-03 19:25
xianfajushi
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:8
帖 子:527
专家分:690
注 册:2007-9-8
得分:0 
2020-01-06 10:52



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-497595-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023854 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved