Within my WinForm i have a method that checks the validation of various user controls and adds them to an errorList. When the user clicks the save button I want it to check the validation method and show the errors if any in a message box. The Validate method is in another form and class so i think that might be my problem.
private void Save_Click(object sender, EventArgs e)
{
var errorList = string.Join(Environment.NewLine, Validate.ToArray());
MessageBox.Show(errorSet);
}
Thank you for any help.
The error 'Form1.Validate(System.Collections.Generic.List<string>)' is a 'method', which is not valid in the given context means that you're using the method wrong.
var errorList = string.Join(Environment.NewLine, Validate.ToArray());
makes no sense. You're missing the parentheses:
var errorList = string.Join(Environment.NewLine, Validate().ToArray());
That's only one problem. The method has a parameter of type List<string>, but you don't pass an argument to the function.
Also, you said in a comment that the return value is of type bool, but it seems you expect it to return a collection of strings.
You have this issue because you are calling the validate method that is on another form without mentioning the instance of that form.
Lets say you have another class Class1.
//create instance of your class/form that has this method
OperationControl oc = new OperationControl ();
private void Save_Click(object sender, EventArgs e)
{
//call the method with form instance created above
var errorList = string.Join(Environment.NewLine, oc.Validate().ToArray());
MessageBox.Show(errorSet);
}
some time this error means you may have the same method with the same name in your program scope. Check if no other function named MessageBox exist in your program
Related
I am writing a piece of code but cannot figure out why it is showing some (from my point of view) weird behaviour. I get this is due to how I have written the code, but I cannot understand why it is not doing what I want it to do.
I have an eventhandler:
private void heatFinishedHandler(object sender, List<Kart> e)
{
mainForm.enableControls();
server.karts = e;
}
If I add e.Clear(); to it, it also clears server.karts.
The argument e comes from another class:
private List<Kart> kartlist = new List<Kart>();
heatFinished?.Invoke(this, kartlist);
The heatFinished event and the heatFinishedEventHandler are connected.
What do I need to do to get e by value, instead of reference? Or is it a value type, but is it pointing to kartlist?
I want to be able to get the value of e and store it in server.karts, not being able to change kartlist.
Make a copy of the list you pass to the function like
private List<Kart> kartlist = new List<Kart>();
heatFinished?.Invoke(this, kartlist.ToList());
Or do it inside the function like
private void heatFinishedHandler(object sender, List<Kart> e)
{
mainForm.enableControls();
server.karts = e.ToList();
}
.ToList() will make a new List-instance with the same values as the original one.
Wierd behaviour when passing values to and from second form.
ParameterForm pf = new ParameterForm(testString);
works
ParameterForm pf = new ParameterForm();
pf.testString="test";
doesn't (testString defined as public string)
maybe i'm missing something? Anyway I'd like to make 2nd variant work properly, as for now - it returns null object reference error.
Thanks for help.
Posting more code here:
calling
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(doc.GetElementById(ParametersButton.Tag.ToString()));
pf.ShowDialog(this);
pf.test = "test";
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
};
definition and use
public partial class ParameterForm : Form
{
public string test;
public XmlElement node;
public delegate void ParameterSubmitResult(object sender, XmlElement e);
public event ParameterSubmitResult Submit;
public void SubmitButton_Click(object sender, EventArgs e)
{
Submit(this,this.node);
Debug.WriteLine(test);
}
}
result:
Submit - null object reference
test - null object reference
pf.ShowDialog(this); is a blocking call, so pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit); is never reached: switch the order.
Submit(this,this.node); throws a null object reference because no event is assigned to it (see above). Generally, you should always check first: if (Submit != null) Submit(this,this.node);
You should change ``pf.ShowDialog(this);topf.Show(this);` so that your main form isn't disabled while your dialog box is open, if that's what you want, or use the model below (typical for dialog boxes.)
I'm not sure what pf_Submit is supposed to do, so this might not be the best way to go about it in your application, but it's how general "Proceed? Yes/No" questions work.
Button ParametersButton = new Button();
ParametersButton.Click += delegate
{
ParameterForm pf = new ParameterForm(testString);
pf.ShowDialog(this); // Blocks until user submits
// Do whatever pf_Submit did here.
};
public partial class ParameterForm : Form
{
public string test; // Generally, encapsulate these
public XmlElement node; // in properties
public void SubmitButton_Click(object sender, EventArgs e)
{
Debug.WriteLine(test);
this.Close(); // Returns from ShowDialog()
}
}
When you want to use your second variant, you have to use a getString()-Method, where you can put the e.g. "testString". The way you wrote it, "testString" should be a method (and got brackets).
EDIT (a bit more precise):
You could write:
pf.getString(testString);
, if "pf" is an instance of your own class, otherwise you had to look up, whether you can retrieve a String in this class.
the thing was in line order :)
pf.Submit += new ParameterForm.ParameterSubmitResult(pf_Submit);
and
pf.Test = "test";
should have been set before
pf.ShowDialog(this);
my mistake thingking that parameter can be passed after 2nd form was displayed
thnx for answers
In my program on the WindowsForms I have a MainForm, which contains ProgressBar. In the ChildForm I want to change it's value, but I'm getting an error:
"int" does not contain a definition for "Value". Unable to find an extension method "Value", taking first argument of type "int" (missing a using directive or an assembly reference?)
Main Form:
public int ProgressBar5
{
get { return progressBar5.Value; }
set { progressBar5.Value = value; }
}
Child Form:
static MainForm mainForm = new MainForm();
private void button1_Click(object sender, EventArgs e)
{
mainForm.ProgressBar5.Value++; // Error
}
Edit1:
In the main form the value of the progressBar5 is always equals 1. What can be reason of it? I thought, that static can fix it.
Main Form:
private void button9_Click(object sender, EventArgs e)
{
Child child = new Child();
child.ShowDialog();
MessageBox.Show(progressBar5.Value.ToString()); // All time Value = 1
}
Your ProgressBar5 property already takes care of accessing the Value property of the progress bar. It doesn't return the progress bar, but rather the current progress. This means that the caller doesn't need to access the Value property of the result (since there is none).
Just write:
mainForm.ProgressBar5++;
Of course, for clarity's sake, it would probably be better to call such a property CurrentProgress rather than ProgressBar because it doesn't return a progress bar, it returns (or sets) the current progress.
Of course, better still would be for the child for to have no knowledge of the main form at all. Rather than passing in the main form itself the child form can accept an IProgress instance. It can then report progress to whomever is creating this form, whether that be your main form, or anything else. The main form is then responsible for doing whatever it wants when progress is reported, which it can define in either an event handler or a delegate passed into the Progress constructor.
The problem is that you have two variables with similar names, causing quite a bit of confusion. When you try and access the ProgressBar5 variable you are actually getting the int property you declared in the first snippet.
Since int does not have a Value property, you get the compile time error. Change your code to:
static MainForm mainForm = new MainForm();
private void button1_Click(object sender, EventArgs e)
{
mainForm.ProgressBar5++; // Fixed!
}
I have a method called BuyShares() that is supposed to take a value in a textbox and add another user input value to it. I would like to use a messagebox that sets off the method by the user clicking okay. The only problem is that I can't seem to call upon the method.
This is the method.
public void BuyShares(int anAmount)
{
int newShares;
newShares = GetInvestmentShare() - anAmount;
SetInvestmentShare(newShares);
}
And this is the messagebox I have set up
private void button1_Click(object sender, EventArgs e)
{
DialogResult result;
result = MessageBox.Show("Your transaction is complete", "Success", MessageBoxButtons.OK);
if(result==DialogResult.OK)
{
txtStockSharesTab3.Text=??????
}
This is a windows form application and the program has several different classes
Without giving away the answer because you probably want to learn something from this...
I'm guessing you want to get the amount of shares to buy from one textbox, then call BuyShares and finally update txtStockSharesTab3 to this value.
Your BuyShares method returns void meaning it won't return a value. Somewhere in that method is where you're going to update your txtStockSharesTab3 textbox. Is that exact method signature for BuyShares required?
I'll start of by saying I'm not a developer. Yes this is a c# nightmare. But this is a one time tool and thats it. Quick and Dirty it just needs to work and thats it.
I have the following code:
public string[] get_status(string local_fname)
{
var dts_doc = new HtmlAgilityPack.HtmlDocument();
dts_doc.Load(local_fname);
//Pull the values
var ViewState = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[1]/input[4]/#value[1]");
var EventValidation = dts_doc.DocumentNode.SelectSingleNode("/html[1]/body[1]/div[2]/input[1]/#value[1]");
string ViewState2 = ViewState.Attributes[3].Value;
string EventValidation2 = EventValidation.Attributes[3].Value;
//Display the values
//System.Console.WriteLine(ViewState.Attributes[3].Value);
//System.Console.WriteLine(EventValidation.Attributes[3].Value);
//System.Console.ReadKey();
return new string[] { ViewState2, EventValidation2 };
}
I want to call get_status from a button on my Main.cs which will show 2 Message Boxes with ViewState2 and EventValidation2.
Again, I'm not a developer, this is probably the wrong way of doing things. But I just need a quick and dirty solution to get this job done once.
Make the function static by adding the static keyword to the function definition:
static public string[] get_status(string local_fname)
Use the class name to reference the function from your Main class.
try this:
foreach(string s in get_status(localFname))
{
MessageBox.Show(s);
}
As you said, it is quick and dirty and I stayed faithful to that paradigm.
And yes, if you need to acces another class, make the method static or just simply create an instance and call the method on it. I hope I have understood the problem correctly.
if you are using visual studio, go to the Button you want to click, double-click the button. This will create an eventhandler. In the eventhandler you should call the above method.
protected void Button1_Click(object sender, eventArgs e)
{
string local_fname = someValue;
string results[] = get_status(local_fname);
MessageBox.Show(results[0]);
MessageBox.Show(results[1]);
}