# VS2010 Question



## Crockeo (Apr 17, 2010)

Lets just start of by saying, I am absolutely TERRIBLE at programming. I know nothing about visual basic, or really any programming language. So please, don't call me out on my mistakes (of which there will be many).

Now, to the real question. I was wondering if there were a command like start or run that I could use in Visual Studio 2010. I had an idea of a universal program starter. But I have no idea how to do anything in VS2010 (as mentioned before). It boils down to, is there a command in that Visual Studio would use to start another program?


----------



## Ninjaboi (Dec 27, 2010)

We'll I'd say no there isn't to that question. VS2010 was not made to open and run other programs from it. Of course it can open source files, images, lots of things. But from the sound of it, your just wanting to be able to start VS2010, and run other programs from it. I personally don't see why you'd want to do that really. Now maybe I'm misinterpreting your question. Are you asking if you can get a program file and open it to see how it works? Are you wanting to make a program that will start up and allow quick links to other programs?


----------



## Crockeo (Apr 17, 2010)

My question is whether or not I can program a button in Visual Studio 2010 to open another program (like notepad) when pressed (so I can have, lets say, a universal game launcher).


----------



## niemiro (Feb 20, 2010)

Hello!

Yes, you can. This is very simple to do (for us at least!) What are you looking to launch? I don't quite see what point thus one button would have. Let me assume that you want to start C:\Game.exe

You open your program, and you press a button, it starts game.exe. Are you looking to create a Form with lots of buttons, one for each game? 

Just as a slight side point (and get a second opinion from Ninjaboi as well, he may agree of disagree) If you are looking for an easy starting language from Visual Studio, I would think that C# is more worth it than Visual Basic. There are arguments both ways however, and I am a C# programmer.

Just start a new C#/Visual Basic Windows Forms application. Drag a button onto the Form. Double click on the button. In the code, where the cursor is, place the following code:

VB:

```
Process.Start("C:\\Windows\\notepad.exe")
```
C#:

```
System.Diagnostics.Process.Start("C:\\Windows\\notepad.exe");
```
If you need Command Line arguments, or anything more complicated, just ask for more help. You can see the documentation here: Process.Start Method (System.Diagnostics)

However, if you want something slightly better, we can always make it for you, and then talk you through the code. I mean if you want a drop down of games, taken from a file you can manage, and a single start button. We can send you the solution, and code etc. etc. 

It wouldn't take us five minutes to do it. I am sure Ninjaboi would also do it, if he were online and I wasn't.

The best of luck!

Richard


----------



## Crockeo (Apr 17, 2010)

Thank you, and I will try C# along with Visual Basic, in the end I'm planning to use C++, but I haven't gotten any books, nor anything else, so I'm just playing around in Visual Basic.


----------



## niemiro (Feb 20, 2010)

Hello!

The best of luck with your learning! Try them both out, for now, but C# may help you slightly more if you are planning to go to C++. Me? I can't stand VB, but love C#. Personal preference! Best to try them both out, for now!

Richard


----------



## Crockeo (Apr 17, 2010)

(Sorry for the double post)

Now I'm wondering what the code is to close a program (so the launcher closes after I launch a game)


----------



## niemiro (Feb 20, 2010)

No problem!

VB:

```
Application.Exit()
```
C#:

```
Application.Exit();
```
Richard


----------



## Crockeo (Apr 17, 2010)

That's well.... really really simple.


----------



## niemiro (Feb 20, 2010)

Crockeo said:


> That's well.... really really simple.


For the moment! There are actually several ways to end the program, for Console Applications, for Windows Forms, dealing with Threads, etc. etc. Some require a Return Code, others not. But yes, whichever one is required, it is simple, just get the right one or nothing happens! lol The joys of ending your application!


----------



## Crockeo (Apr 17, 2010)

My last question, is how do I make the contents of a textbox be a string variable? This is so people can put what file they want to start with this, and then start it.

EDIT: I lied, this is my last question, how do I make it so what is said in the textbox is stored for next time I open my program?


----------



## niemiro (Feb 20, 2010)

Hello!

No problem at all! Your questions don't (by and large) take very long to answer, so one extra is no problem!

C#:


```
string textOfTextBox1 = textBox1.Text;
```
VB:


```
Dim textOfTextBox1 As String = TextBox1.Text
```
Now, how to save it? Slightly trickier. You could store it in the registry. You could store it in a file somewhere, an XML file, or a settings file. Settings files only seem to work if you don't change the directory!

I shall give you a taste of each one, if you give me a moment 

Richard


----------



## Crockeo (Apr 17, 2010)

Thank you again


----------



## niemiro (Feb 20, 2010)

Hello again! You are more than welcome!

C# implementations first:

*Settings file:* 

This is stored in AppData, and managed by Windows. Seems to lose it if you move your program directory. 

// button1 has a Text property of "Change". textBox1 is an editable text box with a change button, and reloads the previous save even after a program restart.

```
private void Form1_Load(object sender, EventArgs e)
        {
            // Set the text when loading the form after a program restart. Uses the "Form1_Load" Event - Events are called when things happen, this time when the Form Loads.
            textBox1.Text = Properties.Settings.Default.Test;
        }

        private void button1_Click(object sender, EventArgs e)
        {
// Save the new text on click of button - another Event
            Properties.Settings.Default.Test = textBox1.Text;
            Properties.Settings.Default.Save();
        }
```
Download the Settings Test program from here:
View attachment SettingsTest.zip
Give it a little try.

For something additional, drop on a Button, and an OpenFileDialog. Call the button Browse :grin: 

Double click on the Button:


```
if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
            }
```
Voila! Browse or typing!

Now, going back to Settings Test. Same text box and Change button, just this time though a file, and then the registry. I shall leave out XML Serialization unless you particularly want to see how it is done. 


```
using System.IO;

private void Form1_Load(object sender, EventArgs e)
        {
// Dodgy place to initialise my variable. Just for illustration.
             string AppData = System.Environment.GetEnvironmentVariable("AppData");

if (File.Exists(AppData + "\\MySettings.txt"))
{
     string data = File.ReadAllText(AppData + "\\MySettings.txt");

            // Set the text when loading the form after a program restart. Uses the "Form1_Load" Event - Events are called when things happen, this time when the Form Loads.
            textBox1.Text = data;

}
        }

        private void button1_Click(object sender, EventArgs e)
        {
// Save the new text on click of button - another Event
// Dodgy place to initialise my variable. Just for illustration.
             string AppData = System.Environment.GetEnvironmentVariable("AppData");
            File.WriteAllText(AppData + "\\MySettings.txt", textBox1.Text);

        }
```
NOTE! There probably will be slight mistakes, as I am doing this without the compiler checking my syntax!

Registry!


```
using Microsoft.Win32;

// Slip this into the previous events:

RegistryKey RegistryKey = Registry.CurrentUser;

if(RegistryKey.OpenSubKey("SOFTWARE\\TestApp") == null)
{
     RegistryKey.CreateSubKey("SOFTWARE\\TestApp");
}

RegistryKey.OpenSubKey("SOFTWARE\\TestApp", true);

// Then use these gets and sets:

if (RegistryKey.GetValue("setting") == null)
{
RegistryKey.SetValue("setting", "Default");
}
else
{
textBox1.Text = RegistryKey.GetValue("setting");
}

RegistryKey.SetValue("setting", data);
```
Is that enough to get started?

The best of luck!

Richard


----------



## Crockeo (Apr 17, 2010)

Right now I'm lost in a dark forest of saving and loading, but I think I'll figure it out.


----------



## niemiro (Feb 20, 2010)

Crockeo said:


> Right now I'm lost in a dark forest of saving and loading, but I think I'll figure it out.


lol Oh dear! How is it going? Do you think that registry would be the best? I can go through the registry stuff in detail, if you want, in both VB and C#.

Richard


----------



## Crockeo (Apr 17, 2010)

I think I found out what I needed to do with saving, but how would I make a "Browse" button, like in some other programs, and in most installations. (What I need is how to browse, and how to put it into the text box)


----------



## Dblanchard1278 (Jun 18, 2009)

I believe it's the openfile dialog that you need the code to link it is OpenFileDialog1.ShowDialog if there are more then one dialog then the number will have to be changed. there are you tube video's that can help you out some, sorry I can't provive more info but I'm still learning at this time.


----------



## niemiro (Feb 20, 2010)

Hello again!

On your Form, you need to drop on a Button and an OpenFileDialog. In the properties window, change the Button's TEXT to Browse. Double click on the button, and where the cursor jumps to, paste in this code:

C# Code:
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}

Done! I hope this helps!

Richard


----------



## Crockeo (Apr 17, 2010)

Do you know the VB code or just the C# code. As my whole program is written in VB.


----------



## Crockeo (Apr 17, 2010)

Wait, never mind I already got this part done. But can you try to explain the saving more, because I tried to do it and I kinda failed...


----------



## Crockeo (Apr 17, 2010)

Is it possible to do a command like,
C:\Program Files (x86)\Java\jre6\bin\javaw.exe Xmx1024M Xms1024M -jar bukkit.jar from visual studio (so I could press a button and it would do that)


----------



## niemiro (Feb 20, 2010)

Hello!

No problem! I shall now do it all in VB! 

To start that process, put a comma, and then a string containing the arguments.


```
Process.Start("C:\\Program Files (x86)\\Java\\jre6\\bin\\javaw.exe", "Xmx1024M Xms1024M -jar bukkit.jar")
```
How are you doing on choosing a language and getting a book? I can do the registry stuff again, but it is really quite advanced, and it is difficult to explain where to put which bits of code when you can't tell the difference between different types of lines, because I can't really re-write all of the first chapters. However, I shall do my best in the time I have!

To make an Event Work, go to your Form (drag-and-drop) look. Select your Form, and not a button etc. On the right hand side, look for the Properties Panel. At the top of this pane, should be five icons. Single click on the Lightning Bolt one. To make an event work, double click in the empty box next to the appropriate event. This will deal with the Event Handler for you. Don't worry about Event Handlers at the moment - far too advanced  Set up the LOAD and FORMCLOSING events now.

Assuming that you are trying to save the contents of TextBox1, do this. 

In your FormClosing Event, put this:


```
SaveSetting("ApplicationName - Change this, but keep it consistent", "Section1", "TextBox1", TextBox1.Text)
```
In your FormLoad event, put this:


```
If GetSetting("ApplicationName - Change this, but keep it consistent", "Section1", "TextBox1") <> "" Then
            TextBox1.Text = GetSetting("ApplicationName - Change this, but keep it consistent", "Section1", "TextBox1")
        End If
```
Now all should work. I can't really explain this without a bit of background. I am now using a really simple and rubbishy method of accessing the registry. 

I am just calling a Method. You can see what I am proving it with here: GetSetting Function and here: SaveSetting Function

The best of luck!

Richard

P.S. I haven't played with VB for years, and had kind of forgotten it. I now remember every reason I hated it so! Four lines of code, and I hated it so much, once again!


----------



## Crockeo (Apr 17, 2010)

Thank you very much!


----------



## niemiro (Feb 20, 2010)

You are more than welcome! 

The best of luck with this! I have actually thought of something else. If you know it already, great, but if you don't, learn it from below!

You may be wondering why I double up all of my back slashes in paths. This is due to something called escape characters.

Assume that you want to write a string on two lines.

One can either do:


```
Console.WriteLine("Test");
Console.WriteLine("Test2");
```
Which will output like this:


```
Test
Test2
```
But this isn't very neat. We can add a line break with \n. All \n are changed to new lines, \t is changed to tab, etc. etc. These are called escape characters.


```
Console.WriteLine("Test\nTest2");
```
goes to the same:


```
Test
Test2
```
Now let us assume this path:


```
"C:\new.exe"
```
This would go to:


```
C:
ew.exe
```
Do you now see the problem!

So, how do we avoid that! Well, there actually two ways. Either, we can double up the back slashes, or we can put an @ symbol before the string, to make escape characters be ignored:


```
\t --> TAB
\n --> NEW LINE
\\ --> \
```
So, using that, "C:\\new.exe" --> "C:\new.exe" Perfect!

However, we could also tell it to ignore escape characters completely:

@"C:\new.exe" -- > "C:\new.exe"

Notice the @ symbol before the first quote?

Last little bit, what happens if you want to put a double quotation marks in your string? 

"I like "Fish" " This will go to:

"I like" + 'Fish' is not recognised in the current context and build error + " "

Not very helpful! Let me add to this:


```
\t --> TAB
\n --> NEW LINE
\\ --> \
\" --> "
```
So:


```
"I like this sort of smilie: \"\\n\""
```
See if you can figure out what that goes to!

Answer in white: I like this sort of smilie: "\n"

Now, go and put that into a compiler, and see if my answer is correct. Look at it again. Depending on the order that a compiler prioritises escape characters, it could get a different answer. I am theoretising, left to right, and could actually be wrong because I can't check it at my work place here.

Hopefully this helps a little!

The best of luck, once again!

Richard


----------



## Crockeo (Apr 17, 2010)

I haven't posted in a while, but I have a question again, this time for a separate program from the press of a button.

Context: I'm making an application that will start, end, and restart an external program (it's being made for server softwares). Along with features like browsing and keeping the location of the file, all of the things you taught me.


----------



## Crockeo (Apr 17, 2010)

"I haven't posted in a while, but I have a question again, this time for a separate program*, how would I close an external program* from the press of a button"

That's what I meant.


----------



## niemiro (Feb 20, 2010)

Hello!



> It's being made for server softwares


Server manager? No wonder you are picking it up so swiftly, if you are, but you are picking it up swiftly anyway, server manager or not!

No problem! But again, this one is slightly trickier to answer. 

Killing a process is quite a violent way of killing an application. Is this how you want to do it, or we could opt to the less violent CloseMainWindow() which allows a program to ask for user input (Do you want to save before exiting?) or even refuse to close, as opposed to Process.Kill()

Try ending Applications and Processes via Task Manager. If you get the Application is Not responding, click Cancel and not End Task to get a more realistic feel - that would be ending the process. 

Also, are we trying to end the same process started earlier, or not. This makes a whole world of difference. We could also end the first occurrence, or all occurrences of the process. All seems like the best choice; better than the pretty much random "first".

What happens if I call my .exe firefox.exe, and try to end my process, but all of them? 

Do you see where the issue is coming from? We need to discuss this more, to work out how we are actually going to perform an exit. And now the tricky side of programming comes out (forgetting Physics engines in C++ and DirectX!)

Thanks!

Richard


----------



## niemiro (Feb 20, 2010)

P.S. Just gave it a quick try. Escape characters do definitely go left to right in C# (and presumably many other languages) so my prediction was absolutly correct:


```
I like this sort of smilie: "\n"
Press any key to continue . . .
```
from:


```
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace EscapeCharactersTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("I like this sort of smilie: \"\\n\"");
        }
    }
}
```
Richard


----------



## Crockeo (Apr 17, 2010)

Well, what I'm wondering is if there is a simple way to just close a specific program, I will be opening it from the server manager if that makes any difference. (Also I learned that the server process name and the game process name are both javaw.exe (as they are written in java))

EDIT: I would like something abrupt not like a slow end application in the application menu, more like a process ending. Oh, and is there a way I can push a command into the server console? As it has a command to shut the server down.


----------



## Dblanchard1278 (Jun 18, 2009)

When I'm using vs 2008 or 2010 I've been using this Me.Close() for mine, I havent been able to find a lot of places that will help with learning vb code except the vids on youtube. The info is out there but you just have to look and try diffrent ways to ask or search for it.


----------



## niemiro (Feb 20, 2010)

Hello!

Apologies for the delay. Long story! Got into a battle with some hackers on my server; who ended up not being hackers! Bug in one of my auto updating applications - recursion! OMG! Horrific bug with a lot of mess to clean up! 

Right, we can do a Process.Kill() happily enough. If I kill all javaw.exe processes, we are going to run into problems. Do you know anything individual about it? If we get desperate, we could try something like MD5, but that will take a fair bit of code.

Thanks!

Richard


----------



## Crockeo (Apr 17, 2010)

Well, I could start a batch file that would do what I wanted to do, do you know if I would be able to kill the batch file itself?


----------



## niemiro (Feb 20, 2010)

Hello!

Really sorry for the delay. Could you guarantee the path? Don't tell me the path, but could you guarantee it, or not?

Thanks!

Richard


----------

