|
Form1 form = new Form1();
form.ShowDialog();
|
|
|
|
|
FORM1 FR = NEW FORM1;
FR.SHOW();
|
|
|
|
|
Been stuck with this for too long now and thought I ask for help. From reading a bunch of threads this seems trivial for you guys. I am not on some homework, just doing it out of self interest following a tutorial at csharpskolan.se
I do know how to make a windows form to generate a number using two numericUpDown boxes but I though I try make a dice program and pick the number of sides out of a comboBox. I have added 3 sided dice, 4 sided dice and so on using the design mode but I do not know how to put the value (1, 4) to the 3 sided dice selection for example.
Feels like I am getting stuck making the method, to call it depending on how many times I want to roll the 3 sided dice. I am going to have the output to a richTextBox.
What I am using is a comboBox(dice sides), textBox(number of times to roll), richTextBox(to diplay the value) and a button to generate it.
This is what I got after redoing it so many times.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
var random = new RollDice();
}
}
public class RollDice
{
public int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
Random rndGen = new Random();
for (int i = 0; i < numberOfSides; i++);
int randomT3;
int randomT4;
int randomT5;
int randomT6;
randomT3 = rndGen.Next(1, 4);
randomT4 = rndGen.Next(1, 5);
randomT5 = rndGen.Next(1, 6);
randomT6 = rndGen.Next(1, 7);
}
}
}
modified 8-May-12 8:35am.
|
|
|
|
|
Here you go.
public static int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
int total = 0;
Random rndGen = new Random();
for (int i = 0; i < numberOfTimes; i++)
{
total = total + rndGen.Next(1, numberOfSides + 1);
}
return total;
}
|
|
|
|
|
Thanks a bunch, now it makes sence. I have been staring myself blind at this. I added a piece of code to the random but I am unsure what it does.
Random rndGen = new Random((int)DateTime.Now.Ticks);
To keep the precision on "random"?
|
|
|
|
|
I'm just guessing here but I think that's a seed value. And probably unnecessary since I guess the parameterless constructor does something similar anyway.
|
|
|
|
|
|
I don't think I got that. I have a comboBox and added 3 sided dice, 4 sided dice etc. using, skipped the design mode and just put it into the form, the code now looks like this.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.Items.Add("3 sided die");
comboBox1.Items.Add("4 sided die");
comboBox1.Items.Add("5 sided die");
comboBox1.Items.Add("6 sided die");
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Value";
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Clear();
var random = new RollDice();
var dice =
int result;
result = ();
richTextBox1.Text = (result).ToString();
}
}
public class RollDice
{
public static int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
int total = 0;
Random rndGen = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < numberOfTimes; i++)
{
total = total + rndGen.Next(1, numberOfSides + 1);
}
return total;
}
}
}
I am unsure how to assign "3 sided die" the value of 3, on my button click take the selected value in the comboBox1 (numberOfSides) + (numberOfTimes) from my textBox1 and call the method.
Some code in here is incomplete, the call method part and comboBox items has 0 value
|
|
|
|
|
Don't use the designer to set the values.
|
|
|
|
|
I did not use the designer, that first sentence looked a bit weird, remove "using". I ment I skipped the designer and made the code for the combobox in the form, the code I pasted above.
Don't really know how to do this, assign each dropdown a value, get the value depending on what is selected + the number of times of course that is typed to roll in the textBox1.
Never used a comboBox before.
modified 9-May-12 9:16am.
|
|
|
|
|
Ah. Don't put string literals in the ComboBox; put enumerated values in.
|
|
|
|
|
Something like this?
public Form1()
{
InitializeComponent();
ComboBox combo = new ComboBox();
combo.DataSource = Enum.GetValues(typeof(DiceValue));
}
public enum DiceValue
{
("3 sided die") = 3,
("4 sided die") = 4,
("5 sided die") = 5,
("6 sided die") = 6,
}
Gives me an error in the enum "Identifier" expected.
Commented out my last population of the comboBox if I still need to use that. Not sure if I am on the right track here.
Then if I use a enum I need to change my button that is
private void button1_Click(object sender, EventArgs e)
to a private enum?
This comboBox really made me pull some hairs but you need to learn somewhere 
|
|
|
|
|
Well you can't write an enumeration that way, but that's the idea. Did you read the article I linked to?
|
|
|
|
|
I'm sorry, I checked all your replys but I can't see a link. I really appreciate you trying to push me into the right direction though 
|
|
|
|
|
At the bottom of my first reply where it says Using human readable enum values in a ComboBox[^]
|
|
|
|
|
 Ok, reading that link you gave me I got the comboBox working but I am missing something here when I add things to my button
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Enum.GetValues(typeof(DiceValue));
}
public enum DiceValue
{
[Description("3 sided die")] Three = 3,
[Description("3 sided die")] Four = 4,
[Description("3 sided die")] Five = 5,
[Description("3 sided die")] Six = 6,
}
public class RollDice
{
public static int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
int total = 0;
Random rndGen = new Random((int)DateTime.Now.Ticks);
for (int i = 0; i < numberOfTimes; i++)
{
total = total + rndGen.Next(1, numberOfSides + 1);
}
return total;
}
}
private void button1_Click(object sender, EventArgs e)
{
var dice = new RollDice();
var numberOfSides = comboBox1.SelectedItem;
var numberOfTimes = textBox1.Text;
int result = dice(numberOfSides, numberOfTimes);
result = (dice);
richTextBox1.Text = (result).ToString();
}
}
}
dice under my button is the problem, "is a 'variable' but used as a 'method'". I thought I could call that variable from the method above?
|
|
|
|
|
Which exact line?
"Random rndGen = new Random"
Make rndGen a static field and instantiate it only once.
"(result)."
You don't need the parentheses.
|
|
|
|
|
 Ok so I moved my random out and made it a static but I still don't get how to call it all on my button. Complete code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Dice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.DataSource = Enum.GetValues(typeof(DiceValue));
}
public enum DiceValue
{
[Description("3 sided die")] Three = 3,
[Description("4 sided die")] Four = 4,
[Description("5 sided die")] Five = 5,
[Description("6 sided die")] Six = 6,
}
public class RollDice
{
public static Random rndGen = new Random((int) DateTime.Now.Ticks);
public static int RandomDice(Int32 numberOfSides, Int32 numberOfTimes)
{
int total = 0;
for (int i = 0; i < numberOfTimes; i++)
{
total = total + rndGen.Next(1, numberOfSides + 1);
}
return total;
}
}
private void button1_Click(object sender, EventArgs e)
{
var dice = new RollDice();
var numberOfSides = comboBox1.SelectedItem;
var numberOfTimes = textBox1.Text;
int result = dice(numberOfSides, numberOfTimes);
richTextBox1.Text = result.ToString();
}
}
}
Getting the feeling this should be easy but I just can't see it 
|
|
|
|
|
Why is RollDice a separate class?
Because RandomDice is static you call it as int result = RollDice.RandomDice(numberOfSides, numberOfTimes);
|
|
|
|
|
private void button1_Click(object sender, EventArgs e)
{
var numberOfSides = comboBox1.Text;
var numberOfTimes = textBox1.Text;
int result = RollDice.RandomDice(numberOfSides, numberOfTimes);
}
Gives me an error after the int result =
class Dice.Form1.RollDice
Error:
The best overloaded method match for 'Dice.Form1.RollDice.RandomDice(int, int)' has some invalid arguments
|
|
|
|
|
Angel_78 wrote: textBox1.Text
That's a string; you'll need to parse it. See Int32.TryParse
Or, better yet, don't use a TextBox, use a NumericUpDown.
|
|
|
|
|
Ok so I changed my numberOfTimes textBox1 to a numericUpDown1 and I sort of got it to work but not the way I want it to.
private void button1_Click(object sender, EventArgs e)
{
var numberOfSides = (int)comboBox1.SelectedValue;
var numberOfTimes = (int)numericUpDown1.Value;
int result = RollDice.RandomDice(numberOfSides, numberOfTimes);
richTextBox1.Text = Convert.ToString(result);
}
All I get is the total random number displayed to my richTextBox1, I want it to display each roll separately.
What am I missing here?
modified 14-May-12 17:44pm.
|
|
|
|
|
Angel_78 wrote: Convert.ToString(result);
No need for Convert; just use result.ToString() (in general, you should never need Convert, so avoid it).
Perhaps you should set a breakpoint in RandomDice and step through to see what's happening.
|
|
|
|
|
In application I made for one client I have a lots of forms with a datagridviews containing comboboxcell usually showing parent data. for example datagridview 'Orders' has as 'Partner' column with combobox cells.
application has been working for a few years now, everything works great on different computers with different MS OS ( Win XP, Win 7). on all except one computer where on exiting a form throws 'Error: DataGridViewComboBoxCell Value is not Valid '. all prerequisites are instaled correctly, I even checked regional settings, they are same as on any other computer. I don't know what else should I try.
anybody with a experience in this sort of problems?
|
|
|
|
|
markoVVVV wrote: 'Error: DataGridViewComboBoxCell Value is not Valid '.
Can you reproduce the error at will? Can you Debug.Print the value?
Bastard Programmer from Hell
|
|
|
|