How can i use Function MenuItem in Function buttonCreate_Click - c#

I am relatively new in C#. My question is: I have created a MenuStrip. I want to create with ButtonCreate_Click a Folder under the Directory path. So how can use the path in the Function buttonCreate? Is that possible?
private void buttonCreate_Click(object sender, EventArgs e)
{
string MyFileName = "Car.txt";
string newPath1 = Path.Combine(patheto, MyFileName);
//Create a folder in the active Directory
Directory.CreateDirectory(newPath1);
}
private void DirectoryPathToolStripMenuItem_Click(object sender, EventArgs e)
{
string patheto = #"C:\temp";
Process.Start(patheto);
}

Because you are declaring patheto in the menu items click event it is only a local variable to that scope. If you create a property for your form, then that property can be used within the forms scope. Something like this:
private string patheto = #"C:\temp";
private void buttonCreate_Click(object sender, EventArgs e)
{
string MyFileName = "Car.txt";
string newPath1 = Path.Combine(patheto, MyFileName);
// Create a folder in the active Directory
Directory.CreateDirectory(newPath1);
}
private void DirectoryPathToolStripMenuItem_Click(object sender, EventArgs e)
{
Process.Start(patheto);
}
This means that your variable patheto can be accessed anywhere within the form. What you have to remember is that wherever you declare your variables, they will only be accessible in that function/class or child functions/methods.

Related

Change Path of OpenFileDialog

At the moment I am using this here in my WPF App which works as it should.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.ShowDialog();
}
It remembers the last path I was in but I want to change it to a set path now.
I have 3 Radiobuttons and each Radiobutton should lead to a different path so I thought about doing it with a variable I give to the openFileDialog but I am not sure on how to go with that. Has anyone done this and can give me a lead on it ?
You can set IntitialDirectory to the folder you want in code where you show the dialog.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.IntitialDirectory = youFolderPath;
openFileDialogPresentations.ShowDialog();
}
The standard file dialogs have an InitialDirectory property that determines in which folder the dialog opens.
private void buttonPresentations_Click(object sender, EventArgs e)
{
openFileDialogPresentations.InitialDirectory = #"X:\Data\Presentations";
openFileDialogPresentations.ShowDialog();
}
You can do it by using InitialDirectory property. You can set three different paths for radio buttons
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
dialog.InitialDirectory=#"D:\MyDir";
dialog.ShowDialog();
}

Trying to pass a string between C# classes

I'm trying to pass a string between C# classes by using a session. I have a class called Profile and it has a search box. I have another class called SearchResults that is supposed to search my database for whatever was entered in the search box.
Upon clicking on the search button, this method in the Profile class is called:
protected void Search(object sender, EventArgs e)
{
Response.Redirect("~/SearchResults.aspx");
String searchedItem = txt_search.Text;
Session["search"] = searchedItem;
}
and here is the Page_Load method in the SearchResults page:
protected void Page_Load(object sender, EventArgs e)
{
string searchedItem = (string)(Session["search"]);
string connStr = ConfigurationManager.ConnectionStrings["MyDbConn"].ToString();
SqlConnection conn = new SqlConnection(connStr);
SqlCommand SearchGames = new SqlCommand("SearchGames", conn);
SearchGames.CommandType = CommandType.StoredProcedure;
SearchGames.Parameters.Add(new SqlParameter("#game_name", searchedItem));
conn.Open();
SqlDataReader rdr = SearchGames.ExecuteReader(CommandBehavior.CloseConnection);
}
I'm getting an error saying that my
SearchGames procedure needs an #game_name parameter which wasn't
supplied
, however it's clear that I've passed that parameter, which leads me think that there's something wrong with SearchItem (or how I'm passing the string between the two classes.
You redirect before setting the session, I think you should redirect after setting session:
String searchedItem = txt_search.Text;
Session["search"] = searchedItem;
Response.Redirect("~/SearchResults.aspx");
Rather than trying to pass the parameter via the session, inject it into the redirect:
protected void Search(object sender, EventArgs e)
{
Response.Redirect($"~/SearchResults.aspx?search={txt_search.Text}");
}
or, if not using C# 6,
protected void Search(object sender, EventArgs e)
{
Response.Redirect("~/SearchResults.aspx?search=" + txt_search.Text);
}
Then within Page_Load, change it to:
protected void Page_Load(object sender, EventArgs e)
{
string searchedItem = Request.QueryString["search"];
...
That way, you avoid passing values through global variables, which will make the code more robust and make testing easier.

Opening outside files in C# WinForms

So basically what I'm trying to accomplish is being able to select a file from a displayed list and open that file. Right now I have it set up in a CheckBoxList that displays the .docx, .mov, and .txt files that exist in the selected folder. The problem is I can't get it to open the file. I've seen most people suggesting-
Process.Start(filename);
But the problem with that is that it requires a specific file name and I'm trying to pull that name from a variable. Any ideas?
Here's my current code -
private void Form1_Load(object sender, EventArgs e)
{
const string path = #"C:\Users\Haxelle\Documents\Journal";
List<string> extensions = new List<string> { "DOCX", "MOV", "TXT" };
string[] files = GetFilesWithExtensions(path, extensions);
ckbEntry.Items.AddRange(files);
}
private string[] GetFilesWithExtensions(string path, List<string> extensions)
{
string[] allFilesInFolder = Directory.GetFiles(path);
return allFilesInFolder.Where(f => extensions.Contains(f.ToUpper().Split('.').Last())).ToArray();
}
private void btnOpen_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = ckbEntry.CheckedItems;
}
Trying to open file in btnOpen_Click
It seems like all you are missing is iterating over the selected files names and opening them. Since the CheckedItemCollection.Item is typed as object, you will need to cast the items, which can be done using LINQ's Cast function.
private void btnOpen_Click(object sender, EventArgs e)
{
CheckedListBox.CheckedItemCollection selectedFiles = ckbEntry.CheckedItems;
foreach (var filename in selectedFiles.Cast<string>()) {
Process.Start(filename);
}
}

Delete Selected Text in Richtextbox

I have a richtextbox and i want to delete not cut the selected when the user presses a button.
I have used
private void button1_Click(object sender, EventArgs e)
{
SendKeys.Send("DELETE");
}
This works but i want to know another way to do it.
I have tried
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText.Replace(richTextBox1.SelectedText, "");
}
This doesn't perform any action.
Pls what can i do?
Just do this:
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.SelectedText = "";
}
Your code doesn't work because the string is immutable, you can't change the richTextBox1.SelectedText that way. All the methods (Replace, Insert, ...) performed on a string will create a new string. This new string will be used to initialize your string variable if you need.
The following line of code works for me:
SendKeys.Send("{DELETE}");
Click Link to visit the Official documentation on SendKeys methods.

SplitterLocation doesn't save in the settings

i have created a splitterLocation setting, Type system.Drawing.Point and Scope : User. to save the splitter location when the user change it. and load again with the new location when the form load.
private void splitter1_LocationChanged(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
private void Form1_Load(object sender, EventArgs e)
{
splitter1.Location = MailSystem.Properties.Settings.Default.splitterLocation;
}
but it doesnt work i dont know why ?.
Try moving the saving code to the FormClosing event.
private void Form1_FormClosing(object sender, EventArgs e)
{
MailSystem.Properties.Settings.Default.splitterLocation = splitter1.Location;
MailSystem.Properties.Settings.Default.Save();
}
and make sure your splitter1 control isn't being docked, DockStyle.None.

Categories