Programatically change focus for TextBox in C# - c#

I have a class derived from TextBox in C#. I override OnClick method to show a file open dialog. Is it possible to lose focus after that? I don't want the user to be able to edit the text because at a moment the file name might be invalid. I tried to set ReadOnly = true, but one can change the text after selecting the file.
EDIT:
I added the relevant code for this. As it is now the focus will be set to next control from my Form.
class Property : TextBox
class FileSelectTextBox : Property
{
protected override void OnClick(EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
Enabled = false;
if (dialog.ShowDialog(this) == DialogResult.OK)
{
Text = dialog.FileName;
}
Enabled = true;
}
}

You have several options here:
Make the textbox ReadOnly. The textbox will still fire OnClick events but the text won't be editable by the user.
Disable the textbox at the end of your click event -- the disadvantage is that the click event won't fire a second time (which means the user won't be able to change their mind and pick a new file).
Simply set the focus somewhere else at the end of the click event. (someOtherTextBox.Focus())
Edit: Once last suggestion: you may want your file popup to happen in FocusGained rather than OnClick, that way the dialog will still pop up if the user tabs into the control. Of course it's your decision if that behavior is desired or not.
Edit 2: Ignore that last edit. It's a bad suggestion that I didn't think through. (Thanks for the heads up commenter)

set the ReadOnly = true property of the textbox (don't change it at any point of time) and it should work lonely..
and rest of the code goes like this..
protected override void OnClick(EventArgs e)
OpenFileDialog dialog = new OpenFileDialog();
//user can still change/edit some non-existing file/path and click OK, so set the followings
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
if (dialog.ShowDialog(this) == DialogResult.OK)
{
Text= dialog.FileName;
}
}

Related

RadioButton.IsChecked Not Going False

I have a UWP app in C#. I have a Radio Button that the user can turn check or uncheck. It is checked by default and the user can uncheck it when they wish. However when they uncheck it the bool value for RadioButton.IsChecked never returns false making impossible for the user to then recheck the Radio Button if required. My code is below.
private void usingmltBtn_Click(object sender, RoutedEventArgs e)
{
if (RadioButton.IsChecked == true)
{
RadioButton.IsChecked = false;
i2cerrRec.Visibility = Visibility.Collapsed;
i2cerrTxt.Visibility = Visibility.Collapsed;
mltI2Cnck = 0;
}
else if (RadioButton.IsChecked == false)
{
RadioButton.IsChecked = true;
mlttempLbl.Text = "N/C";
}
}
Thanks for your help.
As other's have described, RadioButtons are designed to work in a group. This means that you will have multiple RadioButtons for a user to select from but they can only have one checked.
When a RadioButton is checked, it cannot be unchecked by the user by clicking it again without using a custom behaviour that checks if the user has tapped the RadioButton but this would not be recommended.
I suggest that you use the CheckBox for this particular functionality. You can even re-template a CheckBox control to look like a RadioButton if you wish.
Go through the url below
https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/radio-button
You might wrong in XAML code!
Use this
private void usingmltBtn_Click(object sender, RoutedEventArgs e)
{
if (RadioButton.Checked == true)
{
RadioButton.Checked = false;
i2cerrRec.Visibility = Visibility.Collapsed;
i2cerrTxt.Visibility = Visibility.Collapsed;
mltI2Cnck = 0;
}
else if (RadioButton.Checked == false)
{
RadioButton.Checked = true;
mlttempLbl.Text = "N/C";
}
}
I can see 2 problems in here:
1- I might be wrong but the method you posted is triggered by a control whose name is usingltBtn (Not descriptive by the way) if this is the case you might be running this code with the trigger of another control.
2- Either the name of the RadioButton is usingltBtn or not you are not referencing the code to your control RadioButton is the name of a class not a specific control.
How to fix this? If the name of your control is usingltBtn use usingltBtn.IsChecked instead of RadioButton.IsChecked if not find the name of your control and use it

Disabling editing in DataGridView

I'm using Visual Studio 2012. I want to disable the editing on the DataGridView, it seems to work when I used this code:
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
dataGridView1.ReadOnly = true;
}
But when I get back on the menu form then go back to the form where the DataGridView is, it can now be edit. I only define
dataGridView1.ReadOnly = true;
to this form. And I don't know whats the problem. Can someone help? Thanks.
Here's my code on the button going to the menu
Menu menu = new Menu();
this.Hide();
menu.ShowDialog();
and my button going back to the DataGrid:
FrmList frmlist = new FrmList();
frmlist.Show();
this.Hide();
Why don't you try setting the ReadOnly property to True in the Properties window of the DataGridView?
Edit:
Double click on the form and in the design window, select the DataGridView and open the Properties tab. Scroll down through the properties and you will see the ReadOnly option. Change it's value to True.
You were setting the ReadOnly property in the CellContentClick event which will be executed only when user clicks on the grid cells. So, when you create a new object of the form like this,
FrmList frmlist = new FrmList();
it will just create a new instance of the form with the Properties set in the designer. Since the ReadOnly property is set to false by default and the code you wrote to set it to true has not executed, the DataGridView will be editable.
Ref:
DataGridView read only cells
this.dgridvwMain.Rows[index].Cells["colName"].ReadOnly = true;
add this to your code:
dataGridView1.ReadOnly = true;
or you can change Read Only property in designer mode.
Check if the form is reinitialized on navigation. You can set a breakpoint in the constructor. This depends on your navigation service, or how ever it is implemented. In this case you can set the ReadOnly Flag to the last value on initialization or implement it as singleton.
Make the the entire DataGridView read only.
For more information visit MSDN
private void Button8_Click(object sender, System.EventArgs e)
{
foreach (DataGridViewBand band in dataGridView.Columns)
{
band.ReadOnly = true;
}
}

Preventing user to click a Tab Page

I haven't found a solution yet related to this problem. I just to disable other tabpages in my Winforms TabControl when a certain tabpage is open. So not hiding them but disable the function to open them when one clicks a tab page. It just should be displayed grey. Is this possible? I've read something about a "Selected" event but don't know how to use that.
You can use the Selecting event:
Create a class level variable:
int lockedPage = -1;
If it is set to the index of a TabPage you can select it but you can't leave it, i.e. you can't select any other page.
private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
{
if (lockedPage >= 0 && e.TabPageIndex != lockedPage) e.Cancel = true;
}
If you set lockedPage = 0; you prevent the user from leaving the 1st page etc..
To re-enable the selection of other pages set it to -1

How to make textbox text disappear when I click on it

I managed to create textboxes dynamically in C#. When a textbox has text, can I make it disappear when I click on it?
I need to put a word inside textboxes that is the result of a select in Oracle.
Assign a value to the Text property of the TextBox. You could subscribe then to the GotFocus event, and set the text value to an empty string.
// Holds a value determining if this is the first time the box has been clicked
// So that the text value is not always wiped out.
bool hasBeenClicked = false;
private void TextBox_Focus(object sender, RoutedEventArgs e)
{
if (!hasBeenClicked)
{
TextBox box = sender as TextBox;
box.Text = String.Empty;
hasBeenClicked = true;
}
}
javascript is good for the delete.
onclick="$(this).val('');"
alternatively you can use HTML5 placeholder

Disable gridview cell editing with RepositoryItemTextEdit

I'm trying to disable editing for specific gridview cells.
I'm using a RepositoryItemTextEdit with the following properites:
repositoryItemTextEdit.AllowFocused = false;
m_repositoryItemTextEdit.ReadOnly = true;
However i can still click the cell and the edit cursor is present even if i can't change the value.
Is there a way o get rid of the text cursor?
Thank you
I got a same problem the cell and the edit cursor is present while after disabled.
And i got the solution.
private void tree_ShowingEditor(object sender, CancelEventArgs e)
{
Nodes.PromptNode promptNode = tree.FocusedNode as Nodes.PromptNode;
if (tree.FocusedColumn == valueColumn && promptNode.PromptResult.ValueType.MyValueType == ValueType.ValueTypeOptions.Calculated)
{
e.Cancel = true;
}
}
Using ShowingEditor event to cancelled this.
Basically, you have to handle the ShownEditor Event of the GridView. In there, you test the focused row and column and if the cell should be readonly, you do:
grdView.ActiveEditor.Properties.ReadOnly = True
To make things nice and understanable for the user, you can also handle the CustomDrawCell Event, and set the background color (e.Appearance) to the color used for your readonly controls.
This might be somewhat besides the point, since it doesn't get rid of the cursor; but I don't see what that would be useful.

Categories