The user clicks on a text box. After this he selected an item from a list box and the program put the item name in the text box.
The program must uptate the text of the text box only if the user do this path: Cliks on Text Box -> Selected Item from ListBox
If the user does this: Clicks on Text Box -> Does Something Else -> Selected Item from ListBox the program must not uptade the text of the text box.
How can I do it?
private void TextBox_MouseLeave(object sender, EventArgs e)
{
mouse_leave = false;
}
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
if(mouse_leave)
{
//Do something..
}
}
You can access the selected item value as shown in the code below:
public partial class Form1 : Form
{
private bool isUpdated = false;
public Form1()
{
InitializeComponent();
}
private void listBox1_SelectedIndexChanged(Object sender, EventArgs e)
{
if (this.isUpdated)
{
this.textBox1.Text = ((ListBox)sender).SelectedItem.ToString();
this.isUpdated = false;
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
this.isUpdated = true;
}
}
If I understood you correctly , you should do this .
define Public variable .
var selectedTextBox;
when user click on TextBox ,you should save this TextBox name into variable in Step 1.
private void TextBox_MouseLeave(object sender, EventArgs e)
{
selectedTextBox=TextBox.Name ; // Or This.Name;
}
then, when user click on item in listBox, you should take selected value and show in Selected TextBox in Step 2 .
private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
selectedTextBox.Text = ListBox.SelectedItem.ToString();
}
Related
Hy
I have developed a calculator in C# (window applications). I have two textboxes on the form to get inputs from the user and combobox to select the operation to on the input numbers.
And my problem is that when I pressed the 1 button the code is passing 1 to both textboxes. I want to solve this problem like when i click on the textbox1 and then i press the button 1 the code will pass value to only textbox1. And when i click the textbox2 and then by pressing the button 1 the code will have to pass value only to textbox2.
Thanks in advance for answering.
Simply add a Bool to determine which textbox the currently selected focus is on.
Add focus event to textbox:
private void textBox1_Enter(object sender, EventArgs e)
{
Flag = true;
}
private void textBox2_Enter(object sender, EventArgs e)
{
Flag = false;
}
Code:
using System;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static bool Flag = true;
private void button1_Click(object sender, EventArgs e)
{
if (Flag)
{
textBox1.Text += "1";
}
else
{
textBox2.Text += "1";
}
}
private void textBox1_Enter(object sender, EventArgs e)
{
Flag = true;
}
private void textBox2_Enter(object sender, EventArgs e)
{
Flag = false;
}
}
}
Output:
I have a project that uses various click events and looks like this
namespace Example
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btn_obj_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text1");
}
private void btn_catg_Click(object sender, EventArgs e)
{
MyMethods.Method_1("text2");
}
private void btn_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text1");
}
private void btn_top_up_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text2");
}
private void btn_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text3");
}
private void btn_top_down_Click(object sender, EventArgs e)
{
MyMethods.Method_2("text4");
}
public static class MyMethods
{
public static void Method_1(string text) {...}
public static void Method_2(string text) {...}
}
}
}
As you can see I have a quite a number of click events so i'm curious if I can group them all in another c# file or a class or something
In your code-behind, declare a common method you want to call when any of the above buttons fire the Click event.
private void CommonClick(object sender, EventArgs e)
{
}
Now in your Properties window for each button, you can assign this event handler for all buttons:
Now when any of the buttons are clicked this same event handler is called.
If you want to know which button is clicked, you can either use button Name or even the Tag property.
Let's say we assign a separate unique Tag for each button. Tag is a property you can see in the property window for each button (and most controls).
Then you can use a switch-case statement in your code to identify which button was clicked.
private void CommonClick(object sender, EventArgs e)
{
switch (((Button)sender).Tag)
{
case "B1":
break;
case "B2":
break;
}
}
Above, B1, B2 etc are the tags I've assigned to each button.
usually in the form designer you dblclick on the empty "click" event property to generate new method as btn_..._Click(object sender, EventArgs e).
instead you can select existed method, so multiple buttons can call the same method:
Then in the called Method you can check which control trigger this event:
private void button1_Click(object sender, EventArgs e)
{
if (sender == button2)
{
// ....
}
if (sender == button1)
{
// ....
}
}
I would like to have my text deleted only once in my Textbox so that it wouldn't clear Textbox every time I click. My current code looks like:
private void textBox1_Click(object sender, EventArgs e)
{
textBox1.Text = string.Empty;
}
But how can I get it to only delete the text once?
You could use a simple boolean flag:
public partial class Form1 : Form
{
bool firstClick = true;
And in your event handler:
private void textBox1_Click(object sender, EventArgs e)
{
if (firstClick)
{
textBox1.Text = string.Empty;
firstClick = false;
}
}
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();
}
How to display the dropdown list of a comboBox when I click/enter it?
private void comboBox1_Click(object sender, EventArgs e)
{
}
or
private void comboBox1_Enter(object sender, EventArgs e)
{
}
Since you have not mentioned if this is a Web/Windows/WPF so I am suggesting to do this
Winforms
((ComboBox)sender).DroppedDown = true;
WPF
((ComboBox)sender).IsDropDownOpen = true;