[英]How to delete last entering character(s) if TextBox width bigger than 75
[英]How to delete the last character from a TextBox?
我正在嘗試編寫MS Windows計算器的副本 - 只是為了練習我在我正在做的課程中獲得的知識 - 而且我在編寫Backspace鍵時遇到問題,但我不知道如何刪除TxtResult.Text
(文本框)上的最后一個字符。 那么,有人可以教我怎么做嗎?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace ZigndSuperCalc
{
public partial class FrmZigndSC : Form
{
Int64 aux, result;
Int16 cont = 0;
bool sucess;
public FrmZigndSC()
{
InitializeComponent();
}
private void BtnSoma_Click(object sender, EventArgs e)
{
sucess = Int64.TryParse(TxtInput.Text, out aux);
result += aux;
TxtInput.Text = Convert.ToString(result);
TxtInput.Focus();
}
private void BtnCE_Click(object sender, EventArgs e)
{
TxtInput.Text = "0";
}
private void BtnC_Click(object sender, EventArgs e)
{
result = 0;
TxtInput.Text = "0";
}
private void BtnBackspace_Click(object sender, EventArgs e)
{
// write here a method to delete the last character from
}
}
}
如果我們正在模仿calc.exe
,那么它可能是這樣的:
string s = TxtResult.Text;
if (s.Length > 1) {
s = s.Substring(0, s.Length - 1);
} else {
s = "0";
}
TxtResult.Text = s;
編輯:根據要求,我在這里使用的Substring
方法提取字符串的一部分,並將其分配給Text
框的Text
屬性。 請參閱: http : //msdn.microsoft.com/en-us/library/aka44szs.aspx
嘗試
TxtResult.Text = TxtResult.Text.Substring(0, TxtResult.Text.Length - 1);`
if (textBox1.TextLength > 0)
{
textBox1.Text = textBox1.Text.Substring(0, (textBox1.TextLength - 1));
}
else
{
MessageBox.Show("No Number.");
}
我用這個方法:
private void button_Click(object sender, EventArgs e)
{
if (textbox.Text.Length > 0)
{
textbox.Text = textbox.Text.Remove(textbox.Text.Length - 1);
}
}
//CLICK ON BUTTON1
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Length > 1)
{
textBox1.Text = textBox1.Text.Substring(0, textBox1.Text.Length - 1);
}![enter image description here][1]
else
{
textBox1.Text = "0";
}
}
你必須創建一個名為“buttonDel”的按鈕
private void buttonDel_Click(object sender, EventArgs e)
{
if (sender == buttonDel)
{
s = textBox1.Text;
if (s.Length > 1)
{
s = s.Substring(0,s.Length - 1);
textBox1.Text = s;
}
else
{
textBox1.Text = "0";
}
}
}
這對我有用!!
private void txtNumCL_TextChanged(object sender, EventArgs e)
{
bool ctrl = Int32.TryParse(txtNumCL.Text, out int outParse);
if (!ctrl && txtNumCL.Text.Length > 0)
{
txtNumCL.Text = txtNumCL.Text.Substring(0, txtNumCL.Text.Length - 1);
txtNumCL.SelectionStart = txtNumCL.Text.Length;
}
}
更新:
output.Text=output.Text.Remove(output.Text.Length-1, 1);
舊:您可以使用:
TxtInput.Text = TxtInput.Text.ToString().Remove(TxtInput.Text.ToString().Length-1,1);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.