I am creating a C# project which takes two user inputs of complex numbers and completes a mathematical operation on them. The problem I am running into is how to parse i as the square root of -1.
double operandA = 0;
double operandB = 0;
double result = 0;
string textA, textB;
string error = "The value you entered is invalid, try again";
private void plus_Btn_Click(object sender, EventArgs e)
{
result = operandA + operandB;
}
private void Subtract_btn_Click(object sender, EventArgs e)
{
result = operandA - operandB;
}
private void mult_Btn_Click(object sender, EventArgs e)
{
result = operandA * operandB;
}
private void divide_Btn_Click(object sender, EventArgs e)
{
result = operandA / operandB;
}
private void textBox2_Leave(object sender, EventArgs e)
{
textB = textBox2.Text;
if (double.TryParse(textB, out operandB))
operandB = double.Parse(textB);
else
{
MessageBox.Show(error);
textBox2.Select();
}
}
private void textBox1_Leave(object sender, EventArgs e)
{
textA = textBox1.Text;
if (double.TryParse(textA, out operandA))
operandA = double.Parse(textA);
else
{
MessageBox.Show(error);
textBox1.Select();
}
}
It works fine on regular numbers, decimals, and negative numbers, but I can't figure out how to do the value of i that I need. Can anyone help? It has been suggested that I use System.Numeric.Complex, but whenever I try to do this using "Using System.Numerics.Complex" or simply "Using System.Numerics", it says this type/namespace does not exist inside 'System'.
If you want to support operations on complex numbers, you should consider using System.Numerics.Complex
Complex c = Complex.Sqrt(-1);
Console.WriteLine(c + 1);
For documentation on this type see here
You can use Complex in System.Numerics
https://msdn.microsoft.com/en-us/library/system.numerics.complex(v=vs.110).aspx
I have a mathematical problem and I´m trying to solve it, the problem is that you have 81 coins, but one is fake and it´s heavier than the others,you have to find out which one is the fake one by using a scale and doing only 4 comparisons.
I´m trying to make it like a game, when a users decides which coin will be the fake one, and the other player has to find it.
I made an array named monedasf and made all the values 0, so when the users type in the coin that wants to be the fake one, the value changes to 1. I´m trying right now to print the array, but I don´t know if I have to print it in a multiline textbox or where, here´s the code I have untill now.
public partial class Form1 : Form
{
public static int[] monedasf = new int[81];
public Form1()
{
InitializeComponent();
for( int i = 0; i<=80;i++)
{
monedasf[i] = 0;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
int n;
n = Convert.ToInt32(textBox1.Text);
monedasf[n] = 1;
textBox1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text = Convert.ToString(monedasf[i]);
}
}
I have only BASIC KNOWLEDGE of programming, that´s why my code might be so primitive :D
Try using something like this:
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text += monedasf[i].ToString() + " ";
}
If you want you can replace " " with any separator you want, like "\n" for newline.
Actually your code would work too, the problem is you were reseting textBox2's text by using assign operator:
textBox2.Text = Convert.ToString(monedasf[i]); // will clear and then print
textBox2.Text += Convert.ToString(monedasf[i]); // will not clear and print
All you need is not to reset previous text inside.
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = string.Join(", ", monedasf);
}
Use string.Join which is very useful for display.
I have a form in C# Windows Forms, every time a button is clicked I change my index, and I want to change the content of a Label based on this index. The only choice I found is label_click however I want the change to be automatically. Any idea about this?
// lines a gloab list of strings and index changes from a button click
private void label1_Click(object sender, EventArgs e)
{
label1.Text = "videos/" + lines[index] + ".mp4";
}
private void button4_Click(object sender, EventArgs e)
{
index++;
}
private void button3_Click(object sender, EventArgs e)
{
if (index >= 1)
index--;
}
Try the following
List<string> lines = new List<string>(){/*initialization here*/}
int index = 0;
private void button4_Click(object sender, EventArgs e)
{
//Ensure index is inside List bounds.
index = Math.Min(lines.length -1 , index + 1);
ChangeLabelText()
}
private void button3_Click(object sender, EventArgs e)
{
//Ensure index is inside List bounds.
index = Math.Max(0 , index - 1);
ChangeLabelText()
}
void ChangeLabelText() => label1.Text = "videos/" + lines[index] + ".mp4";
I am trying to complete an assignment for a new C# class using VS doing a windows form application (all new to me, code and all). We were assigned to creating a sentence builder with various words that were buttons; and then when the app runs, the user could click the buttons to build a sentence; which is then shown in the Label control.
Well, I have the form built, and from other info I found on this site for a similar question; have made it to this point. BUT my problem is - my instructor said we should be concatenating the results within the Label output, but FIRST I dont' know how to do that with someone just randomly clicking letters or words**(with what we have learned so far).
I got it to run with the following code(without concatenationin Label); except the "spaceButton" event puts IN the text of "(Space)" because that is it's text... I changed it to " " in the code and If I click on it running, it will now put in spaces but changes the text in the running app to a blank button. I don't know how to fix that.
I have had this instructor before and while I might be able to work around the concatenation in the "sentenceOutputLabel" - I might very well get a zero because I didn't concatenate in the output label.
Laura
Here is all the code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Sentence_Builder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void upperCaseAButton_Click(object sender, EventArgs e)
{
string output;
output = upperCaseAButton.Text;
sentenceOutputLabel.Text += output;
}
private void lowerCaseAButton_Click(object sender, EventArgs e)
{
string output;
output = lowerCaseAButton.Text;
sentenceOutputLabel.Text += output;
}
private void upperCaseAnButton_Click(object sender, EventArgs e)
{
string output;
output = upperCaseAnButton.Text;
sentenceOutputLabel.Text += output;
}
private void lowerCaseAnButton_Click(object sender, EventArgs e)
{
string output;
output = lowerCaseAnButton.Text;
sentenceOutputLabel.Text += output;
}
private void upperCaseTheButton_Click(object sender, EventArgs e)
{
string output;
output = upperCaseTheButton.Text;
sentenceOutputLabel.Text += output;
}
private void lowerCaseTheButton_Click(object sender, EventArgs e)
{
string output;
output = lowerCaseTheButton.Text;
sentenceOutputLabel.Text += output;
}
private void manWordButton_Click(object sender, EventArgs e)
{
string output;
output = manWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void womanWordButton_Click(object sender, EventArgs e)
{
string output;
output = womanWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void dogWordButton_Click(object sender, EventArgs e)
{
string output;
output = dogWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void catWordButton_Click(object sender, EventArgs e)
{
string output;
output = catWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void carWordButton_Click(object sender, EventArgs e)
{
string output;
output = carWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void bicycleWordButton_Click(object sender, EventArgs e)
{
string output;
output = bicycleWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void beautifulWordButton_Click(object sender, EventArgs e)
{
string output;
output = beautifulWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void bigWordButton_Click(object sender, EventArgs e)
{
string output;
output = bigWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void smallWordButton_Click(object sender, EventArgs e)
{
string output;
output = smallWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void strangeWordButton_Click(object sender, EventArgs e)
{
string output;
output = strangeWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void lookedAtWordButton_Click(object sender, EventArgs e)
{
string output;
output = lookedAtWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void rodeWordButton_Click(object sender, EventArgs e)
{
string output;
output = rodeWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void spokeToWordButton_Click(object sender, EventArgs e)
{
string output;
output = spokeToWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void laughedAtWordButton_Click(object sender, EventArgs e)
{
string output;
output = laughedAtWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void droveWordButton_Click(object sender, EventArgs e)
{
string output;
output = droveWordButton.Text;
sentenceOutputLabel.Text += output;
}
private void spaceButton_Click(object sender, EventArgs e)
{
string output;
output = spaceButton.Text = " ";
sentenceOutputLabel.Text += output;
}
private void periodButton_Click(object sender, EventArgs e)
{
string output;
output = periodButton.Text;
sentenceOutputLabel.Text += output;
}
private void exclamButton_Click(object sender, EventArgs e)
{
string output;
output = exclamButton.Text;
sentenceOutputLabel.Text += output;
}
/ I DON'T EVEN KNOW WHAT I NEED THIS below FOR NOW
private void sentenceOutputLabel_Click(object sender, EventArgs e)
{
//string output;
// sentenceOutputLabel.Text = sentenceOutputLabel.Text;
}
private void clearButton_Click(object sender, EventArgs e)
{
sentenceOutputLabel.Text = "";
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}`
If I understood the assignment description correctly, this is what I would do:
using System;
using System.Windows.Forms;
namespace StringBuilder
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnInput_Click(object sender, EventArgs e)
{
Button button = sender as Button;
if (button != null)
{
if (button.Text == "Space")
lblOutput.Text += " ";
else
lblOutput.Text += button.Text;
}
}
}
}
With this code, every single button can be assigned to the btnInput_Click event handler, and lblOutput is just the output.
I think the solution might just be so simple, it didn't occur to you it was allowed:
private void spaceButton_Click(object sender, EventArgs e)
{
string output;
output = " "; // don't necessarily have to use the Text property of the button ...
sentenceOutputLabel.Text += output;
}
I won't give you the solution, but maybe I can provide a bit more guidance and assist in the learning process.
First off, the value you're concatenating doesn't have to come from the button itself. The way you have it setup, each button has a designated event (*_CLick(object,EventArgs)). If you already know what button's being click, using that button's .Text isn't really necessary. So, feel free to use the desired result within this method and not stick to the button's .Text.
Also, based on what you've described, you're fulfilling the concatenation part (you seem reluctant to believe this). Every time you append (+=) the clicked item's text to the label's current value you're concatenating (just C# is doing to work for you).
To take it a step further though, each control has a Tag property. This allows you to specify metadata and bind it to that specific control. You may want to consider specifying the desired output (with regards to what's added to the final label) as the button's tag, then appending that value (over the button's Text). You could also re-use a single *_Click(object,EventArgs) method and just concatenate ((Button)sender).Tag.
Here's an example of how you can use a common click event, and how you can use the Tag property to store the value to be added to the label (which may be different than the value displayed on the Button text).
I'm dynamically adding the buttons, which you wouldn't have to do, but this way you can copy/past the code into a new project and run it. Hopefully it is instructive, but not something you would turn in as your own work.
public partial class Form1 : Form
{
Label sentenceOutputLabel = new Label();
public Form1()
{
InitializeComponent();
int padding = 10;
// Add a label to the form
sentenceOutputLabel.Width = ClientSize.Width - (padding * 2);
sentenceOutputLabel.Top = padding;
sentenceOutputLabel.Left = padding;
sentenceOutputLabel.BorderStyle = BorderStyle.FixedSingle;
Controls.Add(sentenceOutputLabel);
// List of words that, for each one, we'll add a button to the form
// Note that some words have two parts: the first part is the button
// text, followed by a colon, then followed by the tag text. We split
// these out later, when assigning button properties
var words = new List<string>
{
"A", "a", "An", "an", "The", "the", "man", "woman", "dog", "cat",
"car", "bicycle", "beautiful", "big", "small", "strange", "looked",
"rode", "spoke", "laughed at", "drove", "[space]: ", "[period]:.",
"[exclamation]:!"
};
// Get the width of the longest word so we size the buttons accordingly
int width;
using (Graphics cg = CreateGraphics())
{
width = Convert.ToInt32(cg.MeasureString(words.Aggregate("", (max, cur) =>
(cg.MeasureString(max, new Button().Font).Width) >
(cg.MeasureString(cur, new Button().Font).Width)
? max
: cur), new Button().Font).Width);
}
// Add the buttons to the form, spacing them evenly
int left = padding;
int top = sentenceOutputLabel.Bottom + padding;
int bottomOfLastButton = 0;
foreach (var word in words)
{
// For some words, we have a colon-separated symbol that we
// will use instead of the word (like 'space', for example)
// So we store the symbol in the 'Tag' property and the word
// in the text property.
var wordParts = word.Split(':');
var text = wordParts[0];
var tag = wordParts.Length > 1 ? wordParts[1] : text;
var button = new Button
{
Text = text,
Tag = tag,
Left = left,
Top = top,
Width = width,
Visible = true
};
// HERE WE ADD A COMMON CLICK EVENT
button.Click += wordButton_Click;
// Add the button to the form
Controls.Add(button);
// Reset left and top if we're going past the width of the form
left += (width + padding);
if (left + width + padding > ClientSize.Width)
{
left = padding;
top += button.Height + padding;
}
bottomOfLastButton = button.Bottom;
}
// Add a clear and exit button
var exitButton = new Button
{
Top = bottomOfLastButton + (padding * 2),
Text = "Exit",
Left = ClientSize.Width - padding - width,
Width = width
};
exitButton.Click += exitButton_Click;
var clearButton = new Button
{
Top = bottomOfLastButton + (padding * 2),
Text = "Clear",
Left = exitButton.Left - padding - width,
Width = width
};
clearButton.Click += clearButton_Click;
Controls.Add(exitButton);
Controls.Add(clearButton);
}
void clearButton_Click(object sender, EventArgs e)
{
sentenceOutputLabel.Text = "";
}
void exitButton_Click(object sender, EventArgs e)
{
Close();
}
void wordButton_Click(object sender, EventArgs e)
{
var wordButton = sender as Button;
if (wordButton != null)
{
sentenceOutputLabel.Text += wordButton.Tag;
}
}
}
I'm quite new to this things. Actually it is my first work. I want a program that reads random file count from textbox. and it has a button to randomize files from selected path. I need to open files in the listbox.
My problem is when I double click on the listbox, it opens the last file in the list no matter what file I d.clicked. I tried to add lines that put two slash before below. But it also didnt work. What can I do?
public Form1()
{
InitializeComponent();
}
Random r = new Random();
string path1;
DirectoryInfo dif;
FileInfo[] files;
int randomchoose;
//FileInfo[] files2;
//int hoho;
int[] randomcount;
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog hoho = new FolderBrowserDialog();
hoho.ShowNewFolderButton = true;
if (hoho.ShowDialog() == DialogResult.OK)
{
path1 = hoho.SelectedPath;
textBox1.Text = path1;
dif = new DirectoryInfo(path1);
files = dif.GetFiles();
}
}
private void btnrasgele_Click(object sender, EventArgs e)
{
randomcount = new int[Convert.ToInt32(textBox3.Text)];
// int hoho=0;
foreach (int k in randomcount)
{
int pd = files.Length;
randomchoose = r.Next(0, Convert.ToInt32(pd + 1));
listBox1.Items.Add(files[randomchoose]);
//files2[hoho] = files[randomchoose].FullName;
}
}
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
//listBox1.SelectedIndex = hoho;
//Process.Start(files2[hoho].FullName);
Process.Start(files[randomchoose].FullName);
}
You passed in the randomchoose which is fixed at that point, try this instead:
private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if(listBox1.SelectedItem != null)
Process.Start(((FileInfo)listBox1.SelectedItem).FullName);
}