Please be patient with me, I am new to C# and I am trying my best
so my question is, when we create a button in windows form app (wfa), there is a private event automatically created if you double click the button.
let's say we have 2 buttons (btn1 & btn2)
btn1 click event has variable of type string called access and I need to call this variable in btn2 click event
so how we will pass the info from a private event to another !
enter image description here
private void button1_Click(object sender, EventArgs e)
{
string access1 = "access 1";
}
private void button2_Click(object sender, EventArgs e)
{
string access2 = access1;
}
You can make the info a field instead of a local variable:
string access1 = "access 1";
private void button1_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
string access2 = access1;
}
Or use a property: What is the difference between a field and a property?
Related
I am creating a library application with a listbox containing a list of books on the main form. I have created an edit form for the books. I want to be able to change the contents of the selected item in the listbox by changing the text in the textboxes of the edit form. Any suggestions how I could do this?
Main form:
private void lstBooks_SelectedIndexChanged(object sender, EventArgs e)
{
string currentBook = lstBooks.SelectedItem.ToString();
}
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
}
Edit form:
private void frmEditBook_Load(object sender, EventArgs e)
{
txtName.Text = listBoxBooks.SelectedItem.ToString();
}
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text.Replace);
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
In your edit form, have a public property for your text
public string NewText
{
get
{
return txtName.Text;
}
}
And then when changing the item in the listbox, simply use the following procedure.
private void btnEdit_Click(object sender, EventArgs e)
{
//Your code from above
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.Show();
frmkeepBookstore.Hide();
//My line
lstBooks.Items[lstBooks.SelectedIndex] = tempEditBook.NewText;
}
Well, it could be very simple if you call the new form with ShowDialog() instead of Show(). In your frmEditBook you should define the property
public string Txt {get; set;}
and set it at btnSave_Click
private void btnSave_Click(object sender, EventArgs e)
{
listBoxBooks.Items.Add(txtName.Text);
Txt = txtName.Text;
frmBookstore.frmkeepBookstore.Show();
this.Close();
}
and use the variable after the edit form is closed:
private void btnEdit_Click(object sender, EventArgs e)
{
lstBooks_SelectedIndexChanged(null, null);
frmEditBook tempEditBook = new frmEditBook(lstBooks);
tempEditBook.ShowDialog();
//tempEditBook.Txt here is your text
lstBooks.SelectedItem = tempEditBook.Txt;
frmkeepBookstore.Hide();
}
So in this listbox I have the names in this order
Abigal
Hannah
Tyler
Bill
Sasquach
The name of the listbox is called namesListBox.
Using TextBoxes and a different button for each input the user needs to input to:
add a name
delete a name
and then search a name to where only that name shows up in the list box at that time.
Once that is complete use a button to count how many items are in the list. Use a different Button to Sort.
This is c#. I can't seem to get nameslistbox.sort or .count to work so any help with that would be great and I have no idea how to do the add, delete, and search.
private void showListButton_Click(object sender, EventArgs e)
{
nameListBox.Visible = true;
}
private void countListButton_Click(object sender, EventArgs e)
{
}
private void addButton_Click(object sender, EventArgs e)
{
List<string> nameListBox = new List<string>();
nameListBox.Add(addTextBox.Text);
}
private void sortListButton_Click(object sender, EventArgs e)
{
}
private void searchButton_Click(object sender, EventArgs e)
{
}
private void deleteButton_Click(object sender, EventArgs e)
{
}
As what icedragon stated in his comment, you are creating a new list every time you click the add button so you need to make it a global variable.
You might want to read more about Linq to handle your collection. http://www.codeproject.com/Articles/19154/Understanding-LINQ-C
private List<string> namesList;
public class YourClass()
{
namesList = new List<string>();
}
private void addButton_Click(object sender, EventArgs e)
{
nameListBox.Add(addTextBox.Text);
}
private void sortListButton_Click(object sender, EventArgs e)
{
nameListBox.Sort();
}
private void searchButton_Click(object sender, EventArgs e)
{
string searchedString = nameListBox.FirstOrDefault(x => x.Contains(searchTextbox.Text);
}
private void deleteButton_Click(object sender, EventArgs e)
{
nameListBox.Remove(removeTextbox.Text);
}
You are creating the List instance in the click event method. This means, that you are creating a new List every time, when you click on the add button and the List is just inside your click method. You should make the List global:
private List<string> namesListBox; // this should be outside of the method
and initialize it in the constructor:
namesListBox = new List<string>();
now you can add elements like this:
private void addButton_Click(object sender, EventArgs e)
{
nameListBox.Add(addTextBox.Text);
}
I have a form with 6 buttons. These buttons serve to increase/decrease tha value of the respective textbox. Now I'm trying to "animate" the buttons. I want to get another effect on the button when the mouse is over him.
To do that, I have two diferent images in Resources and I am doing this code:
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.DownHover;
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
btnHoursDown.Image = Game_Helper.Properties.Resources.Down;
}
This works fine. My question is: it wouldn't be wise to create a class (ButtonBehaviour.cs) and put this code in that class?
So I would have something like this:
ButtonBehaviour buttonBehaviour = new ButtonBehaviour();
private void btnHoursDown_MouseHover(object sender, EventArgs e) {
buttonBehaviour.buttonDownHover();
}
private void btnHoursDown_MouseLeave(object sender, EventArgs e) {
buttonBehaviour.buttonDownLeave();
}
And the class should be:
public class ButtonBehaviour {
public void buttonDownHover() {
// code
}
public void buttonDownLeave() {
// code
}
}
How can I create this Class Behaviour and make the buttons adapt this Behaviour?
if one effect should be applied for all buttons, try to add the same event handlers to them
private void btn_MouseHover(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.DownHover;
}
private void btn_MouseLeave(object sender, EventArgs e)
{
(sender as Button).Image = Game_Helper.Properties.Resources.Down;
}
button which raised event is available via sender variable
this way you avoid code duplication for every button. creating a ButtonBehaviour or CustomButton is probably an over-engineering unless you need them in many forms
I have a event method
private void button1_Click_1(object sender, EventArgs e)
with some code in it. There I want to use a variable from another event method called
public void textBox2_TextChanged(object sender, EventArgs e)
so if a user writes a number it gets saved in a variable in the second method and i want to use this variable in the first method where a button gets clicked. How do I pass it?
Save it as a field of your form class. You can then write it in one event handler and read it in another:
public partial class MyForm
{
private string _someValue = null;
public void TextBox2_TextChanged(object sender, EventArgs e)
{
_someValue = "Some New Value";
}
public void Button1_Click(object sender, EventArgs e)
{
if (_someValue != null)
{
// ...
}
}
}
Though perhaps you can just read TextBox2.Text in the second method.
i try to save user settings in c#.
I can read the value and put it in a textbox but i can not change it.
This is my WindowsForm with an textbox and a save button:
namespace tool
{
public partial class Form4 : Form
{
string source = Properties.Settings.Default.Source;
public Form4()
{
InitializeComponent();
}
private void Form4_Load(object sender, EventArgs e)
{
textBox1.Text = source;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = source;
Properties.Settings.Default.Save();
Application.Exit();
}
}
}
And here is a picture with my settings:
I hope someone as an idea :-)
Thanks for help
Try this:
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Source = textBox1.Text;
Properties.Settings.Default.Save();
Application.Exit();
}
You need to use what is currently in the text box, not your member variable.
Or you can change this event to be as follows (and then you don't have to modify the above):
private void textBox1_TextChanged(object sender, EventArgs e)
{
source = textBox1.Text;
}
could you possibly have a read lock being applied to the key you're looking at while testing this? Maybe the code's not the problem?