Присвоение значения экземпляру класса - C#

Узнай цену своей работы

Формулировка задачи:

public partial class TextBox1 : Form
    {
        public TextBox1()
        {
            InitializeComponent();
            Size1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(Size_KeyPress);
            Size1.Validating += new System.ComponentModel.CancelEventHandler(Size_Validating);
            Text1.LinkClicked += new System.Windows.Forms.LinkClickedEventHandler(this.Text_LinkedClick);
        }
        private void Size_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)
            {
               TextBox1 txt = (TextBox1)sender;
                if (txt.Text1.TextLength > 0)
                    ApplyTextSize(txt.Text1.Text);
                e.Handled = true;
                Text1.Focus();
            }
        }
   }
строчке TextBox1 txt = (TextBox1)sender; вылетает с ошибкой "Не удалось привести тип объекта "System.Windows.Forms.TextBox" к типу "WindowsFormsApplication2.TextBox1"." и что-то не могу я понять, как это исправить подскажите, пожалуйста. заранее спасибо

Решение задачи: «Присвоение значения экземпляру класса»

textual
Листинг программы
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
 
namespace WindowsFormsApplication1 {
  public partial class Form1 : Form {
    public Form1() {
      InitializeComponent();
    }
 
    private void textBox1_TextChanged(object sender, EventArgs e) {
      if (!Regex.IsMatch(textBox1.Text, "[0-9]+"))
        return;
 
      richTextBox1.SelectAll();
      richTextBox1.SelectionFont = new Font("Times New Roman", Convert.ToInt32(textBox1.Text));
    }
  }
}

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

12   голосов , оценка 3.5 из 5
Похожие ответы