I’m sure that many Visual Studio beginners need help with the basic functions that more experienced programmers take for granted. Simple visual studio hacks intend to redress this.
You may be a new programmer or, like me, an old hand returning after an extended absence.
I have been absent for quite a few years. I have not been idle. I have succumbed to Multiple Sclerosis. Now I don’t intend to dwell on the details, suffice to say, I am back trying to relearn the Visual Basic skills that have been dormant for quite long enough.
Visual Studio Hacks
I am an ageing programmer returning to the programming environment after many years. I was a Visual Basic 6.0 programmer, so Visual Studio is completely new to me.
However, I have been delighted to embrace the new programming challenges being set by this exciting program development tool.
WPF Tips for the Beginner
There is a plethora of good programming tips for the Windows Presentation Foundation or WPF.
However, a lot of available information focuses on more advanced programming challenges. The simple everyday difficulties that a beginner programmer encounters are not covered to the best effect.
How to show the vertical scrollbar on a TextBox
How do you include a multiline textbox in your visual studio desktop application? There are a couple of properties you need to set.
Firstly, the textbox needs to be able to display text across multiple lines. Two properties will allow this: TextWrapping=”Wrap” and AcceptsReturn=”True”.
Secondly, you need to enable the scrollbar. In fact, you simply need to show the scrollbar which you do by setting VerticalScrollBarVisibility=”Auto”.
Your XAML code may look like this:
<TextBox x:Name="txtPinTitle" Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="3" TextWrapping="Wrap" AcceptsReturn="True" VerticalScrollBarVisibility="Auto"/>
This is one of my simple visual Studio Hacks that you may find helpful.
How to populate a ComboBox from XAML
Continuing with these Visual Studio Hacks, brings us to the use of XAML code to populate your ComboBox list. Sometimes, you will need a Listbox or ComboBox to display a preset list that does not require dynamic updates.
You can create a subroutine in your “code behind” to populate the list. But, a simpler and cleaner solution would be to declare the list in XAML at the time of declaring the ListBox or ComboBox.
The example below should be self-explanatory.
<ComboBox x:Name="cmbDistType" Grid.Row="6" Grid.Column="4" > <ComboBoxItem Content="Item 1" /> <ComboBoxItem Content="Item 2" /> </ComboBox>
I have declared two list items. They are wrapped within the opening and closing elements of the ComboBox declaration.
Using FILEINFO class and the OpenFileDialog
In the past, I would use all manner of string manipulation techniques to extract the filename from a full drive, path and file specification. These methods worked really well. But, I could never remember how I coded them. So every new project needed a refresher course for me to relearn the ways.
Imagine my delight, when early in my WPF programming venture, I discovered the FileInfo class. The methods contained within this class do this exact job, so much more easily and precisely.
This is one of the Visual Studio hacks that I think you will love.
Dim Myfd As New Microsoft.Win32.OpenFileDialog Dim fbd As New Microsoft.Win32.SaveFileDialog Dim Result As Integer Dim mybox As New System.Windows.Forms.FolderBrowserDialog Dim fSysInfo As System.IO.FileInfo, MyFolder As String, MyFile As String Myfd.Title = "File Select" If Myfd.ShowDialog() = True Then txtResult.Text = Myfd.FileName.ToString End If Result = mybox.ShowDialog If Result = Forms.DialogResult.OK Then txtResult.Text = mybox.SelectedPath.ToString End If fSysInfo = My.Computer.FileSystem.GetFileInfo(Myfd.FileName) MyFolder = fSysInfo.DirectoryName MyFile = fSysInfo.Name MessageBox.Show(MyFolder & vbCrLf & MyFile)
Most of the above code should be fairly self-explanatory. The one thing I will point out is the lines of declaration. The reference Microsoft.Win32.OpenFileDialog is a full reference because I haven’t included the files explicitly in my project.
I open a file using the OpenFileDialog, then pass this full filename to the FileInfo object where I can use the Name and DirectoryName properties quite easily and confidently.
How to use the FolderBrowserDialog to return a target folder name
When you wish to browse your file system for only the folder or directory name you will find many supposed solution that hacks the FileOpenDialog.
This is not necessary or recommended. The FolderBrowserDialog does this better and is the preferred method.
Dim mybox As New System.Windows.Forms.FolderBrowserDialog
The declaration is shown because this is in a different library to the earlier examples
How to display an image in the IMG control from code
This is a very misleading control or the handling of this control in WPF is misleading.
It would be logical to assume, just as I did, that you set the Source property to the filename of the image you wish to display. And yes, from XAML that is precisely what you do.
But, when setting this property from your code behind, this doesn’t work. Because, from the code behind, the Source property expects an image object, not a filename.
However, with the code above, you are creating an ImageSource object and assigning this object to the Source property of your IMG control. It uses the ImageSourceConverer method to create the object from the supplied filename.
After this, your image manipulation should be plain sailing.
How to Display a Window as a DialogBox
Visual Studio incorporates a Dialog object but, any window can be utilised as a dialogbox to return any value you wish.
There are two considerations to effect this. the window.Showdialog() method and the window.DialogResult property.
It should be noted that the DialogResult property is a Boolean value so, if you wish to return a string or a long integer, you should define public variables to hold these values.
In the above code from the Button click event of the parent window, we assign the window to be used as a dialog to the MyDlg variable.
We retrieve the passed value from the public variable “TestString”.
Then from the OK Button event of the dialog window, we save the desired string value and set the DialogResult to True to indicate to the calling window that we clicked the OK button. We then close the window.
Cognitive Improvement Workout is supported by advertising the products and services that we use and love.
We use Bluehost and Siteground to host our group sites. Traffic comes from Pinterest aided by Tailwind and Tasty Pins. We rely on BlogVault for backup and the essential security is provided by Sucuri.