Using c# in 2008 Express. I have a textbox containing a path. I append a "\" at the end on Leave Event. If the user presses 'Escape' key I want the old contents to be restored. When I type over all the text and press 'Escape' I hear a thump and the old text isn't restored. Here what I have so far ...
public string _path;
public string _oldPath;
this.txtPath.KeyPress += new System.Windows.Forms.KeyPressEventHandler(txtPath_CheckKeys);
this.txtPath.Enter +=new EventHandler(txtPath_Enter);
this.txtPath.LostFocus += new EventHandler(txtPath_LostFocus);
public void txtPath_CheckKeys(object sender, KeyPressEventArgs kpe)
{ if (kpe.KeyChar == (char)27)
{
_path = _oldPath;
}
}
public void txtPath_Enter(object sender, EventArgs e)
{
//AppendSlash(sender, e);
_oldPath = _path;
}
void txtPath_LostFocus(object sender, EventArgs e)
{
//throw new NotImplementedException();
AppendSlash(sender, e);
}
public void AppendSlash(object sender, EventArgs e)
{
//add a slash to the end of the txtPath string on ANY change except a restore
this.txtPath.Text += #"\";
}
Thanks in advance,
Your txtPath_CheckKeys function assigns the path to the old path, but never actually updates the Text in the TextBox. I suggest changing it to this:
public void txtPath_CheckKeys(object sender, KeyPressEventArgs kpe)
{
if (kpe.KeyCode == Keys.Escape)
{
_path = _oldPath;
this.txtPath.Text = _path;
}
}
The Control.Validating event might help you.
It describes the order in which events are triggered. So, choosing the event that best suits your need will make it easier to implement this feature.
It might be too much for the need, but trying to Invalidate the control might help too.
Let me know whether it helps.
Related
i have a windows forms app which i want a certain button to change to enable if the textbox is not empty i tried to compare it to string.Empty but it wont work so i decided to compare to TextLength wont work either ..
Code down below:
private void Form1_Activated(object sender, EventArgs e)
{
if (firstDisplayTxtBox.TextLength > 0)
{
plusButton.Enabled = true;
}
}
mayble i place the if statement in the wrong method Let me know where i am wrong Big Thanks for helpers
Probably, you want to use TextChanged event, which fires every time when text in your firstDisplayTxtBox is changed.
public Form1()
{
InitializeComponent();
firstDisplayTxtBox.TextChanged += OnTextChange;
}
private void OnTextChange(object sender, EventArgs e)
{
plusButton.Enabled = firstDisplayTxtBox.Text.Length > 0
}
or
I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.
I want to check if the Clipboard consists of a data and if not, let the "Paste" Button be enabled. But unfortunately, even after I clear the Clipboard it still doesn't show it's null. I am working with Windows Forms.
I manually clear the clipboard:
private void button2_Click(object sender, EventArgs e)
{
Clipboard.Clear();
}
and then I add the following code to the Form LoadEvent:
if (Clipboard.GetDataObject() != null)
{
this.pn1_BtnPaste.Enabled = true;
}
And it makes a button enabled which is weird to me. Can anybody explain why is that happening?
EDIT:
Because I got understood wrong, let me change the code to make it more clear:
private void button2_Click(object sender, EventArgs e)
{
Clipboard.Clear();
if (Clipboard.GetDataObject() != null)
{
this.pn1_BtnPaste.Enabled = true;
}
else
this.pn1_BtnPaste.Enabled = false;
}
I click the "button2" and the "pn1_BtnPaste" is enabled anyway.
Data can appear on the clipboard at any time. The Application.Idle event is a decent way to update the button state:
public Form1() {
InitializeComponent();
Application.Idle += Application_Idle;
}
You have to unsubscribe it again when the window closes to be on the safe side:
protected override void OnFormClosed(FormClosedEventArgs e) {
Application.Idle -= Application_Idle;
base.OnFormClosed(e);
}
Clipboard.GetDataObject() does not work the way you think it does, it never returns null. If you want to handle any data then you can write the event handler like this:
private void Application_Idle(object sender, EventArgs e) {
PasteButton.Enabled = Clipboard.GetDataObject().GetFormats().Length > 0;
}
But it is pretty likely you'll find out that handling every possible format is lot let practical than you assumed.
I'm not sure if I'm using the wrong terminology so that may be why my searching has not turned up anything.
I have a bunch of text boxes that I want to validate and check that they don’t contain apostrophes. The code that I have is:
public void apostropheCheck(TextBox fieldName)
{
Match m = Regex.Match(fieldName.Text, #"'");
if (m.Success)
{
validationErrorProvider.SetError(fieldName, "Field can not contain apostrophes");
}
else if (!m.Success)
{
validationErrorProvider.SetError(fieldName, "");
}
}
and the validation on the textbox is:
private void FirstNameTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
//Checks for apostrophes
apostropheCheck(FirstNameTextBox);
}
However when I run this the value that gets passed to the void is the text that is in the text box (e.g ‘John’ or ‘Mary’) I could get this to work just using the code that’s in the void for each validation event but that would be repeating myself a lot. Is there a better way?
You can have one common handler and use that for all of the textboxes' Validating event.
private void CommonTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
//Checks for apostrophes
apostropheCheck((TextBox)sender);
}
The sender object of the event is a TextBox, so you can cast it to a text box and repeat the same event handler for all of the text boxes in your application
private void FirstNameTextBox_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
apostropheCheck(((TextBox)sender).Text);
}
Terminology: you are referring to your function apostropheCheck as the void which is its return type the standard way is to use the function name apostropheCheck.
All of you text boxes can be validated using the same function if you replace the name of the text box in the code with sender ex.
private void FirstNameTextBox_Validating(object sender,System.ComponentModel.CancelEventArgs e)
{
//Checks for apostrophes
apostropheCheck((TextBox)sender);
}
now i have the current code o MainUC.cs:
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
The thing i want to do is clean the code from main page/form. I have 2 taskbar and 2 toolbar buttons that are calling the same form (this one above), so i don't want to write the code 4 times. I tried to make new class.cs with properties and do it with return value, but it didn't work. Can someone help me with it, or, is there possiblity to call the same code on current page/form. Something like
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
call tsbNoviRacun();
}
"( this isn't working, i know :p)
EDiT: Oh damn me, thanks guys!
In c# there is no "call" keyword for invoking functions. You just type the name and all required arguments in round brackets.
private void tsbStariRacuni_Click(object sender, EventArgs e)
{
tsbNoviRacun_Click(sender, e);
}
This should do it:
public void tsbNoviRacun()
{
if (racunuc == null)
{
racunuc = new RacunUC();
racunuc.Dock = DockStyle.Fill;
Controls.Add(racunuc);
}
racunuc.BringToFront();
}
private void tsbNoviRacun_Click(object sender, EventArgs e)
{
tsbNoviRacun();
}
You can call that method from all the event handlers you want it to run on. Obviously this function is depended on Controls and DockStyle so you must put it within scope of this.