Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
My program goes like this:
I have a public "main method" of the class, which got a data table with values.
I want that when the user will press a specific button, I will calculate something in the event of the button with the data table from the "main method" of the class (public classname()).
There is anyway to do that?
Here is a quick example:
using System.Data;
...
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataTable dt;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
dt = new DataTable();
//load your table here
...
}
// Create an event (maybe via the designer) for button click...
private void button1_Click(object sender, EventArgs e)
{
//you can reference the datatable here, for example tell how many rows it has
MessageBox.Show(dt.Rows.Count.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 6 years ago.
Improve this question
I have a an app. Form 1 has a button which was set to open an instance of form2. I got some funny behaviour with the form reopening itself. So i deleted the code from that button on form1. Still when i click on that button, its opening the form2. Any idea??
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Application.Exit();
}
Try Clean, then rebuild the project.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I have a massive problem with a project im working on. I am trying to make a WFA which will take input from user and then the user will choose to either add what they typed using a hashtable or delete something out of that hashtable by using the add and remove buttons...
Im really struggling on how to add the user input to the hashtable??
someone please help!!!
namespace Lab6_Library2 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonAdd_Click(object sender, EventArgs e)
{
Hashtable books = new Hashtable();
books = textBoxInput.Text;
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
}
private void buttonView_Click(object sender, EventArgs e)
{
MessageBox.Show("All The Books Added are: \n" + textBoxInput);//+ );
}
If you'd like to keep the collection content, you have to move it to class level:
public partial class Form1 : Form
{
HashSet<string> books = new HashSet<string>();
// (...)
}
I used generic HashSet<string> here, because it's the right one to use in your case.
To add item to HashSet instance, use Add method:
private void buttonAdd_Click(object sender, EventArgs e)
{
books.Add(textBoxInput.Text);
}
To remove items, use Remove method:
books.Remove(textBoxInput.Text);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I made a textbox of name tbFirstNumber
When i enter any value in that textbox , i want that Value to dissapear
I am using C# window form application in visual studio 2008
private void tbFirstNumber_TextChanged(object sender, EventArgs e)
{
}
tbFirstNumber.Text = "";
Just set it to empty on text changed event.
If you want that user is not allowed to enter any text, you can make the textbox as read only.
Set the TextBox control's ReadOnly property to true.
tbFirstNumber.ReadOnly = true;
Another way would be to hook in to the KeyPress event.
private void tbFirstNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
// Stop the character from being entered into the control
e.Handled = true;
}
private void tbFirstNumber_TextChanged(object sender, EventArgs e)
{
tbFirstNumber.Text = "";
}
Clear the value of the text box
this.tbFirstNumber.Text = ""
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm using c# 2008 and i want to know if its possible to create(or display) a new button in an if statement after something happened. eg. if a certain label displays text, then a button must be created. If anyone can help, it will greatly be appreciated.
This code show you how to create and display a new button when double click mouse on the form:
public partial class Form1 : Form
{
private Button button1 = null;
public Form1()
{
InitializeComponent();
}
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (button1 == null)
{
button1 = new Button();
button1.Text = "New Button";
button1.Location = new System.Drawing.Point(10, 10);
button1.Size = new System.Drawing.Size(150, 30);
button1.Click += new System.EventHandler(button1_Click);
this.Controls.Add(button1);
}
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Button clicked.");
}
}
Note: this.Controls.Add(button1); add the button1 to the Form1. You also using this Controls property of other control to add a control to another control.
See more details:
http://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.control.controls(v=vs.100).aspx
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 have a button that when pressed is currently outputting messages to a messagebox. Its not very tidy so I created a new form and placed a text box in there. While the 2nd form picks up the data it presents it in another messagebox rather then the textbox like intended. Help is appreciated.
Form2
public Form2(string strTextBox)
{
InitializeComponent();
textBox1.Text = strTextBox;
}
Form 1
private void SaveButton_Click(object sender, EventArgs e)
{
foreach (string error in errorSet)
{
Form2 frm = new Form2(error);
frm.Show();
}
}
There is some more logic in the button_click if it looks a little strange but its pretty redundant for the problem im having.
Thank you
You should loop over your errors collection and store them in a StringBuilder, then, show the Form2 only one time outside the loop (if there is at least an error).
Do not forget to make your textbox MultiLine=true, give it enough height and a vertical scroll bar.
private void SaveButton_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (string error in errorSet)
sb.AppendLine(error);
if(sb.Lenght> 0)
{
Form2 frm = new Form2(sb.ToString());
frm.Show();
}
}