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.
Related
Hopefully this is clear enough, i have this bit of code :
private void BtnAdd_ServerClick(object sender, EventArgs e)
{
var path = #"C:\Users\User\Desktop\Demo\Demo\" + BtnName.Value;
if (!Directory.Exists(path))
{
Directory.CreateDirectory(path);
}
}
That creates a folder named after the value of a text box, and i need to know if there's a way to create an aspx file the same way, and save it in one of those folders or anywhere it just needs to be accessible to the project, thanks in advance! I'm open to any questions or code updates.
I'm trying to change the Background Pic of a Button to another Pic that I saved in the Resources Folder. Currently The Code is the following (FinalDStagePickBANNED.png being the Pic I want it to change to):
private void FinalD_Click(object sender, EventArgs e)
{
BanFDButton.Image = (Image)Properties.Resources.ResourceManager.GetObject(FinalDStagePickBANNED.png);
}
The Thing is, that it apparently finds no Pics in the Resources-Folder, although there are like 10 pics.
Solved: The solution to the problem was to put the FinalDStagePickBANNED into quatation-signs, and to remove hte .png ending.
Basically i have a list of folder paths on XML File. I created a dataGrid View which reads this XML file and displays the list of folders in the XML into the data Grid View. Now I need to create a way where the user chooses a folder path from one of the list I have and the program will check if this folder exists on the computer being used.Is there a way to create this program?
Try Button with event handler for the click event and assuming that your input textbox is named textBoxForPath this should work:
private void Button1_Click(object sender, EventArgs e)
{
string path = this.textBoxForPath.Text;
if (File.Exists(path))
{
// Do Something
}
}
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;
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).