Problem with button when text entered into the textbox - c#

I am trying to make the save button visible when text is entered into the text box by using the following code:
if (tbName.TextModified == true)
{
btnCTimetablesOk.Visible = true;
}
else
{
btnCTimetablesOk.Visible = false;
}
but it gives error at tbname.textmodified
is there any other way to visible the button when we enter the text in text box
this is error i am getting "the event textbox.textmodified can only appear on the left hand side of += or -="

Try using the textbox's Enter and Leave events to show/hide your button:
private void textBox1_Enter(object sender, System.EventArgs e)
{
btnCTimetablesOk.Visible = true;
}
private void textBox1_Leave(object sender, System.EventArgs e)
{
btnCTimetablesOk.Visible = false;
}
Then modify your textbox to use these new methods.

If I'm reading your text correctly, you want the save button to be visible when the textbox has text in it and invisible when the text box is blank. If that's the case, you can use the Leave event (which occurs when the textbox loses focus) and a simple if statement:
private void textBox1_Leave(object sender, System.EventArgs e)
{
if(textBox1.Text != "")
btnCTimetablesOk.Visible = true;
else
btnCTimetablesOk.Visible = false;
}
You can also put this conditional block in any other methods kicked off by events that change the text of the box.
Also, you might want to consider using Enabled instead of Visible, it'll leave the button on the form but will gray out the text and clicking will do nothing.

I'm going to take a stab in the dark here and assume that the button is related to the textbox and you probably want someone to be able to type something in the textbox then click the button. You probably don't want the user to have to type something, then tab out or click somewhere else to make the button visible then click the button.
tbName_TextChanged(object sender, EventArgs e)
{
btnCTimetablesOk.Visible = !String.IsNullOrEmpty(tbName.Text)
}
Btw you're getting that error because TextModified isn't a boolean property, it's an event, like TextChanged or Leave or Enter. You can assign an event handler to it but you can't just check it like that.
As an aside I personally hate systems hungarian for winforms controls. I'd much rather have a timetablesOkButton than a btnCTimeablesOK button. That way if you also have a timetablesNameTextBox you can see at a glance that the button and the textbox match. Of course it may not be up to you.

Related

How to disable Istab function within the application?

I am tired of trying to deactivate the button highlight when it's activated with "istab" set to true.
Basically, I want to deactivate the back highlight of a button (if it's activated) when I click on the picture box. The method partially works. The istab deactivation works when I click on the picture box and the highlight goes away. But I made a method so that when the mouse hovers over the button, "istab" automatically reactivates. But it doesn't seem to work. The "istab" remains deactivated.
Here is what I have tried so far.
private void LogoImage_Click(object sender, EventArgs e)
{
if (btnEssentialRules.IsTab == true)
btnEssentialRules.IsTab = false;
}
private void btnEssentialRules_MouseHover(object sender, EventArgs e)
{
if (btnEssentialRules.IsTab == false)
btnEssentialRules.IsTab = true;
}
Where the btnEssentialRules is the button name.
If you have another solution or a fix to my code. please help me.

How to run code when hitting enter in a c# text box

I'm using web forms
Right now my code runs when I leave the text box and the text has changed. I am running into issues. If I change the text but hit a button instead of enter, it resets via code. I need to be able to change the text and click a button which wont yet do anything, or change the text and hit enter which will trigger code.
thanks for the help
This is text changed event for the text box with notations of what im needing to do. really what I think I need is an event for clicking enter, not changing text
protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
if (txtboxPlate.Text == "plate number")
{
//will check database for "plate number" and do stuff on enter.
}
else
{
resetforms();// on enter
}
else
{
the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
}
}
You need to implement a method for the KeyDown event of your textbox.
private void txtboxPlate_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter) {
//Code
}
}
Keydown is a client-side event, as far as I know you're more than likely going to have to use JavaScript/Jquery.
Refer to the following link:
Textbox Keydown Event
I would of used a comment but... rep issues :/
edit:
To anyone that hasn't realised yet, the question changed to webforms not winforms
Alternative:
Use a button.
You could place a button next to the textfield, it's simple and not a lot of work goes into it. You can set the TabIndex of the textbox to 1 and the TabIndex of the button to 2 so when you hit TAB it will focus the textbox and if pressed again it will focus the button. You could also look into adding the button to the textbox for design purposes.
edit: You also need to think about post back, when you hit a button the page get's post back meaning values are lost, to persist the data within the field you would have to use view state, session variables or a hidden field. For simplicity I'd set the value of the text box to a hidden field and then simply re-apply the value on page_load. If the value needs to persist across multiple postbacks/accessed from other pages use session variables.
So as a code example:
Remove the Text_changed event entirely.
protected void btnDoStuff(object sender, EventArgs e)
{
if(txtPlateNumber.Text == "Plate Number")
{
//Do stuff
}
else
{
//Do other stuff
}
}
-
If you notice the value disappearing after the post back do something like:
protected void btnDoStuff(object sender, EventArgs e)
{
if(txtPlateNumber.Text == "Plate Number")
{
//Do stuff
myHiddenField.Value == txtPlateNumber.Text;
}
else
{
//Do other stuff
}
}
then reset the value on page_load.
If this is winform and you still want to use textchanged you can try catching your code first for example:
protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
//Catching code
if (txtboxPlate.Text != "")
{
if (txtboxPlate.Text == "plate number")
{
//will check database for "plate number" and do stuff on enter.
}
else
{
resetforms();// on enter
}
}
else
{
the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
}
cause what textchanged is doing is unless txtboxPlate.Text equels "plate number" it will always do the else statement. Correct me if i'm wrong though but i had the same problem before which almost made me go insane.
Or try above 1 upvote code:
protected void txtboxPlate_TextChanged(object sender, EventArgs e)
{
//Event only happens if you press enter
if (e.KeyCode == Keys.Enter)
{
if (txtboxPlate.Text == "plate number")
{
//will check database for "plate number" and do stuff on enter.
}
else
{
resetforms();// on enter
}
}
else
{
the text has changed by user, but has clicked a button and needs nothing to happen because of this text change
}
}

How to paste text from clipboard into selected textbox using a button

I am relatively new to c# but I am currently creating a Windows Form that has an editor window. I'm struggling with the Paste button though as I have 2 textbox fields, one for the title of the note and one for the note itself. I'm wanting to be able to paste from the clipboard into either textbox.
I have tried using if statements based on noteText.Focused and titleText.Focused but obviously this doesn't work as the Paste button becomes focused as soon as you click it.
Any suggestions would be of great help.
Create a local variable and save last focused textBox in it.
//subscribe both textBoxes with same GotFocus event handler
textBox1.GotFocus += textBox_GotFocus;
textBox2.GotFocus += textBox_GotFocus;
//local variable
TextBox lastSelected;
//GotFocus
private void textBox_GotFocus(object sender, EventArgs e)
{
//save last Selected textBox
lastSelected = sender as TextBox;
}
private void button1_Click_1(object sender, EventArgs e)
{
//on click get value from clipboard
if(lastSelected != null)
lastSelected.Text = Clipboard.GetText();
}

ComboBox show dropdown menu on text selection

I want to show the list of items in a combo box when the user selects the text. I have a touch screen application and it's very difficult to hit the dropdown arrow so I figure I'd show the menu when the text is selected which is often what gets touched. I'm using VS 2008. and suggestions for a touch friendly numeric up down solution in VS2008?
You could use the ComboBox.Click event handler and the ComboBox.DroppedDown property and do something like this:
private void ComboBox1_Click(System.Object sender, System.EventArgs e)
{
ComboBox1.DroppedDown = true;
}
You could also use the same event handler for a numericUpDown and use the mouseposition as well as the position and height of the NumericUpDown to get whether or not the click was above or below the halfway-line of the control by doing something like this (not sure if my math here is perfect, but it worked when I tested it):
if ((MousePosition.Y - this.PointToScreen(NumericUpDown1.Location).Y < NumericUpDown1.Height / 2))
{
NumericUpDown1.Value += 1;
}
else
{
NumericUpDown1.Value -= 1;
}
HTH
I was working on a similar situation. We wanted to make the text area behave the same as the button on the right. (IE the user clicks and gets the drop down box)
davidsbro is similar to what I ended up doing, but we wanted it to close if they clicked again, so the value became dropDown.DroppedDown = !dropDown.DroppedDown;.
The issue with this is that if the user clicks the right button of the drop down box, the dialog box opens, then calls the onClick event.
I solved this situation by tracking the original state via the onmouseover event. If the value has changed, we have to assume that the button on the select box handled the click already.
private bool cbDropDownState = false;
private void dropDown_MouseEnter(object sender, EventArgs e)
{
cbDropDownState = dropDown.DroppedDown;
}
private void dropDown_Click(object sender, EventArgs e)
{
if (dropDown.DroppedDown == cbDropDownState )
dropDown.DroppedDown = !dropDown.DroppedDown;
}

Changing the AcceptButton depending on active control

I have a working WinForm that handles the search functionality in my customer database. It contains the following controls:
A textBox where you type your search string (A)
A "search" button (B)
A DataGridView (C) that returns the result of the search
An "OK" button (D) and a "Cancel" button (E)
What I am trying to accieve next is this:
When A is active the AcceptButton (enter) should be linked to B
When B or enter is pressed C should become active
When C is active the AcceptButton should be linked to D
I realise this is a somewhat big question so don't hesitate to answer just one of the bullet marks if you know the answer.
EDIT: I have solved the implementation of requirement 1 and 3 above, but I am still looking for an answer to the second one. To clarify, when the search is initiated (meaning i have pressed enter on the keyboard or the search button with the mouse) I want focus to shift to the first line in the DataGridView.
When you get the text changed event for textBox set the AcceptButton to be "Search":
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = SearchButton;
}
There will probably want to be more code in here, checking the length of the string etc.
Then once you've done the search and filled in the DataGridView you can then set the AcceptButton to be "OK".
private void dataGridView1_DataMemberChanged(object sender, EventArgs e)
{
this.AcceptButton = OKButton;
}
Though you probably wouldn't want to use this event.
"Cancel" is always the CancelButton.
EDIT: OK for part 2 you want the following code:
private void SearchButton_Click(object sender, EventArgs e)
{
dataGridView1.Focus();
}
This will make it active as soon as the search button is pressed, but you probably want to do it when the DataGridView has been populated - just in case the search returned no results.
Setup handlers to track the Control.Enter event for the particular controls. When the enter is reached try:
this.AcceptButton = ControlThatShouldBeAcceptButton;
You should implement an event listener to DataGridViewBindingCompleteEventHandler. In that event you could add the following code:
{
if(grid.Rows.Count > 0)
{
grid.ClearSelection();
grid.Rows[0].Selected = true;
}
grid.Focus();
}
private void WhateverControl_Enter(...)
{
this.AcceptButton = myButton;
}

Categories

Resources