Checking if certain file\files exist - c#

I want to make a windows form using C# to check if a file exist.
I've tried this one:
private void test_Click(object sender, EventArgs e)
{
var app1 = (#"C:\Users\frangon\Desktop\Spectrum-Check.EXE");
test.Text = File.Exists(app1).ToString();
}
If possible, I don't want to click it. I just want it to show as "True" if the file exist, or "False" if the file doesn't exist.

You can add an event to trigger when the form loads:
And then in your code behind:
private void Form1_Load(object sender, EventArgs e)
{
string app1 = #"C:\Users\frangon\Desktop\Spectrum-Check.EXE";
test.Text = File.Exists(app1).ToString();
}

Related

On button1 click create variable and on button2 click use that variable created in the button1 | C# WPF

I have the following button1 Click method:
private void button1_Click(object sender, RoutedEventArgs e)
{
string createstringvalue = MyCustomMethod();
}
When the user clicks the button1 it generates a custom string.
I want to use this custom string as an argument in a custom method used by the button2 Click method.
private void button2_Click(object sender, RoutedEventArgs e)
{
bool commandExecuted = CustomMethod2(createstringvalue);
if (commandExecuted)
{
MessageBox.Show("Selected file was loaded successfully");
}
}
Currently I get an ERROR:
The name createstringvalue does not exist in the current context
I apologize if my question is quite simple. I try to learn C# , so I try different concepts.
Please mark the question as duplicate, and I will close it, if it is already answered and post in the comments the duplicate SO question.
You can make a variable outside your click event as Loocid mentioned, or you can directly read the value in button2:
private void button2_Click(object sender, RoutedEventArgs e)
{
bool commandExecuted = CustomMethod2(MyCustomMethod());
if (commandExecuted) //or: if (CustomMethod2(MyCustomMethod()))
{
MessageBox.Show("Selected file was loaded successfully");
}
}
At the top of the class string createstringvalue; Define the variable as
later
private void button1_Click(object sender, RoutedEventArgs e)
{
createstringvalue = MyCustomMethod();
}

Edit Items in a ListBox

I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.

Remeber Me C# app setting

I Create login form and want to put "remember me" check box on it.
But every time i open program it doesn't change.
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
Project.Properties.Settings.Default.rememberMe = true;
Project.Properties.Settings.Default.Save();
}
else
{
Project.Properties.Settings.Default.rememberMe = false;
Project.Properties.Settings.Default.Save();
}
}
Also i want to save user login information, should i save them in app setting just like remember me setting or there is better way?
You're saving the settings, but you need to retrieve those settings too.
Subscribe to the Form's load event and set the value of the CheckBox.
private void Form1_Load(object sender, EventArgs e)
{
checkBox1.Checked = Project.Properties.Settings.Default.rememberMe;
}
Also, and this is just common practice, but your code could be shorter:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Project.Properties.Settings.Default.rememberMe = checkBox1.Checked;
Project.Properties.Settings.Default.Save();
}

Transfer a method to another method

I'm new here and I have a doubt. It is possible to transfer a method to another method?
private void button1_Click(object sender, EventArgs e)
{
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
private void button2_Click(object sender, EventArgs e)
{
// c_Click ????? ------> button1_Click
}
By clicking on Button2, via code is included c_Click the content within the button1.
Finally, clicking on button1, I need to bring up the "Transfer OK" message. Is this possible?
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Default Message");
c.PerformClick();
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
Button.PerformClick Method used to call button click event in any method
I think you can use something like this inside your button2_Click:
button1.PerformClick();
or
c_Click(sender, e);
private void button1_Click(object sender, EventArgs e)
{
c_Click(sender, e);
}
I honestly don't know if I understand your question but based on your comments:
I do not want to run "PerformClick ()" or anything similar. Basically,
it would delete the content of the button1_Click and include C_Click
content within the button1_Click.
Clicking on button1, I need to bring up the "Transfer OK" message.
Button2 will have code that will perform the deletion of button1
content and will include the contents of C_Click event. I guess it's
something using "Delegates" or similar.
You're probably not using the word "Content" in the Windows Forms sense of the word. What I get from that is that when you click button2, you want button1 to start acting like the c_Click button, correct?
If I get you correctly you need to simply remove button1's EventHandler for button1_Click and replace it with c_Click, like so:
private void button1_Click(object sender, EventArgs e)
{
}
private void c_Click(object sender, EventArgs e)
{
MessageBox.Show("Transfer OK!!!");
}
private void button2_Click(object sender, EventArgs e)
{
// c_Click ????? ------> button1_Click
button1.Click -= new EventHandler(button1_Click);
button1.Click +=new EventHandler(c_Click);
}
So that after you click button2, the next time you click on button1, it will do c_Click() instead of button1_Click()

in c#, how can you disable a button after the 1st click, and enable it again after you click another button?

I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}

Categories

Resources