private void delete_Click(object sender, EventArgs e)
{
convertedText.Text = "";
}
private void copy_Click(object sender, EventArgs e)
{
if (convertedText.Text != "")
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
else... what to put here?
}
The program has two buttons (copy and delete) and one textbox. If I click the 'Copy' button, it copies the text from convertedText.Text without any problem. The 'delete' button also clears the textbox fine.
But if there's nothing in the textbox, the 'copy' button still attempts to copy it which is causing an unexpected behaviour.
So, what code do I add to the 'else' statement...? What I want is that if the textbox has nothing in it, then the clipboard operation won't be used. How to do that?
Thanks in advance!
Don't add an else clause, just have the if by itself, e.g.
private void copy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(convertedText.Text))
{
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
}
Also, is there any reason why you're copying the text box text to the clipboard and then using the clipboard text to update the text box text? Unless I'm missing something, this should have no effect on the text box, so the code can be simpler:
private void copy_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(convertedText.Text))
Clipboard.SetText(convertedText.Text);
}
Your error stems from the fact that you are missing some parentheses there:
if (convertedText.Text != "")
{
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
Only the first line after an if statement is considered to be part of what gets executed dependent on the evaluation of the if when you are omitting parentheses.
You could also return if the textbox does not have a value...
private void copy_Click(object sender, EventArgs e)
{
if (convertedText.Text.Equals(""))
return;
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
maybe you are missing brackets { and }
if (convertedText.Text != ""){
Clipboard.SetText(convertedText.Text);
convertedText.Text = Clipboard.GetText();
}
else
Try putting
try
{
string foo = "bar" + 42;
}
catch
{
throw;
}
Related
After spending 90 minutes searching for a solution to this simple problem I have to post a question in shame.
I'm working on a WPF project where the user inputs text. I want to check the inputs while the user is typing, display a tool tip and ideally block characters that are not allowed. Basically it's this thread:
How do I validate characters a user types into a WinForms textbox? or this
Is there a best practice way to validate user input?
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
e.???
}
I created this code behind by double clicking in the KeyDown-Property Field in the designer (just mentioning this if I messed up there).
Screenshot of the Property Window
I can not access the e.SupressKeyPress Property. Why?
As of the Properties offered by VS I think that e is of the wrong Type or in the wrong context here.
Intellisense Screenshot
Edit1
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
var strKey = new KeyConverter().ConvertToString(e.Key);
if (!strKey.All(Char.IsLetter))
{
MessageBox.Show("Wrong input");
e.Handled = true;
}
}
Thanks to #rokkerboci I was able to build something that kind of works.
Yet I think it is overly complex. So improvements are still welcome :)
New Error When Creating a Message Box the application hangs without an exception thrown.
You are using WPF, which does not include the WindowsForms specific SupressKeyPress property.
You can do this in WPF by using the KeyDown event, and setting the KeyEventArgs.Handled property to true (it tells the handler, that it doesn't have to do anything with this event.)
private void NameTextbox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Delete)
{
MessageBox.Show("delete pressed");
e.Handled = true;
}
}
EDIT:
I have found a perfect answer to your question:
C#:
char[] invalid = new char[] { 'a', 'b' };
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
foreach (var item in invalid)
{
if (e.Text.Contains(item))
{
e.Handled = true;
return;
}
}
}
private void TextBox_Pasting(object sender, DataObjectPastingEventArgs e)
{
var text = e.DataObject.GetData(typeof(string)).ToString();
foreach (var item in invalid)
{
if (text.Contains(item))
{
e.CancelCommand();
return;
}
}
}
XAML:
<TextBox PreviewTextInput="TextBox_PreviewTextInput" DataObject.Pasting="TextBox_Pasting" />
I want to create a login page based C# desktop application. I use the bunifu toolbox to create a login page design. But when I want to create a password field using the bunifumaterialtextbox, the textbox does not show any changes / it only displays alphabet. It looks like the ispassword contained in the textbox properties is not working. So what should I do so that this texbox can display the correct password (not displaying alphabeth) when the program is run ?. I apologize for any errors in this question.
is just a easy solution i have found
private void passbox_OnValueChanged(object sender, EventArgs e)
{
passbox.isPassword = true;
}
I'm new using the Bunifu framework tool and I had the same problem then you. The solution I found was to invoke the _TextBox method which I suppose that gives you all the normal TextBox controls.
My code was something like this:
txtPassword._TextBox.PasswordChar = '*';
I loaded this code inside the Form_Load code block. It worked for me, hope that be useful for you too. Good luck!
in side the properties tab of the bunifu text box there is a property ispassword set it to true.
to fully make the bunifu textbox to passwordbox you have to do the following:
1.in side the properties tab of the bunifu text box there is a property ispassword set it to False.
2.create an event of Enter and use the following code:
3.create an event of Leave and use the following code:
private void txtpassword_Enter(object sender, EventArgs e)
{
if (txtpassword.Text == "Password")
{
txtpassword.Text = "";
txtpassword.isPassword = true;
}
}
private void txtpassword_Leave(object sender, EventArgs e)
{
if (txtpassword.Text == "")
{
txtpassword.Text = "Password";
txtpassword.isPassword = false;
}
}
like this
You can use the UseSystemPasswordChar property in your text box.
Example:
TextBox1.UseSystemPasswordChar = true;
this problem's solution is using enter,leave and textchange event such as below code if the name of your text box is txtPassword:
private void txtPassword_TextChange(object sender, System.EventArgs e)
{
if (txtPassword.Text.Trim() != "")
{
txtPassword.PasswordChar = '*';
}
else
{
txtPassword.PasswordChar = '\0';
}
}
private void txtPassword_Leave(object sender, System.EventArgs e)
{
if (txtPassword.Text.Trim() == "")
{
txtPassword.PasswordChar = '\0';
txtPassword.TextPlaceholder = "insert your placeholder..";
}
}
private void txtPassword_Enter(object sender, System.EventArgs e)
{
if (txtUserName.Text.Trim() != "")
{
txtPassword.PasswordChar = '*';
txtPassword.PlaceholderText = "";
}
}
I'm trying to set the "txtMiles" textbox to focus after:
The form opens
When the "clear" button is clicked
I have tried using txtMiles.Focus(); but it doesn't seem to work for me.
CODE BEING USED ON THIS FORM
private void btnConvert_Click(object sender, EventArgs e)
{
//assigns variable in memory.
double txtMile = 0;
double Results;
try
{
// here is where the math happens.
txtMile = double.Parse(txtMiles.Text);
Results = txtMile * CONVERSION;
lblResults.Text = Results.ToString("n2");
txtMiles.Focus();
}
// if the user enters an incorrect value this test will alert them of such.
catch
{
//MessageBox.Show (ex.ToString());
MessageBox.Show("You entered an incorrect value");
txtMiles.Focus();
}
}
private void btnClear_Click(object sender, EventArgs e)
{
//This empties all the fields on the form.
txtMiles.Text = "";
txtMiles.Focus();
lblResults.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
// closes program
this.Close();
}
}
}
Thanks in advance for the help.
You should make sure your TabIndex is set and then instead of Focus(), try use Select(). See this MSDN link.
txtMiles.Select();
Also make sure there isn't a TabStop = true attribute set in the view file.
It's old, but someone could need this.
Control.Focus() is bugged. If it's not working, try workaround:
this.SelectNextControl(_controlname, true, true, true, true);
Change function parameters so they will work with your control, and remember about TabStop = true property of your control.
You already have your txtMiles focused after the clear-button click. As for the Startup, set txtMiles.Focus() in your load-method.
private void MilesToKilometers_Load(object sender, EventArgs e)
{
txtMiles.Focus();
}
using this solution worked perfectly...
txtMiles.Select();
Working in VS 2012, WinForms, C#...
I have a ListBox I would like to populate depending upon the value selected in a ComboBox. I've tested my SQL Query and it works, but I'm getting a weird problem where, when I run my routines, my ComboBox comes up empty, as well as my ListBox. When I comment out the code in my cb_Session_SelectedValueChanged routine, my CB and LB load just fine, but when it's not commented out is when my LB and CB end up blank.
This is what I have:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
private void LoadSessionListbox()
{
int tempID = Convert.ToInt32(cb_Session.SelectedValue);
// Code here to load listbox, which works without above routine.
}
Am I missing something? Why are my CB and LB blank with that first routine added?
[EDIT]:
I put the routines which were in SelectedValueChanged in a MouseClick event and it works, but not when I want it to... You have to click a couple times to get it to re-load with the correct ID. I feel like I'm getting closer, but still not the right event.
Try this:
private void cb_Session_SelectedValueChanged(object sender, EventArgs e)
{
if(cb_Session.SelectedValue>-1)
{
listbox_Sessions.Visible = true;
LoadSessionListbox();
}
}
Figured it out!!
I ended up adding a simple if statement to my SelectedValueChanged routine, and it fixed everything!
private void cb_Sessions_SelectedValueChanged(object sender, EventArgs e)
{
listBox_Sessions.Visible = true;
if (cb_Sessions.SelectedValue != null)
LoadSessionListbox();
}
Works perfectly now.
Try in SelectedIndexChanged Event and follow
private void cb_Session_SelectedIndexChanged(object sender, EventArgs e)
{
if (cb_Session.SelectedValue == null) return;
if (cb_Session.SelectedIndex == -1) return;
listbox_Sessions.Visible = true;
LoadSessionListbox((int)cb_Session.SelectedValue);
}
private void LoadSessionListbox(int selectedValue)
{
//TODO: Do stuff
}
i need to based in what the user wrote in the edition of a node label, rewrite that label with other text. Example if the user wrote "NewNodeName" I want that the node text after finish the edition be "S :NewNodeName".
I try this two codes and i don't know why neither work
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
e.Node.Text = "S :"+ e.Label;
}
and also:
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
treeView1.SelectedNode.Text = "S :"+ e.Label;
}
Yes, doesn't work, the Text property gets the label value after this event runs. Which is why e.Cancel works. So the Text value you assigned will be overwritten again by code that runs after raising this event. Code inside of the native Windows control.
There is no AfterAfterLabelEdit event and you cannot alter e.Label in the event handler, you need a trick. Change the Text property after the event stopped running. Elegantly done by using Control.BeginInvoke(). Like this:
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e) {
this.BeginInvoke((MethodInvoker)delegate { e.Node.Text = "S: " + e.Node.Text; });
}
It's quite late to answer this question, but here's another solution:
1) Remove the part that you want user not to edit of the node label right before you call BeginEdit()
2) In AfterLabelEdit(), set the node text as you want and set NodeLabelEditEventArgs.CancelEdit = true so that the text user input won't replace the text you set
private void treeView1_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node == null) return;
e.Node.Text = e.Node.Text.Substring(3, e.Node.Text.Length - 3);
e.Node.BeginEdit();
}
private void treeView1_AfterLabelEdit(object sender, NodeLabelEditEventArgs e)
{
e.Node.Text = "S :" + e.Label;
e.CancelEdit = true;
}