Cannot implicitly convert DialogResult to MessageBoxResult - c#

I am receiving the error 'Cannot implicititly convert type 'System.Windows.Forms.DialogResult' to System.Windows.MessageBoxResult'
At first I thought this was just a missing import so I put imported System.Windows.Forms but it came to no avail as doing this throws up an error on my actual message box which is 'MessageBox' is an ambigous reference between 'System.Windows.MessageBox' and 'System.Windows.Forms.MessageBox' (Which isn't to hard to figure out :P) but the original message remains the same.
private void DisplayOnWebsiteChecked(Object sender, EventArgs e)
{
var departments = model.Name;
var departmentChildren = model.Children;
var messagebox = MessageBox.Show("Do you wish to hide all sub deparments and products.",
"List of Box",
MessageBoxButton.YesNo);
if (messagebox = System.Windows.Forms.DialogResult.Yes)
{
if (departmentChildren != null)
{
int zeroChildren = 0;
if (departmentChildren.Count.Equals(zeroChildren)) ;
{
foreach (Department Children in departmentChildren)
Children.IsVisibleOnWebsite = false;
}
}
}
else
return;
}
I have included the whole method just incase you need anything from it.

Given that you've now explained that this is a WPF app, if you're using System.Windows.MessageBox.Show you should be comparing against System.Windows.MessageBoxResult.
Basically, if you're doing WPF you almost certainly don't want any mention of System.Windows.Forms, and vice versa. (I'm surprised this was compiling at all - do you have references to both assemblies? That's generally a bad idea...)
Additionally, you need to use == for comparisons, not = (assignment)
var result = MessageBox.Show(...);
if (result == MessageBoxResult.Yes)
{
...
}

I think you've got your Windows Forms and WPF mixed up - if you're using the WPF MessageBox then your return value is different.
So it's not
messagebox = System.Windows.Forms.DialogResult.Yes
It's
messagebox == System.Windows.MessageBoxResult.Yes
Note also the == otherwise you're assigning the value, and the if test will always succeed (with a compiler warning).

You're problem is here:
if (messagebox = System.Windows.Forms.DialogResult.Yes)
First, you're assigning a DialogResult into a MessageBox. Secondly, you're attempting to compare a DialogResult with a MessageBox. Try something like:
if(MessageBox.Show("Message", MessageBoxButtons.YesNo) == DialogResult.Yes)
Or you can assign the DialogResult into its own variable, and compare that.

Related

I'm using int parse same way as it is in the console application but doesn't work in winforms

I need some help because I can't seem to get to display the text label here. I am using Windows Form C# VS 2015. When I press enter the error is on barangay = int.Parse(lblDistrict.Text); uhm also, I am applying the same way console.readline is used but it seems that it doesn't work. Can somebody help me in the code? :) Thanks in advance
private void txtBarangay_KeyPress(object sender, KeyPressEventArgs e)
{
int barangay = 0;
barangay = int.Parse(lblDistrict.Text);
if (e.KeyChar == (char)13)
{
if (barangay >= 1 && barangay <= 146)
{
lblDistrict.Text = "District 1";
}
else if (barangay >= 147 && barangay <= 267)
{
lblDistrict.Text = "District 2";
}
}
}
It is the matter of focus. When you press the key, which control is focused? Is it the one that you have written the KeyPress event for?
So you must make sure that either when the key is pressed either that specific control has the focus, or add this even to all controls.
I don't know what kind of Exception you get , but you must use Int.TryParse instead of int.parse .
check this link http://dailydotnettips.com/2016/01/16/back-to-basic-difference-between-int-parse-and-int-tryparse/
Thanks
First, Please include try-catch block in your program in-order to catch the exception.
If the string in the textBox contains anything other than ONLY numbers (Example "12er"), It will throw Exception. Try Int.TryParse if this is the case

Close current form if dialog results for dialogbox is cancel

I have a Form (frmcustlist).
At one time on this list i scan the list using a dataset and check if it now 0 (no customers left).
At this stage i have an input box pop up (dialog) to ask a new customer name.
If they press OK everything is fine. I also have validation on the box for the input.
However if they press CANCEL, i can get it to escape out of the dialog, but not close frmcustlist that the dialog was called from.
using (inputbox ipfirst = new inputbox("Enter Customer First Name:", "", ""))
{
if (ipfirst.ShowDialog() == DialogResult.OK)
{
newfirstname = ipfirst.answer;
}
else
{
this.Close();
}
}
Now, this.close() doesn't work at all.. so i used return; which stops it going on to ask for the last name and date of birth.. but i want it to stop asking questions (like return) AS WELL as close frmcustlist.
...
Thanks for any advice you can give.
ps. this appears in a few places, but is called in frmcustlist_load as well.. i dont know if that is going to make a difference or not!
Answer was made by STEVE in comments.
As frmcustlist was called itself as a Dialog, i just ended up having to give that dialog a Cancel result.
Final Code that works:
using (inputbox ipfirst = new inputbox("Enter Customer First Name:", "", ""))
{
if (ipfirst.ShowDialog() == DialogResult.OK)
{
newfirstname = ipfirst.answer;
}
else
{
DialogResult = DialogResult.Cancel;
return;
}
}

Accessing class from form and vice-versa

After hunting the internet for two days and not finding a solution that I can understand properly I have to ask on here for an answer.
I have a windows forms application that was written in vb.net and works fine. I have decided to rewrite this in c# which I thought wouldn't be too much of a problem but ...
I have two classes in the project :
FormJobs & AppJobs
FormJobs contains methods and functions that modify the forms in some way.
AppJobs contains methods and functions for everything else (Checks,Scanning and so forth).
On my main form (FrmStart) the On_load event uses a function from AppJobs to check that the network is up (public bool CheckNetConnection) and then Checks to make sure that the root save folder exists (public void CheckRoot).
If CheckNetConnection is false or CheckRoot does not exist then a method in the FormJobs class sets some buttons to disabled, some labels to display information as to what has gone wrong and also sets the height of the form.
The above works in VB.net but I keep getting a StackOverflowException or NullReferenceException with the C# code.
I know the reason for the Exceptions is because the two classes and the form all keep calling each other so I know that I need to remove this code but I am not sure how to let each class and the form access each other. It is obviously bad design as I`m just starting to learn C# so any help on this would be much appreciated.
But my main questions are:-How do I get a form to access multiple classes?
Allow the classes to access each other?
Let the classes make changes to a form?
FrmStart Code
AppJobs Appjobs = new AppJobs();
private void FrmStart_Load(object sender, EventArgs e)
{
KeyPreview = true;
if (Appjobs.CheckNetConnection(this) == true)
{
Appjobs.CheckRoot(this);
}
AppJobs Code
public class AppJobs
{
FormJobs Formjobs = new FormJobs();
public string AppRoot = Properties.Settings.Default.DefaultFolder;
public string DefaultDevice = Properties.Settings.Default.DefaultScanner;
public bool NoDirectory = false;
DialogResult MsgBoxQuestion;
public bool CheckNetConnection(Form StartForm)
{
IPHostEntry ServerIP = new IPHostEntry();
bool ConnectedToServer = false;
string CurrentRoot = "MyServer";
if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
try
{
IPHostEntry DNSTest = Dns.GetHostEntry(CurrentRoot);
if (DNSTest.AddressList.Length > 0)
{
ConnectedToServer = true;
}
else
{
ConnectedToServer = false;
}
}
catch (System.Net.Sockets.SocketException ex)
{
ConnectedToServer = false;
}
}
return ConnectedToServer;
}
public void CheckRoot(Form StartForm)
{
if (string.IsNullOrEmpty(AppRoot))
{
Formjobs.SetHeight(StartForm);
return;
}else if(AppRoot == "0")
{
Formjobs.SetHeight(StartForm);
return;
}
else
{
if ((!Directory.Exists(AppRoot)))
{
NoDirectory = true;
MsgBoxQuestion = MessageBox.Show(AppRoot + " is set, but the directory does not exist." + Environment.NewLine
+ Environment.NewLine + "Would you like to create the folder now?", "Root folder missing", MessageBoxButtons.YesNo);
if (MsgBoxQuestion == DialogResult.Yes)
{
Directory.CreateDirectory(AppRoot);
NoDirectory = false;
}
else
{
MessageBox.Show("You will not be able to use this program until you create a root folder.", "No root folder selected",MessageBoxButtons.OK);
}
}
}
}
}
FormJobs Code
public class FormJobs
{
AppJobs Appjobs = new AppJobs();
public void SetHeight(Form StartForm)
{
if (Appjobs.AppRoot == null | Appjobs.AppRoot == "0") {
if (Appjobs.DefaultDevice == null | Appjobs.DefaultDevice == "0") {
if (StartForm.Controls["MenuStrip1"].Visible == true) {
StartForm.Height = 167;
StartForm.Controls["LblNoRoot"].Visible = true;
StartForm.Controls["LblNoRoot"].Location = new Point(0, 24);
StartForm.Controls["LblNoRoot"].Text = "There is no root folder selected. Please select a root folder to continue.";
StartForm.Controls["LblNoDevice"].Visible = true;
StartForm.Controls["LblNoDevice"].Location = new Point(0, 48);
StartForm.Controls["LblNoDevice"].Text = "There is no default device selected. Please select a default device to continue.";
StartForm.Controls["BtnOkTickets"].Enabled = false;
StartForm.Controls["BtnQueryTickets"].Enabled = false;
StartForm.Controls["BtnSearch"].Enabled = false;
}else
{
StartForm.Height = 147;
StartForm.Controls["LblNoRoot"].Visible = true;
StartForm.Controls["LblNoRoot"].Location = new Point(0, 9);
StartForm.Controls["LblNoRoot"].Text = "There is no root folder selected. Please select a root folder to continue.";
StartForm.Controls["LblNoDevice"].Visible = true;
StartForm.Controls["LblNoDevice"].Location = new Point(0, 33);
StartForm.Controls["LblNoDevice"].Text = "There is no default device selected. Please select a default device to continue.";
StartForm.Controls["BtnOkTickets"].Enabled = false;
StartForm.Controls["BtnQueryTickets"].Enabled = false;
StartForm.Controls["BtnSearch"].Enabled = false;
}
}
One of the causes of your problems is that everyone is changing your StartForm. Apart from that this spaghetti makes it difficult to understand, it certainly doesn't help to make your classes reusable and maintainable if your Startform changes.
It seems to me, that AppJobs is meant to decide what the form should look like (for instance it decides that the StartForm should change height), while FormJobs performs the calculations needed to change this height. StartForm apparently is just allowing to let everyone make changes to him.
A better design would be that StartForm would not ask AppJobs to change its size, and to ask the operator whether a folder should be generated. Instead if ought to ask appJobs for advise: "Which height do you think I should have". after that it could ask FormJobs: "Please adjust my height according to this specification"
FormJobs should trust StartForm that it has gathered the correct information about how a StartForm ought to look like. FormJobs should not ask AppJobs for any information: "Hey AppJobs, StartForm asked me to change its appearance to certain specifications, but I'm not certain whether StartForm has done its job correctly. Please tell me if these specifications are correct, and give me some missing information")
The correct division into tasks would be:
AppJobs specifies the format of any StartForm according to its internal state (a.o. AppRoot, and existence of certain folders)
StartForm is the one who displays all items. He decides who to ask for specifications, and what to do with the returned specifications. He is also the only one who communicates with operators
FormJobs is a class that apparently knows all elements from a StartForm. If you will only have one type of StartForm, then Appjobs should be part of the Startform class. If you think there might be several different Startform classes, all with the same elements that ought to be manipulated similarly, shouldn't all these StartForm classes be derived from a FormJobs class?
Anyway, redesign without everyone causing to manipulate StartForm
Apparently there are a limited number of StartForm layouts depending on AppRoot, defaultDevice etc. You seem to be missing some "else" after your if, so this list might not be accurate. Still you will get the idea:
enum StartFormLayouts
{
DefaultDevice0,
AppRoot0,
Other,
}
// class that specifies the layout of any startform
class AppJobs
{
private bool IsAppRoot0
{
get{return Appjobs.AppRoot == null || Appjobs.AppRoot == "0";}
}
private bool IsDefaultDevice0
{
get{return Appjobs.DefaultDevice == null || Appjobs.DefaultDevice == "0";}
}
public StartFormLayoug GetPreferredLayout()
{
if (this.IsAppRoot0)
{
if (this.IsDefaultDevice)
{
return StartFormLayout.DefaultDevice0;
}
else
return StartFormLayout.AppRoot0;
}
else
{
return StartFormLayout.Other;
}
}
public bool ShouldAskDirectoryCreation()
{
return (!this.IsAppRoot0 && !Directory.Exists(AppRoot));
}
}
Note that this class doesn't need StartForm, nor AppJobs. It could work with any class that wants to know if it should ask for DirectoryCreation. Since it also does not speak any language, even a Chinese StartForm could use it. After all, the StartForm is the only one who knows what language it speaks and what to do if a certain layout is requested.
Furthermore, did you notice that I used a double || to use a Boolean OR instead of a bitwise or?
And I use statements like if (a) instead of if(a=true) a C# Boolean is a real Boolean, in contradiction to Booleans in C and C++.
The class of all kinds of forms that should be able to be layout according to the requested layout contains the functions similar to your
It depends a bit of whether you decide to let it be a base class of StartForm or StartForm itself. If you want it to handle every form class that has the required controls, consider of using an interface:
public Interface IStartForm
{
public int Height {get; set;}
public Label LabelNoRoot {get;}
public Label LabelNoDevice {get; }
public Button BtnTickets {get;}
...
This way you can set the size of any form that has these labels and buttons, even if they have different names than those strings you use.
But again: if you ever only want to size StartForm, then this should be a function in StartForm.
public SetHeight(StartFormLayout layout, IStartForm startForm)
{
switch (layout)
{
case StartFormLayout.DefaultDevice0:
if (startForm.MenuStrip.Visible)
{
startForm.Height = ...;
startForm.LabelNoRoot.Location = ...
// etc
}
else
{
...
Noticed that because of this separation of concerns the AppJobs and FormJobs don't have to know each other. AppJobs and FormJobs also don't really have to know what 'StartForm` is, only that it has the labels and buttons etc that it needs to change.
class StartForm : Form, IStartForm
{
public Label LabelNoRoot {get{return this.label1; } }
...
private void FrmStart_Load(object sender, EventArgs e)
{
AppJobs layoutdesigner = new AppJobs(...);
StartFormLayout layoutdesigner = layouter.GetPreferredLayout();
FormJobs layouter = new FormJobjs();
layouter.SetHeight(this)
}
Noticed that my form didn't have a label named "LabelNoRoot", but a Label1 that should function as a LabelNoRoot. Also: because I used types instead of string, You can be certain that I can't handle a label as if it was a button. I can't by accident try to press the label. Something that could easily been done when you were using strings to identify the items you want to layout.
Extending the comments: you just remove the new part in your FormJobs and AppJobs classes.
leaving the code in i.e. in the FormJobs class like : AppJobs appObj;
Then in your main form create at some point a FormJobs obj and an AppJobs obj and set its property.
I.e. in main Form:
AppJobs appObj = new AppJobs();
FormJobs formObj = new FormJobs();
formObj.appObj = appObj;
Tho I must say i dont like that approach you are taking with this...
You should think of another way or at least refactor your code that FormJobs does not need AppJobs methods and vice versa in a way that all calls to FormJobs and AppJobs come from your main form.

Computing Form Location with Button

I'm working on a programming assignment, and I'm trying to make this button take the values from two textboxes, and calculate the new location for the form window. I'm having trouble converting the textbox values to type int, and being made usable by the btnCompute_click method.
private void btnCompute_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(txtXvalue);
int y = Convert.ToInt32(txtYvalue);
Location = new Point(x,y);
}
private void xValue_TextChanged(object sender, EventArgs e)
{
int xValue =
Convert.ToInt32(txtXvalue);
}
private void yValue_TextChanged(object sender, EventArgs e)
{
int y =
Convert.ToInt32(txtYvalue);
}
I forgot to add some additional info, the acceptable values for x and y must be positive. Would I use an if...else statement to control the acceptable values?
user29... i have no idea why death... replied that. it makes no sense to me. but you question makes sense to me. i suppose death... did not understand your question.
First off, everything you ask does not need anything in the TextChanged methods.
Do all your handling in the btnCompute_Click() method because you want to do something *when you click your button, not when the user edits the text of the text boxes, according to your question.
The code that is in your TextChanged() methods will get executed whenever the Text values of those text boxes change. that's not what you asked for it to do. But you could use these events for example, if you wanted a label to become visible or hidden and to set the text of a label which shows text, so you can use it as an error message label, for instance if the integer value of a text box is negative or even if it cannot be parsed.
So in your btnCompute_Click() methods, you first want to get the int values. You need to decide exactly what you want your code to do if the text is not integers. In my opinion, most beginners code things like message boxes or something. I like to give the user feedback with Labels or a status bar message, depending on what I feel is appropriate. Since my first choice would be to use a Label to show the 'error' message when text boxes cannot be parsed to integers, then i would simply return from the button click method without doing anything when the values are not what i want. That way the user gets their messages without annoying popup message boxes or anything. But it's up to you whether you want to pop up a message box or not. Others have given you good code to do that. I want to give you good code that avoids what i consider annoying popup boxes.
When converting strings to an int, Convert.ToInt32 will throw an error if the string cannot be parsed. int.TryParse is the silver bullet for truly parsing strings to integers without any error. Here is the entire code i would use. I made a new project just to make sure i'm not giving you buggy code. I give you my code on how I handle this.
In your updated prerequisite, you mention x & y must be positive and not negative. I note to you that these are not the same. For instance, 0 is neither positive nor negative. I assume that you technically mean that x and y cannot be negative, (and that it does not need to be positive, since 0 should be allowed).
private void Form1_Load(object sender, EventArgs e)
{
lblErrorX.Text = null;
lblErrorY.Text = null;
}
private void btnMoveForm_Click(object sender, EventArgs e)
{
int x = 0; if (int.TryParse(txtX.Text, out x) == false) { return; }
int y = 0; if (int.TryParse(txtY.Text, out y) == false) { return; }
if (x < 0 || y < 0) { return; }
this.Location = new Point(x, y);
}
private void txtX_TextChanged(object sender, EventArgs e)
{
int x = 0;
if (int.TryParse(txtX.Text, out x) == false)
{ lblErrorX.Text = "X is not an valid integer."; return; }
if (x < 0) { lblErrorX.Text = "X cannot be negative."; return; }
lblErrorX.Text = null;
}
private void txtY_TextChanged(object sender, EventArgs e)
{
int y = 0;
if (int.TryParse(txtY.Text, out y) == false)
{ lblErrorY.Text = "Y is not an valid integer."; return; }
if (y < 0) { lblErrorY.Text = "Y cannot be negative."; return; }
lblErrorY.Text = null;
}
In my project, on the form, in the following left to right order: lblX, txtX, lblErrorX. I have the same corresponding for Y: lblY, txtY, lblErrorY. Then i have one Button: btnMoveForm. So my txtX corresponds to your txtXvalue. my btnMoveForm corresponds to your btnCompute, but to me, 'compute' means to calculate, which is not really what this button is doing. this button is moving the form, so that's why i name it as such.
I have played with setting both the Location and the DesktopLocation and it seems to do the same thing. I've always used Location and i just learned that DesktopLocation works too, but since Location is shorter, i use it.
Someone asked why i don't use if(!int.TryParse(...)) { return; } rather than my if(int.TryParse(...) == false) { return; }. My reason is unfortunately that i think ! is an easy one character to miss when reading code, so i avoid it, especially when that little mistake means the opposite of what the code really would do. So my use of '== false' is always for human clarity. But i do like the C# ease of only needing one character. I just think it's a shame that in my opinion, it's a lot safer to write code that is better for humans so we don't mistake it. That's the only reason i use '== false' instead of !. Use ! if you like. It's quite convenient. I regret not using it. hehe.
Oh, and the reason i set the lblErrorX.Text = null; and lblErrorY.Text = null; is on my form in design view, i give them a text value so i can see them. :) so when the program runs, i set the Text to be blank. But you can use the Visible property if you prefer. I just leave them always visible and only set their Text properties.
Based on your expanded criteria you can check for negative numbers conditionally or use Math.Abs to get the absolute value. Something like this.
int x, y;
if (int.TryParse(txtXvalue.Text, out x) && int.TryParse(txtYvalue.Text, out y))
{
if (x < 0 || y < 0)
{
MessageBox.Show("Negative numbers not allowed");
}
else
Location = new Point(x, y);
}
else
{
MessageBox.Show("Must be an Integer");
}
or
int x, y;
if (int.TryParse(txtXvalue.Text, out x) && int.TryParse(txtYvalue.Text, out y))
{
Location = new Point(Math.Abs(x), Math.Abs(y));
}
else
{
MessageBox.Show("Must be an Integer");
}
I think you are looking for this.
private void btnCompute_Click(object sender, EventArgs e)
{
int x = Convert.ToInt32(txtXvalue.Text);
int y = Convert.ToInt32(txtYvalue.Text);
DesktopLocation = new Point(Math.Abs(x), Math.Abs(y));
}
This gets the location for the desktop. Also you need the .Text to get the text inside the textbox. You should also check to make sure the text is not null or empty before using or it will cause an error.
If this isn't what you are looking for please explain a little more.

How to perform different logic based on checkbox being true or false?

I have two sections(primary and secondary) in a form with several textboxes which display information. On the secondary section I've added a checkbox (which when checked) I want to copy the information from the fields on the primary section into the fields on the secondary side. If its unchecked I want it to leave the field as blank.
Here is some sample code to help see what I am trying to do:
The CheckChanged event calls four different methods (each method contains logic for a specific checkbox):
private void CheckBoxCheckedChanged(
{
Method1();
Method2();
Method3();
Method4();
}
When Method4 is called I'd like to process the logic mentioned above if its checked, and leave blank if its not.
private void Method4()
{
if (checkBox4.Checked = true)
{
secondaryTextbox.Text = primaryTextbox.Text;
}
else if (checkBox4.Checked = false)
{
secondaryTextbox.Text = "";
}
}
The issue I'm having is that when I do this once the checkbox is checked, I can no longer 'uncheck' to change the field back to being blank.
What am I doing wrong, or is there a better way to go about this?
I apologize ahead of time if I posted this wrong, this is my first time using SO.
The code you wrote makes an assignment (=) inside the if statement's expression, but this is not what you mean. You should use == if you want to make a comparison. Or even better, just do this instead:
if (checkBox4.Checked)
{
secondaryTextbox.Text = primaryTextbox.Text;
}
else
{
secondaryTextbox.Text = "";
}
As Paolo pointed out the code you tried gives a compiler warning. If you try to never write code that produces warnings you can catch simple errors like this more quickly. There's even an option to treat warnings as errors which you can use.
You need
if (checkBox4.Checked == true)
to check if true, the way you were doing it was always assigning it the value "true"
You are doing assignment when you mean to be testing
use this
if (checkBox4.Checked == false)
or better this
if (false == checkBox4.Checked)
or even better this
if ( ! checkBox4.Checked)
instead of this
if (checkBox4.Checked = false)
You have a very small problem in your code:
you are using = instead of ==
also it is better to define an array of check-box and text-box instead of func1,func2,func3,func4
You are using a single "=" to test of the checkbox is checked.
THis has the effect of setting the checkbox state.
Use "==", as in
if (checkBox4.Checked == true)
or in fact, much better:
if (checkBox4.Checked)

Categories

Resources