Starting project in same solution with buttonClickEvent/Closing current c# - c#

What i want to achieve is this: I have a solution with 3 projects inside. I've added these projects to the explorer folder, solution explorer and made a reference from projectA to projectB & projectC.
Now what I want is this, when I click a button on a form of projectA (the first project the user sees when he opens the EXE) like this:
private void projectbButton_Click(object sender, EventArgs e)
{
this.Hide();
projectB.form1 li = new projectB.form1 ();
li.Show();
}
that projectB form opens up.
This works but then the problem is that my other project is just hidden. Now is it possible to add something here that my projectA is fully closed and my projectB starts indepently. So that all the paths etc that are declared in my projectB forms like this:
inputStream = File.OpenText("gevaar17.txt");
are still valid. (because now I get a filenotfoundexeption since it's looking in the debug folder of projectA). And that when I search for a textfile in projectB form1 for example it doesn't go looking in projectA/bin/debug folder for the textfile. But that it goes looking in the debug folder of projectB?
thanks in advance.

private void projectbButton_Click(object sender, EventArgs e)
{
Process.Start("Project2.exe");
Application.Exit();
}
This will start the other project in a completely independent environment and the paths will work. Also, Process.Start() returns Process which you can use to do something if you want, just a tip. Add this to the usings:
using System.Diagnostics;

Related

How Do You Edit a Second (Non-Main) C# Form Window In Visual Studio Designer?

I am writing a C# Windows Forms program in Visual Studio. I have a button that creates and shows (opens) a new Form (window) called VideoWindow. I can edit the MainWindow in the Design workspace in Visual Studio which allows me to visually edit its contents. However, I can't find a way to do the same thing with the VideoWindow. I have tried right clicking on VideoWindow and clicking View Designer, but it just takes me to the MainWindow designer. How do I open the designer for the second VideoWindow? Is this possible? Below is the code that creates and opens the new form:
private void ButtonWindow(object sender, EventArgs e)
{
Form VideoWindow = new Form();
VideoWindow.Size = new Size(500, 300);
VideoWindow.Show();
}
Edit: I know you can (and usually should) access the Designer when you create a form through the Visual Studio wizard via Project -> Add Form. However my question was for if you manually write a form class like NewForm.cs. In that case there would be no auto-generated NewForm.Designer.cs file.
You can customize a new form, and then create the corresponding object after modification. Here are the relevant steps:
1.Create a new form videoform
2.Relevant code:
.Show(); and .ShowDialog();
Note the difference between the two.
private void button1_Click(object sender, EventArgs e)
{
VideoWindow videoWindow = new VideoWindow();
videoWindow.Show();
//videoWindow.ShowDialog();
}
3.Ouput:
After some testing I found that it is possible to access the Visual Studio GUI-based Designer for a form class that was created manually (i.e. not created through the usual Project -> Add Form wizard).
If you create a CustomForm.cs file in your project and, importantly, have that class inherit from Form (System.Windows.Forms), then hit Shift F7, Visual Studio will make a GUI Designer for that form.
It will also automatically create the InitializeComonent method in CustomForm.cs and add the SuspendLayout and ResumeLayout calls and Client size, and Name properties in that method.
In this arrangement, the designer elements will not be separated from the class definitions in a separate form.Designer.cs file like usual. Any change through the Designer GUI will directly effect CustomForm.cs. Of course this is less than ideal because there's a higher likelihood of a developer breaking the form since the auto-generated code is mixed in with the manually written code.

C# Embedded Audio Not Playing

I have embedded an audio file (high.wav) in the resx file for my Windows Form (by just double clicking on Form1.resx in solution explorer, Ctrl+F4, then clicking 'Add Resource'), but using
Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.ResourceManager.GetStream("high");
SoundPlayer sp = new SoundPlayer(embeddedfile);
sp.Play();
only plays a system sound, not the embedded one. I have tried all of the combinations of changing the Persistence from 'Linked at compile time' to 'Embedded in .resx', and Build Action from 'None' to 'Embedded Resource'. I have also tried
Stream embeddedfile = WindowsFormsApplication2.Properties.Resources.high;
which doesn't even compile, saying that 'WindowsFormsApplication2.Properties.Resources' does not contain a definition for 'high'
Sorry is this is a dumb question, I am just starting out. If it makes any difference, I am using Windows 8.1 N.
EDIT: I guess I fixed it. Apparently you cannot add resources by going (formname).cs --> (formname).resx . You must go right click the application name, go to properties, and on the sidebar thing go to resources. I don't know what the difference is, as things added the way that works shows up in the place where it doesn't work to add it.
For adding resource right click on the project name from project explorer then click on resources after add resource arrow ==> add Existing file from anyware
and this is code for playing this file added:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Media;
namespace play_Wav
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
SoundPlayer sp = new SoundPlayer(play_Wav.Properties.Resources.myFile);
sp.Play();
}
}
}

Textfiles can't be found when making reference to other projects

Got another problem. I have a solution "solution1" in this solution are 3 projects "projectA" , "projectB", "projectC". I have made a reference to projectB & projectC from projectA. I have added the 2 other projects to my solution folder which contains my projectA already.
I've added them in solution explorer and made a reference as I said.
Now when I click a button on a form from projectA:
private void formProjectAButton_Click(object sender, EventArgs e)
{
this.Hide();
projectB.Form1 fs = new projectB.Form1();
fs.Show();
}
the form from projectB shows no problem. But then when I use the form from projectB (which contains a picturebox and a textbox. The picturebox just contains a static image which is located in my bin/debug folder. The textbox contains text that I load when the picturebox is clicked):
private void picturebox1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
inputStream = File.OpenText("gebod1.txt");
string line = inputStream.ReadLine();
while (line != null)
{
textBox1.AppendText(line);
line = inputStream.ReadLine();
}
textBox1.Visible = true;
}
But now when I click the picturebox to load the text from the textfile (which is in projectB debug folder) into the textbox I get an filenotfoundexeption.
Anything I can do to fix this?
Thanks in advance.
You're using a relative path. The relative path is the current working folder.
So if you have a Project A in C:\Foo\ProjectA and Project B in C:\Foo\ProjectB, your text file in project A will be in C:\Foo\ProjectA\bin\Debug\foo.txt. You'll need to navigate to the correct folder relative to your working folder, or specify a full path.
For example, you could try something like this. Obviously, this is just an example to give you an idea of how you accomplish what you're after.
File.OpenText("..\..\..\ProjectA\bin\Debug\gebod1.txt");
The .. in this context means "up one folder in the tree". So if you're in C:\Foo\Bar, a relative path of ..\Baz would mean C:\Foo\Baz\
Another option would be to add a post-build step to ProjectA to copy the file to a known folder, and then reference the file out of that location.

No Image display in c#

I created a simple windows form application C#, that is supposed to show a picture. I am following a tutorial from here Following is the code of form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void pictureBox1_Click(object sender, EventArgs e)
{
// Construct an image object from a file in the local directory.
// ... This file must exist in the solution.
Image image = Image.FromFile("Picture1.png");
// Set the PictureBox image property to this image.
// ... Then, adjust its height and width properties.
pictureBox1.Image = image;
pictureBox1.Height = image.Height;
pictureBox1.Width = image.Width;
}
}
The image is present in the solution folder, but still nothing is displayed in the window when the application is run. There is no compilation error.
Update
Its solved now.
Make sure the PictureBox click event handler is registrated. It is not sufficient to copy the example code. (I'm just guessing)
Without a full path, the image won't load from the solution directory.. it will load from the directory where your executable is executed from.. which is either the Debug or Release folders when you're running from Visual Studio.. depending on what Profile you're running it with.
So put the image in the /bin/Debug folder and run your program again.
A better option would be to add this image to the project and do either of the two:
Add as a resource. It can later be used with strong typing.
Set this option on the file: Copy to Output Directory = Copy if newer.
It is not a good practice to add anything manually under /bin/, because those folders are volatile and can be erased by Visual Studio at any moment (usually on the next rebuild).

Alternative to TextViewCreated such as (TextViewChanged)?

I am creating a small Visual Studio 2010 extension in C# which uses the IWpfTextViewCreationListener and TextViewCreated to capture when a new TextView is opened in the VS environment. The problem I am having is that this method only fires when a new window is opened via the VS Solution Explorer window, and not fired when VS already contains opened windows when started, and switching window tabs. I have tried looking for something like TextViewChanged, but could not find such method. Is there anyway to capture the new TextView when another tabbed window is selected?
Any help would be greatly appreciated.
This question has also been posted on the MSDN VS Extensibility forum:
VSX 2010 - Alternative to TextViewCreated such as (TextViewChanged)?
Thanks
John
There is no TextViewCreated, but if you register to IWpfTextView.GotAggregateFocus as it is created, you get a hook to every switch between files:
public void TextViewCreated(IWpfTextView textView)
{
textView.GotAggregateFocus += TextViewCameToFocus;
// Do stuff when a new IWpfTextView is created...
}
void TextViewCameToFocus(object sender, EventArgs e)
{
var focusedTextView = (IWpfTextView)sender;
// Do stuff when a specific IWpfTextView comes to focus...
}
You may also want to keep track of the IWpfTextView objects, if you want to be able to link between the fired events to your logic of each textview.

Categories