Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

How do I place code on a Visual Studio control?

I'm trying to learn to use Visual Studio 2012, using the MS tutorial that's included. But I come to a grinding halt after creating a simple project (two radio buttons and a control button) when I try to add code for the control button. Where, and how, do I add the codes?

3 Answers

Relevance
  • Nick
    Lv 5
    7 years ago
    Favorite Answer

    For a button? You should be able to double click it and put the code in the code behind file. It should put your cursor where it needs to be for the control.

    What Pankaj is referring to as a region is

    #Region "Name of The Region"

    Your code would go here

    #End Region

    It makes it easier to navigate if you have large files. I am an ASP.NET programmer and I often poke around in 2000-4000 line files of VB code and tend to get a little cranky if people don't use Regions but I honestly don't feel they are important for small projects like you are going to be doing. When you are just learning and you aren't building anything too complex you should probably have all the code visible instead of collapsed so you can teach yourself to see what is going on.

    If something works you may want to step through it to watch what it does. Just click off to the left of your code on a little bar beside your code window and you should get a little red dot that highlights the line in red. Then you hit the play button and it should stop when you hit the code. Then you can step through it with F10 or F11 and you can actually see what each line does by mousing over the variables or by highlighting and adding a watch or pinning a section to source. It will help you understand what is going on a bit better and being able to use those tools is EXTREMELY important. This is obviously something you need to worry about later rather than right now but still.

    Your code will almost always be in a "Code Behind" file attached to a particular file if you are doing web programming. You can put the code on the front page if you want but it generally isn't a good idea. You can insert it between server tags <% like this %>.

    If you are just doing a Windows app your code should be in a code behind file attached to the XAML file.

    You can see an example here. There is the App.xaml with a code behind file named App.xaml.vb and a MainPage.xaml with a MainPage.xaml.vb.

    The .vb extension means that the code behind file is written in Visual Basic. This is the solution explorer by the way. To the right of your workspace it should say solution explorer on a tab.

    Attachment image
  • 7 years ago

    Thanks Nick.

    You helped me finally get that "very complex" application to run. I just wasn't sure from the tutorial what parts of the code to put where, and I'm still not sure what I did wrong. I don't think the tutorial is up-to date, as it doesn't show some things they tell me to expect - like the list of files in the Solutions Explorer for instance.

  • 7 years ago

    Try :

    Use Regions

    A truly nifty feature of the Visual Studio .NET code editor is the concept of regions. You can create named regions directly in your source code. You can then expand and collapse regions in the editor to hide or show code based on its type. For example, you could create a region called “Public Properties” and put all your property code there. Additional regions could support your public methods, and privates. Using regions is easy: simply type the #Region phrase followed by the name for your region, as in:

    You can now expand and collapse the region by clicking on the +/- icons on the right. You can also use the keyboard to expand and collapse regions. Try Ctrl-M, M and Ctrl-M, L.

    Use the Toolbox to Store Code Snippets

    How about this? Highlight some code in the code editor. Drag the code on to the toolbox. Lo and behold, you now have a snippet of code saved. You can drag that snippet from the toolbox back on to the code editor window in any place and you code snippet is automatically inserted.

    To make this feature truly useful (and to avoid cluttering up your default toolbox tabs) consider creating one or more tabs just to hold snippets. To do this, right-click on the toolbox and select Add Tab. Create a new tab and give it name to describe the type of snippet, and then drag snippets on that tab. I spent an hour or so spelunking through my local disk trying to find where Visual Studio .NET stores these items, but to no avail. Perhaps they are in some mystery binary blob, or hidden deep in the registry encoded as GUIDs. Or perhaps, they are sent off as SOAP packets to that mysterious internet “cloud” Bill Gates keeps talking about. Regardless, I would hardly use this feature as an industrial strength code repository. (What happens when you re-install Visual Studio .NET or move to another computer? Where do your snippets go?)

    Finally, remember that all code repositories are potential bug factories. If you paste in buggy code and re-use it, you are cloning bugs. And this inevitably leads to fascinating new strains and variants of defects which are likely to be un-fixable.

    Control Minus Takes You Back

    I was programming in Visual Studio .NET for over 6 months before I discovered this one, and it is one of the most useful navigation keys there is. To “Go Back” (in other words, return to the previous position before you navigated away) simply press Ctrl-Minus. This works across windows, and the navigation buffer appears to be limitless, so this is a great replacement for the old VB Ctrl-Shift-F2.

    Dock the Class View to the Left of Code

    Call me slow, but after a class I’m writing reaches over 100 properties and methods, I can no longer get a quick handle on how it works. Although using Regions in code makes code expandable and collapsible, that can only go so far. One neat trick I’ve found is to dock the Class View toolwindow directly to the left of my code window. The Class View then acts as a table of contents for me as I work on the code.

    Use Folders to Organize Complex Solutions

    If your solution View tab is becoming hard to manage because your solution has too many projects and items, consider creating folders to group items. To do this, right-click in the Solution Explorer and select Add, New Folder. You can create folders to hold user controls, HTML pages, resources, and other items. This makes navigating through a complex project much easier. You can even drag existing items into new folders which makes reorganizing existing projects much easier.

    Use Full Screen Mode to Battle Dense Code

    When you are working with lots of code and need to see as much as possible, or are designing a particularly large form, switch in to FullScreen mode. To do this, select View, Full Screen from the menu, or press Shift-Alt-Enter. FullScreen mode gives full screen real estate to the current design window by hiding all the toolwindows and command bars. Shift-Alt-Enter again and you’re back in regular view.

    Get a Bigger Monitor

    Visual Studio.NET is a festival of toolbars, menus, toolwindows, and editors. To use the IDE to its fullest potential, you really should consider working at a minimum resolution of 1280x1024. And 1600x1200 is ideal. The obvious problem with these resolutions is that text can be hard to see. Hence, you need a bigger monitor. 19 inch monitors are now cheap enough that you owe it to yourself to upgrade if you haven’t already done so. Increased productivity and coding skills are sure to follow.

    Go To Definition (With the Keyboard)

    Yet another keystroke that evaded me for the first six months of .NET development: Go To Definition. In the code editor, it is easy to right-click on a name and select Go To Definition, but the keystroke eluded me. Here it is. It is F12. There are hundreds of very cool key combinations in Visual Studio.NET. The best guide to these is the online help topic:

    Visual Studio Shortcut Keys

    Zoom HTML Windows (like Visual Studio .NET Help for Instance)

    All Visual Studio.NET help is in HTML format. That means when you view help, you are using Internet Explorer. Did you know you can change the text size on the fly to help read those particularly dense code examples? The five cent way is to right-click on the help window, and select Text Size. You can then select one of six preset text sizes. But did you know that if you have a Microsoft Mouse with a wheel, you can use the wheel? Simply click anywhere in the help window, hold down the Shift key, and wheel away. You can zoom in and out with this handy feature.

    Hidden Power of the “Find” toolbar dropdown

    You probably never noticed it before, but the innocuous little Find dropdown on the toolbar is actually a fully function command window.

    Click on the box and type the > character. Now you are in command mode. Type in any valid Visual Studio .NET command alias and press Enter. What is a Command Alias you ask? You can find a complete list of pre-defined aliases in the following Visual Stduio .NET help topic:

    Pre-defined Visual Studio Command Aliases

    Resize Windows Forms Controls with the Mouse

    If you are like me, after about seven cups of coffee, your hands shake way to bad to use the mouse to do minute sizing and positioning of controls. Here’s a tip: use the keyboard instead. Simply highlight the control, and use Ctrl-Left/Right and Ctrl-Up/Down to resize. Of course, this isn’t new to Visual Studio .NET (You could do this in VB 6, at least I think you could), but it bears repeating.

    Use Keyboard Mapping Schemes

    If you are coming to Visual Studio .NET from another Microsoft IDE such as Visual Basic of VC++, you may find that the default key mappings are way to foreign for you to be productive. Although you have the chance to set default key mappings on the Start Page when Visual Studio .NET runs for the first time, you may want to change them as you work on a different machine. To do this, go to Tools, Options, Environment, Keyboard and select the scheme that most closely matches what you are used to. You can also use this dialog to map keys to commands.

    Make Visual Studio .NET a bit Snappier

    Depending on your hardware, Visual Studio .NET may seem a bit sluggish compared to previous Microsoft IDEs. Here are two simple tips to make things appear a bit quicker. First, turn off that awful Dynamic Help window. When open, it steals cycles as it tries to determine which inappropriate help topics to display based on where the mouse currently is. Second, go to Tools, Options, Environment, General and turn off the Animate Environment Tools option. This option causes un-pinned toolwindows to fold in slowly. Who need’s that? When you’re three days past your deadline, you have 200 unsolved bugs, and there is too little blood in your caffeine system, that last thing you need is slow window paint effects.

    Build Code Comment Web Pages

    C# has the ability to be somewhat self-documenting through the use of XML Comments. You can type XML comments into your code and the Visual Studio .NET Build Code Comment Web Pages command can extract those comments, along with information from the code itself to generate basic documentation web pages. For example, if you typed the following comments before your button1_Click member:

    You then generate Comment Web Pages, and you see the following entry:

    C++ has limited support for this feature, and Visual Studio .Net has no support for XML Comments. However, with Visual Studio .Net, you can still Build Code Comment Web Pages albeit without much information.

    Visual Basic .NET Shows You Errors as you Type

    Visual Basic .NET has a background compiler that compiles your code as you type. A huge benefit of this technology is that Visual Studio .NET can show you compiler errors as you type. This benefit is truly wonderful when you remember the old Visual Basic debug cycle: compile, hit the first error, respond to the message box, fix the error, compile, and start again. With Visual Basic .NET, you can see compiler errors right way.

    Compiler errors are shown as a squiggly underline in your code. The default color for this squiggly is blue, but I like compiler errors to be really noticeable. Mine show up in red (you can change this in Tools, Options, Fonts and Colors. The real interesting part happens when you hover your mouse over the offending code: it shows a tool tip explaining the error.

    You can also see all compile errors in the Task List toolwindow as you type.

    Source(s): gathered
Still have questions? Get your answers by asking now.