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;
}
}
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:
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();
}
I'm using two buttons for "on" and "off". When I press "On" button, it passes a string value "1", and pressing the "Off" button passes the string value "2".
How can I do this within one button? By default button should be off, when I press it should on and when i press again it should off.
public partial class _Default : System.Web.UI.Page
{
SerialPort ardo;
protected void Page_Load(object sender, EventArgs e)
{
ardo = new SerialPort();
ardo.PortName = "COM5";
ardo.BaudRate = 9600;
}
protected void Button1_Click(object sender, EventArgs e)
{
string stop = "1";
ardo.Open();
ardo.Write(stop);
ardo.Close();
}
protected void Button2_Click(object sender, EventArgs e)
{
string On = "2";
ardo.Open();
ardo.Write(On);
ardo.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
//consider initially its stopped
if(Session[“currentState”] == null)
Session[“currentState”] = “1”;
if(Session[“currentState”].ToString() == “1”)
{
Session[“currentState”]= “2”;
}
else
{
Session[“currentState”]= “1”;
}
ardo.Open();
ardo.Write(Session[“currentState”].ToString());
ardo.Close();
}
it set to hidden text box.
then you can check if statement it is pressed or not
protected void Button1_Click(object sender, EventArgs e)
{
// hf means hidden textbox
if(hf.text==1){
// your on code
hf.text=0;
}
if(hf.text==0){
//your off code
hf.text=1;
}
}
Declare a string variable
private string isOn = "2";
and then on the button click event you can handle it like
protected void Button1_Click(object sender, EventArgs e)
{
if(isOn.Equals("2"))
isOn = "1";
else
isOn = "2";
}
I'm trying to implement a customized exit prompt in my WinForms. (I should not be using DialogBox)
I have a User Control Object placed in my main form that is invisible and disabled by default. Clicking in a certain button I have placed on the form shows and enables the object, disabling everything in my form except the User Control.
private void btn_close_Click(object sender, EventArgs e) {
prompt1.Visible = true;
prompt1.Enabled = true;
disableControls();
//Wait for a button to be pressed in prompt1
//Make an action based on a button pressed.
//closeApp returns a boolean
if (!prompt1.closeApp)
{
prompt1.Visible = false;
prompt1.Enabled = false;
enableControls();
}
else
{
Application.Exit();
}
}
Here's my code at the prompt object:
public partial class Prompt : UserControl
{
bool exit;
public bool closeApp
{
get{return exit;}
}
public Prompt()
{
InitializeComponent();
}
private void btn_yes_Click(object sender, EventArgs e)
{
exit = true;
}
private void btn_no_Click(object sender, EventArgs e)
{
exit = false;
this.Hide();
}
}
What I want to do is wait for a button to be pressed in my prompt object before proceeding to the next line in the btn_close_Click().
What should I do? Is there a better way to implement this?
Add events to your usercontrol then handle those events on your main form.
In your usercontrol:
public event EventHandler<EventArgs> ExitCancelled;
public event EventHandler<EventArgs> ExitApplication;
private void btn_yes_Click(object sender, EventArgs e)
{
ExitApplication?.Invoke(this, EventArgs.Empty);
}
private void btn_no_Click(object sender, EventArgs e)
{
ExitCancelled?.Invoke(this, EventArgs.Empty);
}
Handle the events on your form:
public void prompt1_ExitApplication(object sender, EventArgs e)
{
Application.Exit();
}
public void prompt1_ExitCancelled(object sender, EventArgs e)
{
prompt1.Hide();
enablecontrols();
}
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();
}