简体   繁体   中英

Removing Prefixes in Visual Basic 2010

I have some code that adds a prefix to text in a label using the code

LblUsername.Text = "Welcome " & TxtUsername.Text

this code works perfectly but when I link this to other form using

 TDC.LblName.Text = Me.LblUsername.Text
    FTDC.LblName.Text = Me.LblUsername.Text
    MP.LblName.Text = Me.LblUsername.Text
    ALC.LblName.Text = Me.LblUsername.Text

i want to strip the prefix. Does anyone know how this is done??

Well you cold two thinks: 1. Simple:

 TDC.LblName.Text = Me.LblUsername.Text.Replace("Welcome ","")

This simply removes the Welcome again. 2. Better because more clean: Introduce a UserNameText Property and acess this property instead of the label. The main advantage is, that can be easily extended and is not as surprising as stripping the Welcome again afterwards.

Like this (in C#):

Public string UserName
{
get
  {
     return TxtUsername.Text;
  }
}

Then acess it liks this: LblUsername.Text = "Welcome " & UserName TDC.LblName.Text = Me.UserName

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM