-1

What I'm trying to figure out is how can I populate a textbox based on the value of another?

Let's say textbox1 value is a file path to a .txt file, I want textbox2 to update to say "this is a text file" or something of that sort. How would I go about that?

Thanks in advance.

  • Here: https://stackoverflow.com/questions/44371042/how-to-get-file-type-of-a-file –  Apr 10 '20 at 03:22
  • See [here](https://stackoverflow.com/a/47763682/7444103). Call the main method as: `Dim extensionInfo = FileExtentionInfo(AssociationQuery.FriendlyDocName, Path.GetExtension("c:\somefile.txt"))`. You get back something like: `Text Document`. See the other options (e.g., specifying `AssociationQuery.FriendlyAppName` you get, possibly, `Notepad`, or the default opener, whatever that is). Otherwise, you'll have to build a `Dictionary(Of string, string)`, where the Key is the extension and the Value is the string you want to associate with that extension. – Jimi Apr 10 '20 at 03:33
  • Thank you very much guys! And @esqew yes I understand this, just wanted help to get in the right direction. – Darin Johns Apr 10 '20 at 05:35

1 Answers1

0

Use databinding with custom .Format event handler

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
Fabio
  • 31,528
  • 4
  • 33
  • 72