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
How do I write a code that allows me to count the number of numbers that i put in a text box.
For example I have a form , a button and a text box. I type in 1 in the text box; press the button. Type in 3; press the button. Type in 5; press the button. and when I close out my form, a message box shows up saying i have 3 numbers.
code so far for form 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnReadings_Click(object sender, EventArgs e)
{
using (Form2 f2 = new Form2())
{
while (f2.ShowDialog() != DialogResult.OK)
{
this.Enabled = false;
}
this.Enabled = true;
}
}
}
form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
this.DialogResult = DialogResult.OK;
}
private void button1_Click(object sender, EventArgs e)
{
}
}
You need to handle the form's Closing event, like this:
private void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
int countOfNumbers = 0;
foreach(char c in textBox1.Text)
{
if(Char.IsDigit(c))
{
countOfNumbers += 1;
}
}
// Display a MsgBox asking the user to save changes or abort.
MessageBox.Show("Number of numbers in text box is: " + countOfNumbers.ToString());
}
Related
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 1 year ago.
Improve this question
I have a pictureBox that I use as a button. When I start the form I load it as disabled and after pressing a button I activate it, it works to change the image from disabled to activated. Then, when I disable this pictureBox again, the image doesn't change anymore ... what could be wrong?
here's my code:
private void btnUpdate_EnabledChanged(object sender, EventArgs e)
{
if (btnUpdate.Enabled == true)
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateNormal))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateNormal);
btnUpdate.BackgroundImage = bt;
}
}
else
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateDisabled))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateDisabled);
btnUpdate.BackgroundImage = bt;
}
}
}
Edit:
I changed the string to make that easier and put the entire relationated code:
string botaoUpdateNormal = "btnUpdate_normal.png", botaoUpdateDisabled = "btnUpdate_disabled.png",
botaoUpdateFocus = "btnUpdate_focus.png", botaoSearchNormal = "btnSearch_normal.png",
botaoSearchFocus = "btnSearch_focus.png", botaoInsertNormal = "btnInsert_normal.png",
botaoInsertFocus = "btnInsert_focus.png";
then i load the form:
private void IEstoque_Load(object sender, EventArgs e)
{
if (mt.ArquivoExiste(Metodos.pathImagens, botaoUpdateDisabled))
{
Image bt = Image.FromFile(Metodos.pathImagens + botaoUpdateDisabled);
btnUpdate.BackgroundImage = bt;
}
}
After that I have an event that when I change the row of a grid, the btnUpdate activates, and when I click on it and update my dataBase it desactivates.
This code work for pictureBox "like button" and with separate button:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.pictureBox.Enabled = false;
this.pictureBox.EnabledChanged += new System.EventHandler(this.pictureBox_EnabledChanged);
this.pictureBox.MouseClick += new System.Windows.Forms.MouseEventHandler(this.pictureBox_MouseClick);
this.button.Click += new System.EventHandler(this.button_Click);
this.MouseClick += new System.Windows.Forms.MouseEventHandler(this.Form1_MouseClick);
}
private void pictureBox_EnabledChanged(object sender, EventArgs e)
{
if (pictureBox.Enabled == true)
{
var botao = "1.png";
Image bt = Image.FromFile(botao);
pictureBox.BackgroundImage = bt;
}
else
{
var botao = "2.png";
Image bt = Image.FromFile(botao);
pictureBox.BackgroundImage = bt;
}
}
private void button_Click(object sender, EventArgs e)
{
pictureBox.Enabled = !pictureBox.Enabled;
}
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
if (!MouseIsPicture(e.Location)) return;
TogglePicture();
}
private void pictureBox_MouseClick(object sender, MouseEventArgs e)
{
TogglePicture();
}
private bool MouseIsPicture(Point location)
{
// Make sure the location is over the image.
if (location.X < 0) return false;
if (location.Y < 0) return false;
if (location.X >= pictureBox.Width) return false;
if (location.Y >= pictureBox.Height) return false;
return true;
}
void TogglePicture()
{
pictureBox.Enabled = !pictureBox.Enabled;
}
}
More over: check if pictures you are loading are not the same
I solve it! before deactivating the button I called a form as follows: formname.Show ();
I just changed it to formname.ShowDialog (); and it worked normally.
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 5 years ago.
Improve this question
private void opc(object sender, EventArgs e)
{
//NOTE: buttons 13-16 share same click event
// needs for each loop
button13.Enabled = false;
button14.Enabled = false;
button15.Enabled = false;
button16.Enabled = false;
}
You can try this.
private void opc(object sender, EventArgs e)
{
for (int i = 13; i <= 16; i++)
{
Control[] buttons = this.Controls.Find(String.Format("button{0}", i), false);
if (buttons.Length > 0)
{
Button btn = (buttons[0] as Button);
btn.Click += btn_Click;
btn.Enabled = true; // or false
}
}
}
void btn_Click(object sender, EventArgs e)
{
MessageBox.Show((sender as Button).Name);
}
See code below :
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button13.Click += new System.EventHandler(this.button_Click);
this.button14.Click += new System.EventHandler(this.button_Click);
this.button15.Click += new System.EventHandler(this.button_Click);
this.button16.Click += new System.EventHandler(this.button_Click);
}
private void button_Click(object sender, EventArgs e)
{
Button button = sender as Button;
string name = button.Text;
}
}
}
You want to put the assignmens into a loop? With statically defined names for the buttons that is not possible AFAIK. You should put the buttons into an array or collection in the first place:
Button[] button = new Button[numberOfButtons];
for (int i=0; i<numberOfButtons; i++)
{
button[i] = new Button();
button[i].Click += new System.EventHandler(WhateverYouLike);
// ... some other property assignments, probably also rule-based
}
// ... and at a later time:
for (int i=0; i<numberOfButtons; i++) button[i].Enabled = false;
I'm new to C# and I need this function for a program im working on for school. I need to make a new window pop up when i click a button, not a message box though like a forms window, one that i can design with text boxes and buttons. What is on the new pop up window depends on the previous window but i can figure that out.
Also I need a way to close the previous window once the new one appears
Here's my code:`
// This makes sure only one box is checked
private void MulCB_CheckedChanged(object sender, EventArgs e)
{
if( MulCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void DivCB_CheckedChanged(object sender, EventArgs e)
{
if (DivCB.Checked == true)
{
MulCB.Checked = false;
AddCB.Checked = false;
SubCB.Checked = false;
}
}
private void AddCB_CheckedChanged(object sender, EventArgs e)
{
if (AddCB.Checked == true)
{
DivCB.Checked = false;
SubCB.Checked = false;
MulCB.Checked = false;
}
}
private void SubCB_CheckedChanged(object sender, EventArgs e)
{
if (SubCB.Checked == true)
{
DivCB.Checked = false;
AddCB.Checked = false;
MulCB.Checked = false;
}
}
private void oneDCB_CheckedChanged(object sender, EventArgs e)
{
if(oneDCB.Checked == true)
{
twoDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void twoDCB_CheckedChanged(object sender, EventArgs e)
{
if ( twoDCB.Checked == true)
{
oneDCB.Checked = false;
threeDCB.Checked = false;
}
}
private void threeDCB_CheckedChanged(object sender, EventArgs e)
{
if (threeDCB.Checked == true)
{
oneDCB.Checked = false;
twoDCB.Checked = false;
}
}
// ends here
// Button operation
private void button8_Click(object sender, EventArgs e)
{
var form = new Form();
}
}
}
`
Thanks a lot!
Sal
The project is im supposed to make a quizzing program for kids. They should be able to choose 1 operation and the amount of digits the numbers will have. It then has to out put 10 random questions according to the selection made by the kid, then once they have completed the quiz, it should display their results and which questions they got wrong.
Assuming that the design of the window doesn't have to be completely dynamic, you can design it in Visual Studio (I'm assuming you did so with the first one). Then you can pass the results to the window. Like:
// Note: Form2 ist the name of your designed From
Form2 myform = new Form2();
this.Hide();
//You could pass the question settings like this
// 1 is for multiplication, 2 for division,3 for addition, 4 for substraction
myform.operation=1;
myform.digits=2
myform.Show();
And in the code of Form2:
namespace Yournamespace {
public partial class Form2: Form {
//Add these two lines about here
public static int operation;
public static int digits;
public Form2() {
InitializeComponent();
}
}
}
Then you can use the variables in Form2 and fill in the textbox or other elements you might design.
Also: You cloud use radio buttons instead of checkboxes as you then won't have you worry about unchecking the other checkboxes.
This question already has answers here:
Right click to select a row in a Datagridview and show a menu to delete it
(12 answers)
Closed 9 years ago.
i have a form1 in which my datagridview is located. my form2 has a textbox in which the value is taken from the datagridview of form1. i have inserted a contextmenustrip in my datagridview which will then take the data from the selected row and pass it on to form2. i have only done this in a listview. this is how i did it in listview
form1:
private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
{
strinf dis = listView1.Items[listView1.FocusedItem.Index].SubItems[0].Text;
int r = Convert.ToInt32(dis);
form2 nf2 = new form2(r);
nf2.ShowDialog();
}
form 2:
public Form2(int g)
{
InitializeComponent();
textBox1.text = g.ToString();
}
how can i do this in a datagridview?
Solution is very well described here
private void DataGridViewMouseDownHandler(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Right)
{
var hti = dataGridView.HitTest(e.X, e.Y);
dataGridView.ClearSelection();
dataGridView.Rows[hti.RowIndex].Selected = true;
}
}
this.dataGridView.MouseDown += new System.Windows.Forms.MouseEventHandler(this.DataGridViewMouseDownHandler);
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 9 years ago.
Improve this question
I want to restrict a textbox to accept only numbers in C#. How do I do that?
The most crude down and dirty way of doing it is doing something like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = !char.IsDigit(e.KeyChar);
}
}
You're still not safe with this approach, as users can still copy and paste non-numeric characters into the textbox. You still need to validate your data regardless.
From others have said before me, we have to almost know what you will use this in, WinForms, ASP.NET, Silverlight ...
But now I take a chance that it is WinForm:)
private void TxtBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
e.Handled = true;
}
has some bugs but easy way :D
private void textbox1_TextChanged(object sender, EventArgs e)
{
char[] originalText = textbox1.Text.ToCharArray();
foreach (char c in originalText)
{
if (!(Char.IsNumber(c)))
{
textbox1.Text = textbox1.Text.Remove(textbox1.Text.IndexOf(c));
lblError.Visible = true;
}
else
lblError.Visible = false;
}
textbox1.Select(textbox1.Text.Length, 0);
}
Have you tried the MaskedTextBox control in WinForms?
Have a look at something like this
Masked C# TextBox Control
This may not be the best way, but works ok for WPF TextBox...
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
string originalText = ((TextBox)sender).Text.Trim();
if (originalText.Length>0)
{
int inputOffset = e.Changes.ElementAt(0).Offset;
char inputChar = originalText.ElementAt(inputOffset);
if (!char.IsDigit(inputChar))
{
((TextBox)sender).Text = originalText.Remove(inputOffset, 1);
}
}
}
You can use the FilterTextBox of AJAX Control Toolkit. Using this you can allow/disallow any character type. This is quite flexible.
You can use the Microsoft.VisualBasic.Information.IsNumeric is numeric function. Just add the reference Microsoft.VisualBasic
private void textBox1_Validating(object sender,
System.ComponentModel.CancelEventArgs e) {
if (!Microsoft.VisualBasic.Information.IsNumeric(textBox1.Text)) {
e.Cancel = true;
} else {
// Do something here
}
}
This allows the user to enter scientific notation which is not trivial to filter for.
Not sure if this is the correct way, but it works for me, running it on the TextChanged event TextBox has:
private void CoordinateValidation(object sender, TextChangedEventArgs e) {
TextBox inputBox = e.OriginalSource as TextBox;
inputBox.TextChanged -= CoordinateValidation;
int caretPos = inputBox.CaretIndex;
foreach (TextChange change in e.Changes) {
if (inputBox.Text.Substring(change.Offset, change.AddedLength).Any(c => !ValidChars.Contains(c)) ||
inputBox.Text.Count(c => c == '.') > 1 ||
(inputBox.Text.Length > 0 && inputBox.Text.Substring(1).Contains('-'))) {
inputBox.Text = inputBox.Text.Remove(change.Offset, change.AddedLength);
caretPos -= change.AddedLength;
}
}
inputBox.CaretIndex = caretPos;
inputBox.TextChanged += CoordinateValidation;
}
Have a look at the below code
private void txtbox1_KeyPress(object sender, KeyPressEventArgs e)
{
const char Delete = (char)8;
e.Handled = !Char.IsDigit(e.KeyChar) && e.KeyChar != Delete;
}
This will be helpful but users can Copy and Paste characters :-)