This is a sign-up form with 3 textboxes and one dateTimePicker. There also have to be 4 buttons(add, show, prev and next) - when you click add button, it's supposed to store data typed by an each user and show button must print out information related to each one to the labels, then I should be able to walk through on eveyone's data when I click on prev and next buttons.
Simple as it sounds but I don't know how to assign values from textboxes into labels using ArrayList collections. How to do that?
ArrayList list = new ArrayList();
int ind = 0;
private void Add_Click(object sender, EventArgs e)
{
list.Add(textBox1.Text);
list.Add(textBox2.Text);
list.Add(textBox3.Text);
list.Add(dateTimePicker1.Text);
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}
private void button3_Click(object sender, EventArgs e)
{
{
label5.Text =
label6.Text =
label7.Text =
label8.Text =
Related
When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
{
textBox2.Focus();
}
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
{
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
}
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
{
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
}
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
{
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
}
I have a certain number of Textboxes, I need to track the last two focused Texboxes. This is the approch that I attempted.
private Control _focusedControl;
private Control _lastfocusedControl;
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
This is not working because when I press the button the contenent of the _lastfocusedControl will be the same as _focusedControl because another control was focused by clicking that button.
You can handle Enter event of all those TextBox controls using a single handler and in the handler and keep track of last n focused TextBox controls:
const int n = 2;
TextBox[] textBoxes = new TextBox[n];
private void textBox_Enter(object sender, EventArgs e)
{
var destination = new TextBox[n];
Array.Copy(textBoxes, 1, destination, 0, textBoxes.Length - 1);
textBoxes = destination;
textBoxes[textBoxes.Length - 1] = (TextBox)sender;
}
In above example, we are shifting the array to left, then assign the sender to the last element. This way the array always contains the last n focused TextBox controls for you.
I would suggest, to make it simpple, do something like when your textbox get focused, put the name of it into an array, for example, and after that, list the last two name added of the array created.
So i have a datagridview which is populated with a list of objects.
I added on the form 3 textboxes and a button. The question is how to insert and populate another row into the datagridview with the text from textboxes.
this is my class:
class Professor
{
private int id;
private string name;
private double salary;
public Professor()
{
this.id= 0;
this.name = null;
this.salary= 0;
}
public Professor(int m, string n, double s)
{
this.id= m;
this.name = n;
this.salary= s;
}
}
This is the declaration for the list:
ArrayList listProfessors = new ArrayList();
This is the button which populates the DataGridView:
private void addInGridViewFromList_Click(object sender, EventArgs e)
{
string linie;
System.IO.StreamReader file= new System.IO.StreamReader("D:\\Profesor\\Profesor\\Profesori.txt");
while ((line= file.ReadLine()) != null)
{
string[] t = line.Split(',');
listProfessors .Add(new Professor(Convert.ToInt32(t[0]), t[1], Convert.ToDouble(t[2])));
}
file.Close();
dataGridView1.DataSource = listProfessors ;
}
And here on this button i want to add another row manually(using texboxes) into the DataGridView.
private void AddFromKeyboard_Click(object sender, EventArgs e)
{
}
You need to create a button event that do what you want, and when the button pressed you add a new row with the textboxes you want.
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.Rows.Add(textBox1.Text, textBox2.Text, textBox3.Text);
}
edit: So try to add a new item to your list from the button:
private void AddFromKeyboard_Click(object sender, EventArgs e)
{
listProfessors.Add(new Professor(Convert.ToInt32(textBox1.Text), textBox2.Text, Convert.ToDouble(textBox3.Text)));
}
Before anything you should provide us a little bit of your code where you are doing some stuffs, but without it also I will try to explain it to you,
first of all what you might do is next: check is your datagrid's source some list, so you might create an corresponding object from a your textbox and you might simply add it to your list which is source to datagrid and simply refresh the source like:
dataGrid.ItemsSource=null;
dataGrid.ItemsSource = myCustomList;
Or you might do something like this:
private void btnAdd_Click(object sender, EventArgs e)
{
string firstColum = firstTextBox.Text;
string secondColum = secondTextBox.Text;
string[] row = { firstColum, secondColum };
yourDatagrid.Rows.Add(row);
}
But I'm saying again for fully correct answer you should provide / post your code.
Whatever I hope that this will help you
The title was properly written wrong but i will explain currently i have 2 sets of three buttons linked to a label. when i press a button it will place a number in the label as a result what i want to do is after the first button is pressed and then a second is clicked i want the two scores to add together to make a total result in the label eg if i press "three of a kind" which equals 3 points then press "four of a kind" which equals 6 points i want a result of 9 in the label and then if i press "five of a kind" which equals 12 point the label would then read 18 ect can i get any help please there isnt any code to put in due to not finding anything i will put in how my buttons are linked to my labels.
Result
public class Result
{
}
private void button2_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(3)).ToString();
label6.Text = Convert.ToString(add);
}
private void button5_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(3)).ToString();
label7.Text = Convert.ToString(add);
}
private void button3_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(5)).ToString();
label6.Text = Convert.ToString(add);
}
private void button6_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(5)).ToString();
label7.Text = Convert.ToString(add);
}
private void button4_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(12)).ToString();
label6.Text = Convert.ToString(add);
}
private void button7_Click(object sender, EventArgs e)
{
String add = (Convert.ToInt32(12)).ToString();
label7.Text = Convert.ToString(add);
}
I didn't quite understand the question, but I would suggest you to read about WPF and the MVVM mechanism to sync the data between the UI and the data.
What you're doing is writing a lot of code-behind code that is duplicated over and over and is doing a lot of unnecessary work (you convert a number to Int, then convert the result to string and then convert it again to string)
This function can be used to do the work property and efficiently once:
private void UpdateLabelText(Label label, int number) {
label.Text = number.ToString();
}
And use this method whenever you need to update the text in your label.
You have to cast the value of label.Text to int then add the value and then recast to string:
private void button6_Click(object sender, EventArgs e)
{
AddToLabel(label7, 12);
}
void AddToLabel(Label label, int value)
{
var n = int.Parse(label.Text); // convert the actual value of label.Text to int
var add = n + value; // add the increment
label.Text = add.ToString(); // assign to label.Text
}
I am very new and extremely confused as to how I can accomplish this project. The project requests us to create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. This is where I am starting to have problems; I don't need to have what the user selects displayed in a message but the message reflecting what the font size and type were that the user selected. After the user clicks a button, display "Hello" in the selected font and size. I need help in getting the button to display the message in the desired font in a C# Windows Visual Studio 2010 form. I have just a basic code written to start me off which includes the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Since this is the beginning of this project, the font name and size that the user selects will eventually produce a message in that chosen font name and size. Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
You can use the ListBox.SelectedIndex property to set the initial selection of the listboxes. For example, you can add the following lines of code to explicitly select the first items in the listboxes after you add the event handlers:
this.listBox1.SelectedIndexChanged += new System.EventHandler (this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler (this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
By default, the SelectedIndex property of a ListBox is -1, which means there is no selection.
To answer your second question, to display 'Hello' in the selected font and size, I will assume that we can simply change the font of the textBox1 control.
First, make sure that textBox1 has some text; put this statement into the Form1 constructor after the call to InitializeComponent:
textBox1.Text = "Hello!";
Then, modify the event handlers to change the typeface and size of the font:
private void UpdateFont()
{
if (listBox1.SelectedIndex == -1 || listBox2.SelectedIndex == -1)
return; // selection not complete yet, so do nothing
string typeface = listBox1.SelectedItem.ToString();
float size = Convert.ToSingle(listBox2.SelectedItem.ToString());
textBox1.Font = new Font(typeface, size);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateFont();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateFont();
}
to put the default value of your listboxes:
listBox1.SelectedItem = "Arial";
listBox2.SelectedItem = "8";
or a better "dynamic solution":
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
the following code will make the text font and size change, depending on what the user has selected in the listbox.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
edit:
you are getting an error because there's probably no text in your textbox.
TextBox1.Text ="this is some text";
add this to your form.