C Sharp Clear Method [英] C Sharp Clear Method

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

问题描述

这两行之间有什么区别:

 comboBox3.Text =  ; 
textBox5.Clear();

解决方案





当我反编译 TextBoxBase.Clear 方法时,我得到了这个:

  ///   <  摘要 >  
/// 清除文本框控件中的所有文本。
/// < / summary >
public void 清除()
{
.Text =( string null ;
}



因此,清除方法设置文本属性为 null


'TextBox的'Clear方法只删除所有文本来自TextBox的内容;它完全等同于someTextBox.Text =; ...或者... someTextBox.Text = null;



操作ComboBox的'文本字段是一个非常不同的过程,因为它与ComboBox的当前'SelectedIndex属性



1. comboBox3.Text =; ...或comboBox3.Text = null; ...将当前SelectedIndex设置为-1。您将在运行时在ComboBox文本区域中看到的内容是:nothing。



2. comboBox3.Text =Choice ABC;可能有两种效果:



a。如果Choice ABC ComboBox项集合中的现有项目,则当前SelectedIndex将设置为与Choice ABC的索引匹配。并且,选择ABC将显示在ComboBox文本区域中。



b。如果选择ABC不是 ComboBox项目集合中的现有项目,则不会更改当前的SelectedIndex,但选择ABC将显示在ComboBox文本区域中。

Hello Mohi,



两种方法的动作相似,它们清除了文本框的内容,你可以使用 TextBox.Clear 清除控件的内容的方法,而不是为Text属性分配一个空字符串。



问候,


what is the difference between these two lines :

comboBox3.Text = "";
textBox5.Clear();

解决方案

Hi,

When I decompiled the TextBoxBase.Clear method, I got this:

/// <summary>
/// Clears all text from the text box control.
/// </summary>
public void Clear()
{
  this.Text = (string) null;
}


So, the Clear method sets the Text property to null.


The 'Clear method of a TextBox simply removes all text content from the TextBox; it is exactly equivalent to someTextBox.Text = ""; ... or ... someTextBox.Text = null;

Manipulating the 'Text field of a ComboBox is a very different process because it interacts with the current 'SelectedIndex property of the ComboBox:

1. comboBox3.Text = ""; ... or comboBox3.Text = null; ... will set the current SelectedIndex to -1. What you will see displayed in the ComboBox text-area at run-time is: nothing.

2. comboBox3.Text = "Choice ABC"; will possibly have two effects:

a. if "Choice ABC" is an existing item in the ComboBox item collection, then the current SelectedIndex will be set to match the index of "Choice ABC." And, "Choice ABC" will be displayed in the ComboBox text-area.

b. if "Choice ABC" is not an existing item in the ComboBox item collection, then the current SelectedIndex will not be changed, but "Choice ABC" will be displayed in the ComboBox text-area.


Hello Mohi,

Both methods are similar in their action, they clear the contents of a textbox, You can use TextBox.Clear method to clear the contents of the control instead of assigning the Text property an empty string.

Regards,


这篇关于C Sharp Clear Method的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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