C# textbox access - c#

I'm having an irritating problem with textbox. I enter data in one textbox and with the click of the button i need to display result in another textbox. I really dont know why my method wouldnt work. I dont get any errors, but it wouldnt display the result. Thanks!
All of the textboxes are dynamically created during the run time.
here is the code:
private void button2_Click(object sender, EventArgs e)
{
TextBox tbox8 = new TextBox();
tbox8.Name = "textBox8";
tbox8.Location = new System.Drawing.Point(54 + (0), 55);
tbox8.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox8);
tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;
tbox8.TextChanged += new EventHandler(tbox8_TextChanged);
TextBox tbox9 = new TextBox();
tbox9.Name = "textBox9";
tbox9.Location = new System.Drawing.Point(54 + (60), 55);
tbox9.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox9);
tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption;
tbox9.TextChanged += new EventHandler(tbox9_TextChanged);
}//button_click
//input data into texbox8
TextBox tbox;
double var1;
private void tbox8_TextChanged(object sender, EventArgs e)
{
tbox = sender as TextBox;
var1 = Convert.ToDouble(tbox.Text);
}
//display the result in textbox9
TextBox tbox2;
private void tbox9_TextChanged(object sender, EventArgs e)
{
tbox2 = sender as TextBox;
}
//perform calculation and on button click display data on referenced textbox
private void button3_Click(object sender, EventArgs e)
{
double result2 = var1 * 2;
if( tbox2 != null)
{
tbox2.Text = result2.ToString();
}
}

Here is your code with the unnecessary bits commented out, and some new lines added:
TextBox tbox8 = new TextBox(); //make it a member variable
TextBox tbox9 = new TextBox(); //same for this one
private void button2_Click(object sender, EventArgs e)
{
tbox8.Name = "textBox8";
tbox8.Location = new System.Drawing.Point(54 + (0), 55);
tbox8.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox8);
tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;
tbox8.TextChanged += new EventHandler(tbox8_TextChanged);
tbox9.Name = "textBox9";
tbox9.Location = new System.Drawing.Point(54 + (60), 55);
tbox9.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox9);
tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption;
tbox9.TextChanged += new EventHandler(tbox9_TextChanged);
}//button_click
//input data into texbox8
//TextBox tbox;
double var1;
private void tbox8_TextChanged(object sender, EventArgs e)
{
//tbox = sender as TextBox;
var1 = Convert.ToDouble(tbox8.Text);//tbox8
}
//display the result in textbox9
//TextBox tbox2;//tbox2 is actually tbox9 anyway
private void tbox9_TextChanged(object sender, EventArgs e)
{
// tbox2 = sender as TextBox;//so we don't need a tbox2
//do something
}
//perform calculation and on button click display data on referenced textbox
private void button3_Click(object sender, EventArgs e)
{
double result2 = var1 * 2;
//if( null != tbox9 )
//{
tbox9.Text = result2.ToString(); //changed to tbox9
//}
}
However, this still has problems. What happens when the user clicks button2 more than once?

Assuming tbox8 (try using meaningful names BTW) is the one that the text is supposed to be entered and tbox14 is where you expect the output to go, why do you only set the tbox2 variable in the TextChanged event of tbox14? You code probably isn't working because tbox2 is always null, because you never execute the TextChanged event for tbox14. And why are you using the variable tbox2 instead of just using tbox14 anyway?
Also, parsing the text in the TextChanged event of tbox8 is probably the wrong place to do it. The TextChanged event will fire on every keystroke. Why not just do it once in the button3 Click handler? Or, do it when it loses focus.
Try this (note: not that this isn't still crappy code). First, get rid of all the TextChanged events, then:
private void button3_Click(object sender, EventArgs e)
{
var1 = Convert.ToDouble(tbox8.Text);
double result2 = var1 * 2;
tbox14.Text = result2.ToString();
}

Something like this should work assuming result * 2 doesn't cause an arithmetic overflow for a double. If the value entered into tbox8 can't be parsed into a double, the result displayed in tbox9 after clicking button3 will be 0.
Since I'm not using any of the events of the TextBoxes, I don't need to create handlers for them. I'm also disabling button2 after it is clicked so that it can't be clicked again.
If you need to click button2 again, you'll need to enable it from another button event handler or something so that you have the chance to remove tbox8 and tbox9 from the this.Controls collection before enabling it (button2) to prevent the application from attempting to create additional copies of them.
private void button2_Click(object sender, EventArgs e) {
button2.Enabled = false;
TextBox tbox8 = new TextBox();
tbox8.Name = "textBox8";
tbox8.Location = new System.Drawing.Point(54 + (0), 55);
tbox8.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox8);
tbox8.BackColor = System.Drawing.SystemColors.InactiveCaption;
TextBox tbox9 = new TextBox();
tbox9.Name = "textBox9";
tbox9.Location = new System.Drawing.Point(54 + (60), 55);
tbox9.Size = new System.Drawing.Size(53, 20);
this.Controls.Add(tbox9);
tbox9.BackColor = System.Drawing.SystemColors.InactiveCaption; }
private void button3_Click(object sender, EventArgs e) {
double result = 0;
double.TryParse(tbox8.Text, out result);
tbox9.Text = (double)(result * 2).ToString(); }

Related

C# how do I make my label from an label array disappear when clicked on?

My code makes 5 labels appear with a random .Left location, you can see it.
I want the particular label to disappear when I click on it, but I don't know how to tell it to my click void.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label [] kubeliai = new Label [5];
int poz = 100;
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < kubeliai.Length; i++)
{
kubeliai[i] = new Label();
Controls.Add(kubeliai[i]);
Random pos = new Random();
kubeliai[i].Top = 50;
kubeliai[i].Left = poz;
poz += pos.Next(50, 200);
kubeliai[i].BackColor = Color.Red;
kubeliai[i].Height = 20;
kubeliai[i].Width = 20;
kubeliai[i].Click += new EventHandler(kubelio_clickas);
}
}
void kubelio_clickas (object sender, EventArgs e)
{
}
}
The instance of "clicked" label is in sender parameter:
void kubelio_clickas (object sender, EventArgs e)
{
Label clickedLabel = sender as Label;
if (clickedLabel != null) {
clickedLabel.Visible = false;
}
}
Because in .NET Event Handlers by default use object as type of sender you have to cast it to Label first.
I want the particular label to disappear when I click on it
Just set the label's .Visible property to false:
void kubelio_clickas (object sender, EventArgs e)
{
if (sender is Label)
((Label)sender).Visible = false;
}
The object sender is a reference to the object which fired the event. So basically, the sender is the object you are looking for.
You just need to set it invisible:
((Label)sender).Visible = false;

Get values of dynamically created controls (comboboxes)

I've got panel on which by default are two comboboxes and one "+" button which creates two new combo boxes bellow the first one, I can create multiple (n) rows with two combo boxes and everything is working, I just can't figure out how to get values of those boxes?
Here's code for creating (adding) controls
private void btnCreateFilter_Click(object sender, EventArgs e)
{
y += comboBoxHeight;
ComboBox cb = new ComboBox();
cb.Location = new Point(x, y);
cb.Size = new Size(121, 21);
panelFiltri.Controls.Add(cb);
yDrugi += comboBoxHeight;
ComboBox cbSql = new ComboBox();
cbSql.Location = new Point(xDrugi, yDrugi);
cbSql.Size = new Size(121, 21);
panelFiltri.Controls.Add(cbSql);
btnCancel.Location = new Point(btnCancel.Location.X, btnCancel.Location.Y + 25);
btnSaveFilter.Location = new Point(btnSaveFilter.Location.X, btnSaveFilter.Location.Y + 25);
}
And here's code where I'm lost:
private void btnSaveFilter_Click(object sender, EventArgs e)
{
int i;
foreach (Control s in panelFiltri.Controls)
{
//GOT LOST
}
}
You can get the text in the ComboBox as
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (Control control in panelFiltri.Controls)
{
if (control is ComboBox)
{
string valueInComboBox = control.Text;
// Do something with this value
}
}
}
I don't really know what you're trying to achieve... Maybe this will help you along...
private void btnSaveFilter_Click(object sender, EventArgs e)
{
foreach (ComboBox comboBox in panelFiltri.Controls)
{
var itemCollection = comboBox.Items;
int itemCount = itemCollection.Count; // which is 0 in your case
}
}

Can't refer to Text propery of textbox from TextChanged eventhandler

I have a problem with getting the Text value of a textbox in the TextChanged event handler.
I have the following code. (simplified)
public float varfloat;
private void CreateForm()
{
TextBox textbox1 = new TextBox();
textbox1.Location = new Point(67, 17);
textbox1.Text = "12.75";
textbox1.TextChanged +=new EventHandler(textbox1_TextChanged);
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
varfloat = float.Parse(textbox1.Text);
}
I get the following error:'the name textbox1 does not exist in the current context'.
I probably made a stupid mistake somewhere, but I'm new to C# and would appreciate some help.
Thanks in advance!
You've declared textBox1 as a local variable within CreateForm. The variable only exists within that method.
Three simple options:
Use a lambda expression to create the event handler within CreateForm:
private void CreateForm()
{
TextBox textbox1 = new TextBox();
textbox1.Location = new Point(67, 17);
textbox1.Text = "12.75";
textbox1.TextChanged +=
(sender, args) => varfloat = float.Parse(textbox1.Text);
}
Cast sender to Control and use that instead:
private void textbox1_TextChanged(object sender, EventArgs e)
{
Control senderControl = (Control) sender;
varfloat = float.Parse(senderControl.Text);
}
Change textbox1 to be an instance variable instead. This would make a lot of sense if you wanted to use it anywhere else in your code.
Oh, and please don't use public fields :)
Try this instead :
private void textbox1_TextChanged(object sender, EventArgs e)
{
varfloat = float.Parse((sender as TextBox).Text);
}
Define the textbox1 out side CreateForm() at class level scope instead of function scope, so that it is available to textbox1_TextChanged event.
TextBox textbox1 = new TextBox();
private void CreateForm()
{
textbox1.Location = new Point(67, 17);
textbox1.Text = "12.75";
textbox1.TextChanged +=new EventHandler(textbox1_TextChanged);
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
varfloat = float.Parse(textbox1.Text);
}
You have not added the textbox control to the form.
It can be done as
TextBox txt = new TextBox();
txt.ID = "textBox1";
txt.Text = "helloo";
form1.Controls.Add(txt);

Address a generated button for clickevent

I have a class that creates panels with controls based on my database. It creates a panel with a button on each panel, per row in DB. How do I address one specific button to make a click event?
I'm a rookie, and maybe abit over my head, but you don't learn to swim in shallow water ;)
Any help appreciated!
while (myDataReader.Read())
{
i++;
Oppdrag p1 = new Oppdrag();
p1.Location = new Point (0, (i++) * 65);
oppdragPanel.Controls.Add(p1);
p1.makePanel();
}
class Oppdrag : Panel
{
Button infoBtn = new Button();
public void makePanel()
{
this.BackColor = Color.White;
this.Height = 60;
this.Dock = DockStyle.Top;
this.Location = new Point(0, (iTeller) * 45);
infoBtn.Location = new Point(860, 27);
infoBtn.Name = "infoBtn";
infoBtn.Size = new Size(139, 23);
infoBtn.TabIndex = 18;
infoBtn.Text = "Edit";
infoBtn.UseVisualStyleBackColor = true;
}
}
You'll need a method that matches the event thrown by clicking on the button.
i.e.)
void Button_Click(object sender, EventArgs e)
{
// Do whatever on the event
}
Then you'll need to assign the click event to the method.
p1.infoBtn.Click += new System.EventHandler(Button_Click);
Hope this helps.
You can add the event handler for the button when you create the button. You can even add a unique CommandArgument per button so you can distinguish one button from another.
public void makePanel()
{
/* ... */
infoBtn.UseVisualStyleBackColor = true;
infoBtn.Click += new EventHandler(ButtonClick);
infoBtn.CommandArgument = "xxxxxxx"; // optional
}
public void ButtonClick(object sender, EventArgs e)
{
Button button = (Button)sender;
string argument = button.CommandArgument; // optional
}

How would you code many buttons into one Method?

I'm trying to fix a computer program in C# for the game MasterMind. Right now, there is a lot of unnecessary code for the buttons. I know i can put them all into one method but i don't know how. Here is some of the code. Please help.
private void button1_Click(object sender, EventArgs e)
{
this.ActiveControl.BackColor = controlColor;
this.ActiveControl.Text = controlNumber;
allCellsClicked[0] = '1';
if (all_Buttons_Clicked())
{
allCellsClicked[0] = '0';
allCellsClicked[1] = '0';
allCellsClicked[2] = '0';
allCellsClicked[3] = '0';
button04.Enabled = false;
button03.Enabled = false;
button02.Enabled = false;
button01.Enabled = false;
guess++;
Label1.Text = "Guess Number " + Convert.ToString(guess);
label4.Visible = true;
label5.Visible = true;
label4.Text = "0";
label5.Text = "0";
int a = int.Parse(button01.Text), b = int.Parse(button02.Text), c = int.Parse(button03.Text), d = int.Parse(button04.Text);
int rightCol, rightPos;
CheckAnswer(a, b, c, d, out rightPos, out rightCol);
label4.Text = rightCol.ToString();
label5.Text = rightPos.ToString();
If what you are saying is that all of the buttons execute essentially the same code but it is copied multiple times, then you can look to the place where each button's click event is being hooked up to each of their event handlers and point them all to the same method.
Somewhere (probably in your filename.Designer.cs), you have something like this:
button1.Click += new EventHandler(button1_click);
To change this, you can make each of them like this (note that this does not need to be put in the Designer.cs file and that it is not recommended to manually edit this file):
button1.Click += new EventHandler(button_click);
button2.Click += new EventHandler(button_click);
button3.Click += new EventHandler(button_click);
button4.Click += new EventHandler(button_click);
...
where you have a method defined like this:
private void button_Click(object sender, EventArgs e)
{
// stuff that happens when a button is clicked
}
This will make all of the buttons use the same button_click event handler. If you need to know which button fired the event, you can check the sender's id:
Button buttonThatClicked = sender as Button;
if (buttonThatClicked != null)
{
// do whatever you need to, based on the button's properties
}
If you have duplicate code in several button_click methods just create a separate method and have the button_click methods call it.
private void button1_Click(object sender, EventArgs e)
{
this.ActiveControl.BackColor = controlColor;
this.ActiveControl.Text = controlNumber;
allCellsClicked[0] = '1';
checkGuess();
}
private void button2_Click(object sender, EventArgs e)
{
this.ActiveControl.BackColor = controlColor;
this.ActiveControl.Text = controlNumber;
allCellsClicked[0] = '2';
checkGuess();
}
private void checkGuess(){
if (all_Buttons_Clicked())
{
allCellsClicked[0] = '0';
allCellsClicked[1] = '0';
allCellsClicked[2] = '0';
allCellsClicked[3] = '0';
button04.Enabled = false;
button03.Enabled = false;
button02.Enabled = false;
.....
}
You can just associate the same Click event callback function with every button's Click event. You can do this via code or you can do this via the Properties pane. Simply select each button and then pick the same event handler for each one.
In your case, if you want every button to use the button1_Click event, then associate that button1_Click event with each button. If you do that, you might want to rename the event callback function to something more generic.

Categories