Enabling textbox on button click event - c#

How can i add the mechanism in my downloader for the case although many threads on SO deals either with php etc and not upto the need.
I have a browse button at the front of a textbox where i get's the user entered Path on local drive to fix the location for downloading but i have already hardcoded one for system drive.I want textbox button to be disabled unless user clciks browse button and then new path can be entered for downloading after then.
How can i go?

A quick example:
private void browseButton_Click(object sender, EventArgs e)
{
var saveFileDialog = new System.Windows.Forms.SaveFileDialog();
var selectedPath= saveFileDialog.ShowDialog();
if (selectedPath == System.Windows.Forms.DialogResult.OK)
{
_savePath = saveFileDialog.FileName;
textBox1.Enabled = true;
textBox1.Text = _savePath;
}
}

Related

C# Windows Form Application, button name with readline

I want to create button that is named by the user.
This is what I have (sorry I'm so bad at this but I am trying :D)
string 1;
private void button1_Click(object sender, EventArgs e)
{
if (1 = null)
{
Console.Write("Give Button's name");
1= Console.ReadLine();
button1.Name = 1;
}
}
I also want the button to open a file path selected by the user. Is there a way to do that?
(sorry for my stupid question and anything else... :D)
Based on yr comments:
1. Use a TextBox(called txtUserInput) to get the user's input
2. Use OpenFileDialog to help user to choose a file
private void button1_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(txtUserInput.Text))
{
button1.Text = txtUserInput.Text;
var openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
MessageBox.Show("You chose: " + openFileDialog.FileName);
}
}
}
You can't mix Console Application input with Windows Forms input. You need to decide what you want. From the comments, it looks like you want a Windows Form Application so you will need some way to get user input like a text box.
On your form you will need to create a text box and give it a name like tbxUserInput. After that you can change you click method to do something like this:
private void button1_Click(object sender, EventArgs e)
{
button1.Text = tbxUserInput.Text
}
One thing that was wrong was that you were assigning the Name property of the button and not the Text property. The text property is what is actually displayed on the button.
It may be a good idea to read some tutorials about Windows Forms.

open file dialog keeps on showing even after choosing file c#

I'm working with window forms on c# I'm trying to open a file using openfile dialog when I browse to my file and open it the open file dialog keeps on showing many times .
That's my code for opening a file :
private void OpenBtn_Click(object sender, EventArgs e)
{
// Create OpenFileDialog
OpenFileDialog dlg = new OpenFileDialog();
// Set filter for file extension and default file extension
dlg.DefaultExt = ".xml";
dlg.Filter = "XML Files (*.xml)|*.xml";
// Display OpenFileDialog by calling ShowDialog method
DialogResult result = dlg.ShowDialog();
if (result == DialogResult.OK)
{
pathtext.Text = dlg.FileName;
sourceName = dlg.FileName;
}
// destFile = resultFile.Name;
if (pathtext.Text != null)
{
createBtn.Enabled = true;
}
}
and this event handler of the method in the form load
OpenBtn.Click += new EventHandler(this.OpenBtn_Click);
I can't see where did I miss the thing.
The only way I can reproduce your bug is when I double click the button in the designer so that it creates an automatic event handler which you can see in the event properties:
If I then in addition to that add inside the code a manual registration of the event Click for example in the Load event:
private void Form1_Load(object sender, EventArgs e)
{
button2.Click += new EventHandler(this.OpenBtn_Click);
}
Then I will get the behaviour that the dialog pops up twice. If I do it one more time:
private void Form1_Load(object sender, EventArgs e)
{
button2.Click += new EventHandler(this.OpenBtn_Click);
button2.Click += new EventHandler(this.OpenBtn_Click);
}
It will pop up 3 times! It is very likely that you register this event in a loop. So when the first one is executed all others just follow up. Remove the manual registering line and put the event handler name simply into the event properties.
EDIT: The main problem is the operator += it adds the delegates to an internal list as described in this answer.

Prevent user from browsing elsewhere in an OpenFileDialog

What I need to do is prevent users from browsing anywhere else then where I declare them to browse.
for example I have: ofd.InitialDirectory = Location; on button click.
Location for example being C:\Users\Chris\Pictures.
The thing is that I know that some users will try be clever and might go selecting things outside that folder I declare. Is there any way to prevent that from happening?
Possible workaround:
Just an if statement checking if the location of the selected file starts with the same Location I start them off in.
OpenFileDialog ofd = new OpenFileDialog();
private void button1_Click(object sender, EventArgs e)
{
string newPath = #"C:\Users\Chris\Pictures";
ofd.InitialDirectory = Location;
if (ofd.ShowDialog() == DialogResult.OK)
{
if (ofd.FileName.Contains(Location))
{
textBox1.Text = ofd.FileName;
}
else
{
MessageBox.Show("Please select a file that is located in the specified location (" + Location + ")"
, "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
You can't prevent users from navigating to another folder, you have to build an own OpenFileDialog if you want to do that. You can prevent though that the file isn't accepted.
You have to handle the FileOk event for that. You can check if the parent directory is the one you want it to be.
Something like this:
private void openFileDialog1_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
string[] files = openFileDialog1.FileNames;
foreach (string file in files )
{
if (Path.GetDirectoryName(file) != yourRequiredPath)
{
e.Cancel = true;
break;
}
}
}
This is not possible.
You can't restrict users to prevent them to browse elsewhere where you don't wish to.
One possible way to fix this is to write your custom open dialog box with custom validations.
If you really want to prevent navigation in the file dialog the only way to do it would be to use the Common Item Dialog, provided by COM1, and give it an IFileDialogEvents and implement the OnFolderChanging event. To allow navigation, return S_OK and to deny navigation return something else.
1 "Provided by COM" as in the component object model, not some third party.

How do I show a directory path in a textbox when selected by the user?

First, I am using Visual Studio 2013 and coding in C# to develop a Windows Form Application. I have added the "System.IO" namespace.
I need to show a directory path in a textbox when selected by the user.
The code works correctly to where the user selects a folder from a popup
and presses the OK button, which then displays the number of files within
that folder -- but the folder path does NOT get displayed as I desired.
Code looks like this:
private void button1_Click(object sender, EventArgs e)
{
//
// This event handler was created by clicking the button in the application GUI.
//
DialogResult button1_Click = folderBrowserDialog1.ShowDialog();
if (button1_Click == DialogResult.OK)
{
//
// The user selected a folder and pressed the OK button.
// A message pops up and identifies the number of files found within that folder.
//
string[] files = Directory.GetFiles(folderBrowserDialog1.SelectedPath);
MessageBox.Show("Files found: " + files.Length.ToString(), "Message");
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
string path;
path = folderBrowserDialog1.SelectedPath;
// folderBrowserDialog1.ShowDialog(); // NOT SURE ABOUT USING THIS!
textBox1.Text = path;
}
You could just add this to the end of your button1_Click method (inside the if block):
textBox1.Text = folderBrowserDialog1.SelectedPath;

Executing another Application with selected file using a button (C# Programming)

To start, I'm a newbie with C# Programming and I've tried googling for solutions about my problem but it seems that i cannot find one, or simply too unlucky or too blind to spot one. I'm using Microsoft Visual Studio 2005.
Anyways. I was assigned to modify/create an automated test environment input application. The said application already has a function to run/start a CANoe program with a pre-defined file or if it's already running, stop the program.
private void button1_Click(object sender, EventArgs e)
{
// Execute CANoe(Obtain CANoe application objectg)
mApp = new CANoe.Application();
mMsr = (CANoe.Measurement)mApp.Measurement;
try
{
mApp.Open("C:\\Users\\uidr3024\\Downloads\\SRLCam4T0_Validation_ControlTool\\cfg\\SVT_SRLCam4T0_025B.cfg", true, true);
}
catch (System.Exception ex)
{
System.Console.WriteLine(ex.Message);
}
}
private void button2_Click(object sender, EventArgs e)
{
// Finish CANoe
if (mApp != null) {
mApp.Quit();
}
// Release the object
fnReleaseComObject(mMsr);
fnReleaseComObject(mApp);
}
What I wanted to do now is to have an OpenFileDialog box that will show a selection of files and user will be able to browse and select any file to start the CANoe program with the selected file and not just the file path that's been input in the code along the "mApp.Open()" syntax. I've tried this:
private void button5_Click_1(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = #"C:\Users\uidr3024\Downloads\SRLCam4T0_Validation_ControlTool\cfg";
openFileDialog1.Title = "Browse Configuration Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.Filter = "CANalyzer/CANoe Configuration (*.cfg)|*.cfg |All files (*.*)|*.*";
openFileDialog1.FilterIndex = 1;
openFileDialog1.RestoreDirectory = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openFileDialog1.FileName;
}
}
I've tried this code that I often see in the Web and in the Tutorials but I don't know how to incorporate it with the button for running the CANoe program so that when user clicks the Open button from the Dialog Box, the file path would be shown in the textbox(optional) and/or when the user then clicks the Start CANoe, CANoe program would start with the selected .cfg file.
Am I making any sense here? Or am I doing the right thing here?
Btw, I found these... And I'm using a CANoe library for these, ofc.
#region "***** CANoe Object definition *****"
private CANoe.Application mApp = null; // CANoe Application CANoeƒAƒvƒŠƒP[ƒVƒ‡ƒ“
private CANoe.Measurement mMsr = null; // CANoe Mesurement function CANoe‘ª’è‹#”\
private CANoe.Variable mSysVar = null; // System variable ƒVƒXƒeƒ€•Ï”
private CANoe.Variable mSysVar_start = null; // System variable ƒVƒXƒeƒ€•Ï”
#endregion
I think you have done most of the hard work, unless I've missed something I think you just need something like this in your button1_Click method:
if( textBox1.Text != String.Empty && System.IO.File.Exists(textBox1.Text) )
{
// The textbox has a filename in it, use it
mApp.Open(textBox1.Text, true, true);
}
else
{
// The user hasn't selected a config file, launch with default
mApp.Open("C:\\Users\\uidr3024\\Downloads\\SRLCam4T0_Validation_ControlTool\\cfg\\SVT_SRLCam4T0_025B.cfg", true, true);
}

Categories