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?
Related
I want to build a program in Windows forms where the user can create a pizza by pressing buttons.
My problem is that when the user presses an ingredient more than once, the list will just increment. I tried various methods but they don't seem to work.
I have seen a solution using a for loop checking individual items in the list however I will have to implement that 19 times which is not really efficient (once for every button)
string check = "Thin Base";
if (My_Pizza.Contains(check))
{
My_Pizza.Items.Remove("ThinBase");
My_Pizza.Items.Add("Thin Base");
}
You have "ThinBase" and "Thin Base". Not the same thing.
Also, if an item is already in the list, you don't need to do anything. Simply invert your check
string check = "Thin Base";
if (!My_Pizza.Items.Contains(check))
{
My_Pizza.Items.Add(check);
}
General idea is not to hardcode string values, but create reusable method that will do what you need: check if certain value is already in the list and if it not, add this item to list. This will help you to avoid duplicate code.
In the button event handler you simply call this method and provide string value as parameter. I'm not sure how exactly you handle button clicks, but I would suggest creating single reusable method once again and acquire string value from button.Text property.
Here is code sample for you to demonstrate the idea.
private void OnButtonClick(object sender, EventArgs e)
{
Button clickedButton = (Button) sender;
if (clickedButton != null)
{
string buttonContent = clickedButton.Text;
CheckAndAdd(buttonContent);
}
}
private void CheckAndAdd(string valueToCheck)
{
if (!My_Pizza.Items.Contains(valueToCheck))
{
My_Pizza.Items.Add(valueToCheck);
}
}
I'm C# with Compact Framework and I realized something weird today. I'm calling a method by an event that applies a set to an object and when I debug, it passes by this, but just performs after the last close bracket of the method. My example:
public string Loading
{
set { lblLoading.Text = value; }
}
private void btnAuth_Click(object sender, EventArgs e)
{
Loading = "Loading...";
_presenter.PerformAuth();
}
When I debug, it passes by my first statement, applies it, but doesn't change anything on the screen... Oh, until it do PerformAuth(). After it, so, then the label value is changed. Oh, the problem isn't just by it be synchronous. The same occurs when I try to do an asynchronous task:
private void btnAuth_Click(object sender, EventArgs e)
{
ASyncResult res = BeginInvoke(new Action(() =>Loading = "Loading..."));
EndInvoke(res);
_presenter.PerformAuth();
}
I think it might be a bug in thread and in C# design implementation. And also with direct set it is stubborn to me. As you can see in the image below:
I just want to set a text in a label, call a method and unset it in an event. Why does C# get it so complicated?
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!
}
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
I'm sending the contents of text boxes and checkboxes to my database via a WCF service.
So far, I have
fNameTxtBox.Text, - string
(DateTime) BirthDate.Value, - System.DateTime
toggle1.IsChecked, - bool
But what happens if the datatype is int?
Unfortunately the autocomplete does not help me as such, and of course it doesn't accept .Text as an ending.
Anybody able to lend a hand and let me know what this would be?
EDIT:
Here is my complete statement that I am trying to complete. I am looking for the ending for ownerTextBox which for which the method required it to be an int, when the source is a text box on a WP7 app:
private void addAccountBtn_Click(object sender, RoutedEventArgs e)
{
_ServiceClient.AddDriverAsync(fnameTxtBox.Text, snameTxtBox.Text, (DateTime)BirthDate.Value, phonemobNumBox.Text, toggle1.IsChecked, toggle2.IsChecked, toggle3.IsChecked, toggle4.IsChecked, toggle5.IsChecked, toggle6.IsChecked, toggle7.IsChecked, toggle8.IsChecked, toggle9.IsChecked, toggle10.IsChecked, toggle11.IsChecked, toggle12.IsChecked, toggle13.IsChecked, toggle14.IsChecked, toggle15.IsChecked, ownerTextBox.????);
}
ie replace the ???? with the correct ending, or reformat that part as such so that it works.
You need to perform validation on the client to ensure that an invalid value never gets sent to your service. You can do this by checking if the value is a integer, and display a message if its not. Only send the data to your service if it meets the validation criteria.
public void OnButtonClick(object sender, EventArgs e) {
int value;
bool isValid = int.TryParse(textBox.Text, out value);
if (isValid) {
// send to WCF
}
else {
// display a message
}
}
You will need to parse the value using the int class, e.g.
int.Parse(myTextBox.Text)
Note that int.Parse will throw an exception if the value cannot be represented as an integer, so you may want to perform appropriate validation or exception handling. Look into the TryParse family of methods.
You may find it useful to read up on Casting, Type Conversion, and Parsing.
Using the source-code you posted, you would change it to:
private void addAccountBtn_Click(object sender, RoutedEventArgs e)
{
_ServiceClient.AddDriverAsync(
fnameTxtBox.Text, <snip_lots_of_arguments>,
toggle15.IsChecked,
int.Parse(ownerTextBox.Text)); //I've only changed the very last bit here
}