Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
I can run the program and fill in my inputs but get no output.No errors at all.
I am making an calculator that works by selecting radio buttons to select a mathematical function.
The answer must apear in the tbAntwoord but I cant get that working
Can somebody please help>? Its a school assignment and I am really new to this
private void buBereken_Click(object sender, EventArgs e)
{
//variabelen
double antwoord;
int x, y, graden;
//inlezen variabelen
antwoord = Convert.ToInt16(tbAntwoord.Text);
x = Convert.ToInt16(tbX.Text);
y = Convert.ToInt16(tbY.Text);
graden = Convert.ToInt16(tbGraden.Text);
// berekeningen
if (raDelen.Checked)
antwoord = x / y;
if (raMacht.Checked)
antwoord = Math.Pow(x, y);
if (raSin.Checked)
antwoord = (Math.Sin(graden));
if (raCos.Checked)
antwoord = (Math.Cos(graden));
tbAntwoord.Text = antwoord.ToString();
Console.Write("antwoord");
You are calculating a value and assigning it to a variable antwoord, but you are printing a string "antwoord" rather then the variable:
Console.Write( antwoord ) ;
Even then, if your application has no console (a text-only window), there will be nothing to see the output on.
You are also setting the Text property of tbAntwoord to a string representation of antwoord, but it is not demonstrated in the code what tbAntwoord is. For it to be displayed, it must refer perhaps to a object in the GUI form, or something else must specifically display it. It cannot be determined from this fragment alone what the problem is.
Related
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed last month.
Improve this question
What I want to achieve is to make sure that my property is always 0 or greater. The type doesn't matter.
Lets say:
var x = 3;
var y = 5;
And after x -= y x is supposed to be 0.
Currently using this way and was wondering of there is a way without member variables:
public int Coins
{
get { return Math.Max(0, _coins); }
set { _coins = value; }
}
Couldn't either figure out a way with uint.
All suggestions appreciated.
uint x = 3;
uint y = 5;
Expected x beeing 0 after x -= y but received unit max.
If you implement the get and set differ from the automatic properties, then you always need to declare a private member to store the value. This is by design.
Even if you use the automatic properties and don't declare the private member, the compiler will do it anyway. You just see less code, but the computer sees no differences.
Source: https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/auto-implemented-properties
Anyway, I would suggest 1 small change:
public int Coins
{
get { return _coins; }
set { _coins = Math.Max(0, value); }
}
This way, the "wrong" _coins value don't get stored. But of course, it based on your bussiness logic, so the suggested code might not the right one for you
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a windows form project ( Net 4) in C#, and I want to change all of my label.Texts in a loop. but my labels are in a Tabcontrol (in tabpage 4) and i don't know what to do. my labels name are label1 label2 and so.. until label 100.
You could use something like this to loop through the controls and set the label text based on the number in the name.
foreach ( Control ctrl in tabPage4.Controls )
{
if ( ctrl.GetType().Equals(typeof(Label)) )
{
string strName = ctrl.Name;
if ( strName.StartsWith("label", StringComparison.InvariantCultureIgnoreCase) )
{
string strNum = strName.Substring(5);
int iIndex;
if ( int.TryParse(strNum, out iIndex) )
{
ctrl.Text = "your text";
}
}
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I want to change the value of total price's text box when unit price and quantity is entered.Unit price and total price's data type is float.
This code show error tht cannot convert int to float
how i can solve this issue?
Txtbox_quantity_textchanged
{
TxttotalPrice.text= (a*b).toString();
}
I can't see where is the problem, since you're unclear.
the following code compiles and runsL
float floatNumber = 60;
int intNumber = 34;
string result = (intNumber*floatNumber).ToString();
If you're having other trouble try casting:
float floatNumber = 60;
int intNumber = 34;
float result = (float) intNumber*floatNumber;
string newResult = result.ToString();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am trying to set the text in textboxes to something like A B C and so on, so I am trying to use a for loop to have something like the code below
string alpha = "abcdef.."
for (int i = 1; i <= number +1; i++)
{
textboxi.text = .alpha.CharAt(i)
}
"textboxi" dose not work. textbox + i dose not work idir, would anyone have any ideas
Thanks for the help
Assuming you've got some number of textboxes named textbox1, textbox2, etc., you can't just put a variable in the name and have it refer to the right textbox. You should put the textboxes in an array first.
TextBox[] textboxArray = new TextBox[] { textbox1, textbox2, textbox3, ... };
string alpha = "acdefde";
for (int i = 0; i < alpha.Length; i++)
{
textboxArray[i].Text = alpha[i].ToString();
}
EDIT
Note that this does no error checking whatsoever, so if you give it a string that's too long it'll blow up. That's not that big an issue here where the length of both items are known at compile time, but nevertheless here's a fix: change i < array.Length to i < array.Length && i < textboxArray.Length. If you were to put this into a more general function, I'd have it throw an exception if the number of textboxes given isn't exactly equal to the length of the string given.
Try this
string alpha = "abcdef.."
StringBuilder sb = new StringBuilder();
for (int i = 1; i <= number +1; i++)
{
sb.Append (alpha.CharAt(i));
}
textbox.Text = sb.ToString();
EDIT:
My answer missed that you're trying to assign a letter per textbox, as the comment suggests.
So, Jack's answer is the right one, but I highly suggest you do some thinking about your code, because it smells funny (not in a good way). besides, if the text will not be of the same length of the textboxes, it'll throw an exception.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a button (let's call it button1), a textbox (let's call it textbox1) and another textbox (let's call it textbox2). - Windows Form Application in C#
So i've made it that you need to write how long you want the characters to be in textbox2 (e.x 9) when button1 is clicked to generate random 9 characters (like D0yZk#!eA - depending how many characters you've set it to in textbox2) to textbox1. It can generate from 1 to 99 characters, that's just an example.
So my question is how to make in C# when button1 is clicked, the result from textbox1 to go to a text file in a new line which to be included in the program in which i can scroll in to see what i have generated? Like a textbox (but that textbox's content to be a textfile that all generated things will be in a new line) that show's all or the latest generated things in a new line each that is only readable.
Add a ListBox to your form and call it listBox1. Then add a click event to button1, and do something like:
private void button1_Click(object sender, EventArgs e) {
int length;
// We use TryParse to make sure it is a number, otherwise
// we tell the user and return;.
if (!int.TryParse(textBox2.Text, out length)) {
MessageBox.Show("Not a number!");
return;
}
// Make a random string
string myString = RandomString(length);
// Show it in the text box
textBox1.Text = myString;
// ... and in the listbox
listBox1.Items.Add(myString);
}
The function for a random string could look like this:
string RandomString(int length) {
string options = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890!#%&/()=?";
StringBuilder str = new StringBuilder(length);
Random random = new Random();
// Add the characters one by one
for (int i = 0; i < length; i++) {
// Get a random index in our character options string.
int randomIndex = random.Next(options.Length);
// Add the random character
str.Append(options[randomIndex]);
}
// Make a string of the StringBuilder and return it.
return str.ToString();
}