如何定义常数 [英] How defined a constant

查看:135
本文介绍了如何定义常数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好

我正在创建简单的笔记应用程序,只需打开新文件保存并保存,但当我到达编写代码保存时,它可以找到我的常数我不知道为什么



hello
i'm creating simple note application to do just open new file save and save but when i arrive to write codes for save it can find my constant i don't know why

SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "*.txt(textfile)|*.txt";
if (savefile.ShowDialog() == DialogResult.OK)
    {
    rtb.SaveFile(savefile.FileName, RichTextBoxStreamType.PlainText);
    }



已添加代码块 - OriginalGriff [/ edit]



我尝试了什么:



这是我的所有代码

代码末尾给出错误信息'rtb.SaveFile .....'的名称不存在




[edit]Code block added - OriginalGriff[/edit]

What I have tried:

this is all my codes
at the end of the code it give an error message that the name of 'rtb.SaveFile.....' don't exists

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;




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

        private void tabControl1_Layout(object sender, LayoutEventArgs e)
        {

        }

        private void nouveauToolStripMenuItem_Click(object sender, EventArgs e)
        {
            TabPage tp = new TabPage("New Document");
            RichTextBox rtb = new RichTextBox();
            rtb.Dock = DockStyle.Fill;
            tp.Controls.Add(rtb);
            tabControl1.TabPages.Add(tp);
        }

        private void ouvrirToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Stream myStream;
            if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    string filename = openFileDialog1.FileName;
                    string readfiletext = File.ReadAllText(filename);
                    TabPage tp = new TabPage("New Document");
                    RichTextBox rtb = new RichTextBox(); 
                    rtb.Dock = DockStyle.Fill; 
                    tp.Controls.Add(rtb); 
                    tabControl1.TabPages.Add(tp);
                    rtb.Text = readfiletext;
                }
            }
        }

        private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
        {

        }

        private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
        {
         
        }

        private void enregistrerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog savefile = new SaveFileDialog();
            savefile.Filter = "*.txt(textfile)|*.txt";
            if (savefile.ShowDialog() == DialogResult.OK)
            {
             rtb.SaveFile(savefile.FileName, RichTextBoxStreamType.PlainText);
            }
        }
    }
}

推荐答案

rtb不是常量。如果要访问它,则必须将其定义为类变量,或者从表单中检索它。



rtb isn't a constant. If you want to access it, you have to either define it as a class variable, or retrieve it from the form.

public class Form1 : Form
{
    private RichTextbox rtb = new RichTextBox(){ Dock = DockStyle.Fill };

   ... more code
}


这篇关于如何定义常数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆