Random number will not show up in text box [closed] - c#

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
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 RollTheDice
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnDice_Click(object sender, EventArgs e)
{
int Roll;
Random rand = new Random();
Roll = rand.Next(0,10);
diceDisplay.Text = btnDice.ToString();
}
}
}
Not able to output number to the text field, there is no error it just will not work.
When the button is clicked system. error does show on it but does not show on the build

You are not assigning random number to the TextBox but assigning the Button.ToString:
int Roll;
Random rand = new Random();
Roll = rand.Next(0,10);
diceDisplay.Text = Roll.ToString(); //modified
Also precise your code and replace with just one line:
diceDisplay.Text = new Random().Next(0,10).ToString();

You should use roll.ToString() instead of btnDice.ToString();

Related

how do i make it so that instead of names already being in the array that the user has to input names into the array?

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 namegenerator1._2
{
public partial class Form1 : Form
{
string[] FirstNames = { "jan", "jaap", "sjuul", "koen" };
Random rand = new Random();
public Form1()
{
InitializeComponent();
}
private void btn_generate_Click(object sender, EventArgs e)
{
int indexFirstName = rand.Next(FirstNames.Length);
this.txtbox_FirstName.Text = FirstNames[indexFirstName];
}
}
}
This basically makes a little box that generates random names from the array. I need to make it so that before the user gets to that he needs to put in a bunch of names himself. Which I don't know how to do.
You either check the array length to see if there are enough games, or use list as suggested.
If the array.Length is not enough, either use Input box or TextBox to ask for user input.
The input will go in the array/list with list.Add or by rearranging the array

Unregistering hotkeys at runtime with MouseKeyHook [closed]

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 2 years ago.
Improve this question
I want to allow users to re-assign hotkeys at runtime and am using the Gma.System.MouseKeyHook NuGet package.
Creating new hotkeys at runtime works just fine and dandy but clearing the Action of an already assigned Combination/Sequence (by setting its Action = null) does not remove the original assignment.
Is there some way to do this on a per Combination/Sequence basis? More specifically I want to avoid calling Dispose() on my Hook.GlobalEvents() reference and having to re-initialize the whole systems assignments.
Any help would be greatly appreciated :)
If you want to unsubscribe from keypress event:
_globalHook = Hook.GlobalEvents();
_globalHook.KeyPress += GlobalHookKeyPress; //Subscribe
_globalHook.KeyPress -= GlobalHookKeyPress; //Unsubscribe
Edit:
I understand now that you called OnCombination.
After going over the code of this method, you cannot change the combinations list after you created it. Other calls to OnCombination will just add more registrations.
Edit:
Another option is to use reactive extensions:
using System;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reactive.Linq;
using Gma.System.MouseKeyHook;
using MouseKeyHook.Rx;
namespace HotkeyPlay
{
public partial class Form1 : Form
{
private IDisposable _keysObservable;
public Form1()
{
InitializeComponent();
var triggers = new Trigger[]
{
Trigger.On(Keys.H).Alt().Shift()
};
_keysObservable =
Hook
.GlobalEvents()
.KeyDownObservable()
.Matching(triggers)
.Subscribe((trigger) =>
{
Debug.WriteLine(trigger.ToString());
});
}
private void button1_Click(object sender, EventArgs e)
{
_keysObservable.Dispose();
var triggers = new Trigger[]
{
Trigger.On(Keys.B).Alt().Shift()
};
_keysObservable =
Hook
.GlobalEvents()
.KeyDownObservable()
.Matching(triggers)
.Subscribe((trigger) =>
{
Debug.WriteLine(trigger.ToString());
});
}
}
}
Nuget packages:
Install-Package System.Reactive.Linq
Install-Package MouseKeyHook.Rx
MouseKeyHook.Rx is still in pre-release version.

Adding text box to form [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I am trying to add a dynamic text box to my windows form with the following code. It's all fine up until the last line:
Form1.Controls.Add(dynamicTextBox);
I'm getting the error:
"An object reference is required for the non-static field, method, or
property Control.Controls."
I'm not sure what I'm doing wrong here and would appreciate any help possible!
//create a text box
TextBox dynamicTextBox = new TextBox();
dynamicTextBox.Text = "Text";
dynamicTextBox.Name = "textBox1";
dynamicTextBox.Enabled = false;
dynamicTextBox.Multiline = true;
dynamicTextBox.Height = 80;
dynamicTextBox.Width = 300;
dynamicTextBox.Location = new System.Drawing.Point(234, 279);
Form1.Controls.add(dynamicTextBox);
The following code adds an text box to the form
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
TextBox txtBox;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
txtBox = new TextBox();
txtBox.Location = new Point(10, 50);
txtBox.Visible = true;
Controls.Add(txtBox);
}
}
}

Use of unassigned local variable "strb" StringBuilder [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
So I am trying to learn how to use the StringBuilder Class. I read up about it and it seems amazing compared to string !
I am trying to create the StringBuilder in the other button but it keeps throwing me the error:
; expected" and Use of unassigned local variable "strb"
on line 42 & 43.
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 WindowsFormsApplication15
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string nrmlString = "C#";
nrmlString += " This";
nrmlString += " is";
nrmlString += " a";
nrmlString += " Test";
nrmlString += " Thisss";
MessageBox.Show(nrmlString);
}
private void button2_Click(object sender, EventArgs e)
{
StringBuilder strb new StringBuilder("something");
strb.Append("Something else");
MessageBox.Show(strb.ToString());
}
}
}
You missed one = in your button2_Click:
StringBuilder strb = new StringBuilder("something");
strb.Append("Something else");
#VargaDev,
Your error was a simple missing of the = in your code.
StringBuilder strb = new StringBuilder("something");
And it has already been answered. I just chimed in for a suggestion. When trying this type of codes, using VS and creating a form is cumbersome. Have a check at the wonderful (and free) utility LinqPad. You can use that as a code scratch pad. ie: For testing your code above, you would simply do this:
-Choose C# statements (or C# program) from the combo
-Type
StringBuilder strb = new StringBuilder("something");
strb.Append("Something else");
strb.ToString().Dump();
and hit F5. That is it! Dump() is on streoids. You can dump almost anything, it shows the result in a suitable way (a datagrid for example if it is a list like thing).
PS: I don't have any affiliation with the author of the utility (Joseph Albahari), just a lover of it. It is (and he is) worth to be appraised.

C# The name does not exist in the current context [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am trying to learn programming and I am starting with a book called Software Development Fundamentals. However I am having loads of difficulty understanding certain subjects. Especially because my native language is not English. I am stuck at the subject (events) and (delegates). I feel like this is to difficult for me, I can not even get this code to work!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lesson02
{
class Program
{
static void Main(string[] args)
{
Rectangle r = new Rectangle();
r.Changed += new EventHandler(r_Changed);
r.Length = 10;
}
static void r_changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine(
"Value Changed: Length = {0}",
r.Length);
}
}
class Rectangle
{
public EventHandler Changed;
private double length;
public double Length
{
get
{
return length;
}
set
{
length = value;
Changed(this, EventArgs.Empty);
}
}
}
}
I get this error:
Error 1 The name 'r_Changed' does not exist in the current context 14 59 Lesson02
C# is case-sensitive language. You have defined function as r_changed and using it as r_Changed
Use
r.Changed += new EventHandler(r_changed);
instead of
r.Changed += new EventHandler(r_Changed);
I'm pretty sure you'd know by now that C# is a case sensitive programming language.
This should work
static void r_Changed(object sender, EventArgs e)
{
Rectangle r = (Rectangle)sender;
Console.WriteLine("Value Changed: Length = {0}", r.Length);
}
Notice how r_Changed is capitals (r_changed is what you originally defined)
I would suggest using this because it is easier to read.
There is a little typo mistake in your code. It should be r_Changed instead of r_changed in the your Event Handler.
i.e write
static void r_Changed(object sender, EventArgs e)
in place of
static void r_changed(object sender, EventArgs e)
(Remember C# is Case-sensitive)

Categories