首页 > 解决方案 > 根据另一个文本框中的值填充文本框中的值

问题描述

我想弄清楚的是如何根据另一个文本框的值填充文本框?

假设 textbox1 值是 .txt 文件的文件路径,我希望 textbox2 更新为“这是一个文本文件”或类似的东西。我该怎么做呢?

提前致谢。

标签: vb.net

解决方案


将数据绑定与自定义.Format事件处理程序一起使用

Public Sub New()
  InitializeComponent()

  ' Bind Text property of one textbox to Text property of another
  Dim binding = 
    txtDesc.DataBindings.Add("Text", txtFile, "Text", True, DataSourceUpdateMode.OnPropertyChanged)

  ' Add format handler to manipulate output for another textbox
  AddHandler binding.Format, Sub(sender, e)
                               Dim extension = Path.GetExtension(e.Value.ToString())
                               e.Value = $"This is a {extension} file"
                             End Sub

End Sub

推荐阅读