what can i do to make my code to work - c#

i have 4 errors and im working on my save button if i could get these fixed it will only save the selected items that the user wants
THIS IS NOT all the code just the code that im having problems with. this program is for and icecream application with 2 combo boxes and 3 check boxes
I PUTT COMMENT LINES WHERE I HAVE THE ERRORS ARE AT
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = new SaveFileDialog();
if (sfd.ShowDialog() == DialogResult.OK)
{
StreamWriter sw = new StreamWriter(
new FileStream(sfd.FileName,
FileMode.Create,
FileAccess.Write)
);
if(flavorBox) // i have an error right here (Cannot implicitly convert type 'System.Windows.Forms.ComboBox' to 'boolean)
{
sw.WriteLine(flavorBox.SelectedItem);
}
else(syrupBox) //syays i need semecolons right here for some reason
{
sw.WriteLine(syrupBox.SelectedItem);
}
if (Nuts.Checked)
{
this.Tag = "checked";
sw.WriteLine(Nuts);
}
else(Cherries.Checked) //says i need semicolons here to i dont know why
{
this.Tag = "checked";
sw.WriteLine(Cherries);
}
if(Sprinkles.Checked)
{
this.Tag = "checked";
sw.WriteLine(Sprinkles);
}
sw.Close();
}
}
THIS IS MY 4TH ERROR
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want to send the data back?",
"Data Sender",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
e.Cancel() = true; //ITS ASKED ME AM I MISSING A DIRECTIVE OR ASSEMBLY REFRENCE (FOR CANCEL)
}

In an if-else only the if should have a condition, and the else should not. Use an else if statement to explicitly define a condition.
if (Nuts.Checked)
{
this.Tag = "checked";
sw.WriteLine(Nuts);
}
else if(Cherries.Checked)
{
this.Tag = "checked";
sw.WriteLine(Cherries);
}
else if(Sprinkles.Checked)
{
this.Tag = "checked";
sw.WriteLine(Sprinkles);
}
Flavorbox is a textbox, so by doing if(flavorbox) you are checking if flavorbox is equal to true or false. It's a textbox so this isn't possible. You'll probably have to just change the flavorbox. Try the following:
if(!String.IsNullOrEmpty(flavorbox.Text)) {
sw.WriteLine(flavorBox.SelectedItem);
}

1. about
if(flavorBox)
what are you checking?
about the:
else(syrupBox) and else(Cherries.Checked)
you cannot do else(something).
you can do else if(something)
because else is all the rest of the options.
so change them to:
else if(syrupBox) and else if(Cherries.Checked)
2. about the cancel:
what are you trying to do?
when you click no in the dialog, what are you trying to accomplish with the e.cancel?

For the fourth error, note that EventArgs.Cancel is a property, not a method. Remove the brackets:
e.Cancel = true;

Related

Disable NumericUpdown C#

The student is back! I am trying to self-teach C#, so please pardon my simple but many questions. I appreciate you all.
I am working on a quiz app.What I want but cant seem to achieve is that when "Testing mode" (radio button) is selected, "Number of questions" need to be grayed out.Otherwise, student can select number of questions to attempt.
Here is my code
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("Click 'Start' to continue..");
btnclose.Hide();
}
else
{
MessageBox.Show("You Must select an option to continue.");
}
}
//if testing mode, dissable number of questions ,and also the 'Close' button
Like this - see comments
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
//this event fires when rdotesting is checked or when it is unchecked (change)
//set the enabled state of the nud/button to th opposite of the checked state
//ie when checked = true then enabled = false
numberQsNUD.Enabled = !rdotesting.Checked;
closeButton.Enabled = !rdotesting.Checked;
//if not in test mode, exit to stop the message showing every time
if(!rdotesting.Checked)
return;
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
rdotesting.Checked = false; //user said no; turn off test mode
simply you can try this code...
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
numberQsNUD.Enabled = false;
closeButton.Enabled = false;
} else {
numberQsNUD.Enabled = true;
closeButton.Enabled = true;
}
}
or you can customize this via using this code...
private void EnableComponent(bool check) {
numberQsNUD.Enabled = check;
closeButton.Enabled = check;
}
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
EnableComponent(false);
} else {
EnableComponent(true);
}
}

C# Button with DialogResult Retry

I'm going to use two buttons that have a DialogResult Retry. When you push the button the winform will hide, do something and it pops-up again. I use the While method for this. But if you have two buttons with a retry this won't work unless you set one button DialogResult to Yes and do a While method. But is there a better way to do this, case switch or something?
Note this is within a Class
try
{
// Create a form to select objects.
DialogResult result = System.Windows.Forms.DialogResult.None;
while (result == DialogResult.None || result == DialogResult.Retry)
{
// Picking Objects.
if (result == DialogResult.Retry)
{
System.Windows.Forms.SaveFileDialog saveFileDialog1 = new System.Windows.Forms.SaveFileDialog();
saveFileDialog1.InitialDirectory = Convert.ToString(Environment.SpecialFolder.MyDocuments);
saveFileDialog1.FileName = "test";
saveFileDialog1.Filter = "Family Files (*.rfa)|*.rfa|All Files (*.*)|*.*";
saveFileDialog1.FilterIndex = 1;
var dialogResult = saveFileDialog1.ShowDialog();
if (dialogResult == DialogResult.OK)
{
string address = "http://www.autodesk.com/revit-basic-sample-family-2017-enu?_ga=2.28765692.1750602280.1538397390-459409917.1521646598";
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.DownloadFile(address, saveFileDialog1.FileName);
Autodesk.Revit.DB.Family family = null;
using (Transaction tx = new Transaction(doc))
{
tx.Start("Load Family");
if (doc.LoadFamily(saveFileDialog1.FileName, out family))
{
String name = family.Name;
TaskDialog.Show("Revit", "Family file " + name + " has been loaded ");
}
else
{
TaskDialog.Show("Revit", "Can't load the family file or already exists.");
}
tx.Commit();
}
}
if (dialogResult == DialogResult.Cancel)
{
}
}
// Show the dialog.
using (testForm selectionForm = new vuurenForm(commandData))
{
result = selectionForm.ShowDialog();
}
}
return Result.Succeeded;
You may set the DialogResult in the code, not in the form designer. Just double click the buttons and add something like:
private void button1_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Retry;
}
That way both buttons will have the same DialogResult.
Than the loop is OK with only checking for the DialogResult.Retry.
Exactly you can try this code to manage your DialogResult like:
switch(MessageBox.Show("Text", "Title", MessageBoxButtons.YesNo))
{
case DailogResult == DialogResult.Yes:
//Do something
case DailogResult == DialogResult.Retry:
//Do something
}
Actually for two Button objects you have to have two event-handler objects and you can set:
DialogResult = DialogResult.Retry;
In the event that you want to Retry.
You can try this:
var dialogResult = DialogResult.Retry;
while (dialogResult == DialogResult.Retry) {
try {
CheckSomething();
break;
}
catch {
if (dialogResult == DialogResult.Abort) {secondDialog.DialogResult = Retry;}
throw;
}
}
You can also use enums like below:
enum Result {Ignore, Abort,Retry};

I want to check which button was clicked

Right now I have a main form that copies a file to another directory.
I want to handle the case 'a file of the same name already exists' in a catch statement.
I want this to be done by it popping up another window asking whether to replace or keep via buttons. Then using an if statement to check which button was clicked
Current code:
catch (IOException x)
{
Copy copy = new Copy();
copy.ShowDialog();
}
Goal:
catch (IOException x)
{
Copy copy = new Copy();
copy.ShowDialog();
if (//Replace button was clicked)
do this
else if (//Keep button was clicked)
do this
}
I can't seem to find the methods that serve this purpose.
You could make your Copy dialog return the DialogResult when the button is clicked. For instance you could use DialogRsult.OK for the Replace button and the DialogResult.Cancel for the Keep button. Something like this:
When clicked Replace button within the Copy dialog you can set this within the Replace_Click event handler
this.DialogResult = DialogResult.OK;
this.Close();
set in the similar way the DialogResult.Cancel in the Keep_Click event handler
and you could call your dialog like this:
Copy copy = new Copy();
DialogResult dr = copy.ShowDialog();
if(dr == DialogResult.OK)
//Replace clicked
else if(dr == DialogResult.Cancel)
//Keep clicked
You should consider using the DialogResult class instead.
You want something like this:
catch (IOException x)
{
DialogResult dr = new DialogResult ();
dr.ShowDialog();
if (dr == DialogResult.OK)
MessageBox.Show ("File replaced.");
else if (dr == DialogResult.Cancel)
MessageBox.Show ("File kept.");
}
Do not use exceptions to handle this kind of situations.
Just test if file exists and use a simple MessageBox with YesNo buttons
sourceFile = "Your_Source_File_To_Copy";
string destFile = Path.Combine(destFolder, Path.GetFileName(sourceFile));
if(File.Exists(destFile))
{
DialogResult dr = MessageBox.Show("File already exist! Do you wish to overwrite?",
"Warning!",
MessageBoxButtons.YesNo);
if(dr == DialogResult.Yes)
// Overwrite
else
// Do something else
}
As Mr Lippert says in this answer,
Exceptions are there to
help you debug your program, not to control its flow.
Have you looked into the Form.ShowDialog method?
http://msdn.microsoft.com/en-us/library/c7ykbedk.aspx
Code Excerpt from MSDN:
public void ShowMyDialogBox()
{
Form2 testDialog = new Form2();
// Show testDialog as a modal dialog and determine if DialogResult = OK.
if (testDialog.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of testDialog's TextBox.
this.txtResult.Text = testDialog.TextBox1.Text;
}
else
{
this.txtResult.Text = "Cancelled";
}
testDialog.Dispose();
}
Check for the file existence before copying:
if (File.Exists(destFileName))
{
Copy copy = new Copy();
System.Windows.Forms.DialogResult res = copy.ShowDialog();
if (res == System.Windows.Forms.DialogResult.Yes)
File.Copy(sourceFileName, destFileName, true);
}
else
{
File.Copy(sourceFileName, destFileName);
}
Also set the DialogResult property in the Copy form appropriately.
Perhaps implementing your own result is what you are looking for.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
var result = new Form2().ShowDialog();
MessageBox.Show(result.ToString());
}
}
public partial class Form2 : Form
{
ButtonResult buttonResult;
public Form2()
{
InitializeComponent();
}
public new ButtonResult ShowDialog()
{
base.ShowDialog();
return buttonResult;
}
private void KeepButton_Click(object sender, EventArgs e)
{
buttonResult = ButtonResult.Keep;
this.Close();
}
private void ReplaceButton_Click(object sender, EventArgs e)
{
buttonResult = ButtonResult.Replace;
this.Close();
}
}
public enum ButtonResult
{
None = 0,
Keep = 1,
Replace = 2,
}

WPF Save DialogBox (for windows 64)

This is similar to older posts on this site but I keep getting an error message. I want to create a button in C # WPF that opens a dialogbox and saves a text file to be read at a later date. This code works for windows 32, but crashes on windows 64. How can I change this code to get it to work on both systems? I am a beginner at programming.
Microsoft.Win32.SaveFileDialog saveFile = new Microsoft.Win32.SaveFileDialog(); //throws error message here
private void savebutton_Click(object sender, RoutedEventArgs e)
{
saveFile.FileName = Class1.stringjobnum;
saveFile.Filter = "CCurtain (*.cur)|*.cur";
saveFile.FilterIndex = 2;
saveFile.InitialDirectory = "T:\\Tank Baffle Curtain Calculator\\SavedTanks";
saveFile.OverwritePrompt = true;
bool? result = saveFile.ShowDialog();
if (result.HasValue && result.Value)
{
clsSaveFile.s_FilePath = saveFile.FileName;
int iDotLoc = clsSaveFile.s_FilePath.LastIndexOf('.');
string strExtTest = clsSaveFile.s_FilePath.Substring(iDotLoc);
if (strExtTest != ".cur")
clsSaveFile.s_FilePath += ".cur";
FileInfo sourceFile = new FileInfo(clsSaveFile.s_FilePath);
clsSaveFile.saveFile();
}
}
You're setting an invalid FilterIndex, that might have something to do with it.
There is no 2nd filter in the filter string as written:
"CCurtain (*.cur)|*.cur"
Try setting the FilterIndex to 1 or adding another filter to the string.
You should try adding a catch around the statement to get a better idea as to what is going on.
try
{
code here
}
catch (Exception ex)
{
ex.message contains the info
}
Also, check for null:
bool? result = saveFile.ShowDialog();
if (result != null && (result.HasValue && result.Value))
{
// code
}
I would create the dialogbox IN the event. And you don't have two different filters.
private void savebutton_Click(object sender, RoutedEventArgs e)
{
Microsoft.Win32.SaveFileDialog saveFile = new Microsoft.Win32.SaveFileDialog();
saveFile.FileName = Class1.stringjobnum;
saveFile.Filter = "CCurtain|*.cur";;
saveFile.FilterIndex = 1;
saveFile.InitialDirectory = "T:\\Tank Baffle Curtain Calculator\\SavedTanks";
saveFile.OverwritePrompt = true;
// Show open file dialog box
Nullable<bool> result = saveFile.ShowDialog();
// Process open file dialog box results
if (result == true)
{
string filename = saveFile.FileName;
// are you sure you need to check the extension.
// if so extension is a a fileinfo property
}

How can I prevent an exception when I cancel an openfiledialog?

My program has a button which when clicked opens an openfiledialog to choose a picture:
private string ChoosePicture()
{
fDialog.Title = "Select Picture";
fDialog.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp; *.gif*;*.jpg";
fDialog.InitialDirectory = "C:";
fDialog.ShowDialog();
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
//returns a string for the directory
return fDialog.FileName.ToString();
}
Using a check on the dialogresult box hasn't resolved my issue either:
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
DialogResult res = fDialog.ShowDialog();
if (res == DialogResult.OK)
{
//returns a string for the directory
return fDialog.FileName.ToString();
}
return null;
The code works if I do choose a picture and complete the file selection. However if I cancel the process at any point in between I get the exception "The path is not of a legal form". I am not sure which part I imagine I could take care of this with a try-catch, however I'm not positive which part is causing the issue? If I put a try catch around the call to the ChoosePicture() method, I can at least stop it from crashing the program but the exception is still being thrown when no picture is selected in the fdialogbox.
DialogResult result = fileDialog.ShowDialog();
if (result == DialogResult.OK) {
//returns a string for the directory
return fDialog.FileName;
}
return null; //not sure what you will return if they cancel
also, FileName is already a string, so no need to use .ToString() on it
EDIT: fixed indenting
Check the dialog result and act accordingly:
private string ChoosePicture()
{
fDialog.Title = "Select Picture";
fDialog.Filter = "Image Files (*.bmp, *.gif, *.jpg)|*.bmp; *.gif*;*.jpg";
fDialog.InitialDirectory = "C:";
DialogResult res = fDialog.ShowDialog();
if(res == DialogResult.OK)
{
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
//returns a string for the directory
return fDialog.FileName.ToString();
}
else
{
return null; // or something
}
}
Test to see if a file was selected:
fDialog.ShowDialog();
if (!string.IsNullOrEmpty(fDialog.FileName))
{
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
//returns a string for the directory
return fDialog.FileName.ToString();
} else {
return String.Empty;
}
DialogResult dresult=fDialog.ShowDialog();
Check if dresult==DialogResult.Ok and only after proceed with file operations.
fDialog.AddExtension = true;
fDialog.CheckFileExists = true;
fDialog.CheckPathExists = true;
DialogResult res = fDialog.ShowDialog();
if (res == DialogResult.OK)
{
//returns a string for the directory
return fDialog.FileName.ToString();
}
return null;
Now it will work !
We should add properties to the dialogbox before its actually been shown. So when it opens, it will have all these properties when you open them for the first time.
Edit :okay you have added to the designer by the toolbox already and its by default all of these options. but if some add from code. it should be always before its being shown. I will leave this here. so that someone who does this
this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
in code, will know that they should do these property addition before showing the dialog. Again, these true values are by default so unless u have mentioned false before elsewhere and making it true here.
You can just do it like this instead of return fDialog.FileName; and DialogResult.Cancel is a better option since your looking for a cancel and not for the OK result.
DialogResult result = fDialog.ShowDialog();
if (result == DialogResult.Cancel)
{
return;
}
i added a boolean and check if file selected or not
public Form1()
{
InitializeComponent();
}
bool fileSelected = false; //default false because nothing selected at start.
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
openFile();
if (fileSelected == true)
{
codes...
}
}
string path= "";
private void openFile()
{
OpenFileDialog file= new OpenFileDialog();
file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
file.Filter = "Text File|*.txt";
//file.RestoreDirectory = true;
if (file.ShowDialog() == DialogResult.OK)
{
path= dosya.FileName;
fileSelected = true;
}
else
{
MessageBox.Show("File not selected.");
}
}
i prevent this error like that.

Categories