In my application I have this code in my view model:
hiddenTextContainer.PreHideVerticalOffset = VerticalOffset;
hiddenTextContainer.HiddenText = Text.Remove(SelectionStart, SelectionLength);
hiddenTextContainer.HasHiddenText = true;
hiddenTextContainer.NonHiddenTextStart = SelectionStart;
Text = Text.Substring(SelectionStart, SelectionLength);
SelectionStart = Text.Length;
hiddenTextContainer.ImmediatePostHideVerticalOffset = VerticalOffset;
This code is used to hide selected text in a textbox. Text is a string property data bound to the text property of a textbox and VerticalOffset is a double property data bound to the VerticalOffset property of that same textbox.
I need to save the VerticalOffset before and after the hiding of selected text takes place, but with my code below both hiddenTextContainer.PreHideVerticalOffset and hiddenTextContainer.ImmediatePostHideVerticalOffset are always set to the same value no matter what.
I have figured out that this is because the text of the textbox has not been updated by the time the code reaches:
hiddenTextContainer.ImmediatePostHideVerticalOffset = VerticalOffset;
Is there any way I can fix this?
It is probably not that the property of text is being updated too slowly, it's that the Measure and Arrange is being performed asynchronously. I'd suggest explicitly calling Window.UpdateLayout (or similar, depending on the container you need to recalculate).
Possibly, you'll need to do, InvalidateArrange or InvalidateMeasure first.
Related
I have a Windows Form with a number of Textboxes. I bind them to a DataSource as the Form Loads.
public partial class FormSettings : Form
{
private readonly DbContext _context = new DbContext();
public FormSettings()
{
InitializeComponent();
}
protected override void OnLoad(EventArgs e)
{
_context.OrderEntrySettings.Load();
SettingsBindingSource.DataSource = _context.OrderEntrySettings.Local.ToBindingList();
Binding saNbinding = new Binding("Text", SettingsBindingSource, "InvoiceNumber");
InvoiceNumberTextBox.DataBindings.Add(saNbinding);
Binding pthNbinding = new Binding("Text", SettingsBindingSource, "SalesOrderExportPath");
PathTextBox.DataBindings.Add(pthNbinding);
<snip>
…
</snip>
}
One of the Textboxes is bound to a Directory Path (string) Property.
If I type in a new directory in the Path Textbox and hit my save button, The path saves just fine.
But, If I change the Text Property on the Textbox to the SelectedPath from a FolderBrowserDialog dialog and then hit save, the Textbox Text shows the directory I selected but the Entity is not modified and nothing gets saved back to the database.
As a workaround, I set both the Path Textbox Text and set the Property from the context to the SelectedPath.
Why does a Bound Textbox not modify its associated property if the Text value is set programmatically?
Here is the reason. Binding class has a property called DataSourceUpdateMode with 3 options - Never, OnPropertyChanged and OnValidation, the last being a default. When you set the Text property programatically, there is no validating/validated events because they fire only when the control is on focus and has been edited, so the binding does not update the data source.
That being said, here are the options you have:
(A) Do not set the control property programatically, set the data source property instead.
(B) Change the binding DataSourceUpdateMode to OnPropertyChanged. This however will cause the data source property to be updated on every char that the user is typing.
(C) Use the following snippet:
yourTextBox.Text = ...;
yourTextBox.DataBindings["Text"].WriteValue();
If memory serves, data binding in WinForms (and possibly WebForms) uses the changes in control focus to detect when the data in the field has changed.
When you are modifying the values of the controls in the code-behind, you generally aren't changing the control focus. Consequently, there isn't anything that pokes the data-binding engine in the ribs and clues it in to the fact that it needs to re-evaluate the data.
I have a Form, that contain a DataGridView control named gridParameters.
I'm adding control on-top of it, based on some values, to add controls that are not available (for example, DateTimePicker):
DateTimePicker dtp = new DateTimePicker();
gridParameters.Controls.Add(dtp);
dtp.Location = rowCellLocation;
dtp.Size = rowCellSize;
dtp.CustomFormat = "yyyy/MM/dd";
dtp.Format = DateTimePickerFormat.Custom;
dtp.Tag = rowId;
dtp.ValueChanged += dtp_SelectedValueChanged;
dtp.Visible = true;
That code is inside a loop that iterate throgh the grid rows, and is in an If statement (I add DateTimePicker for some rows and NumericUpDown for others based on a row cell value). The variables rowCellLocation, rowCellSize and rowId are set in a loop for each row.
My DataGridView selection mode is a full row selection.
I want to make the added control (DateTimePicker, NumericUpDown...) get focus when I switch to the row, for example, when I click on a row with the mouse.
To do so, I've tried the following:
In the loop that creates the controls, I set the control to the relevant row Tag property.
In the grid Row_Enter event (I also tried CurrentCellChanged) I added the following code:
if (gridParameters.CurrentRow.Tag != null)
{
((Control)gridParameters.CurrentRow.Tag).Focus();
}
I've also tried using Select() and also
this.ActiveControl = (Control)gridParameters.CurrentRow.Tag;
None of the above sets focus to the control.
I place a break point, and the code is getting to this line, the Tag property contain the control that need to be focused, and execute it, but I can't get the control to focus.
UPDATE: Setting focus to another control outside of the DataGridView, works just fine (for example, setting focus to an "OK" button works).
I want to set the text in a combo box. Below is the code-
private System.Windows.Forms.ComboBox selectModel;
this.selectModel = new System.Windows.Forms.ComboBox();
this.selectModel.Name = "selectModel";
this.selectModel.FormattingEnabled = true;
this.selectModel.Size = new System.Drawing.Size(64, 21);
this.selectModel.Location = new System.Drawing.Point(3, 76);
this.selectModel.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
The following line is not working-
selectModel.SelectedText = getModelNameFromConf();
The documentation says that "it Gets or sets the text that is selected in the editable portion of a ComboBox". I can't make it editable to user.
Any workaround please.
This is because when you use ComboBoxStyle.DropDownList, the dropdown has no editable portion. To make it editable, use ComboBoxStyle.DropDown.
Note too the remarks on the SelectedText property relating to whether the control has focus. You may find the Text property more suitable for many purposes.
EDIT For example:
selectModel.Text = getModelNameFromConf();
Assuming the combo contains that value in its list, setting Text will also set the SelectedIndex property of the dropdown.
(I think some of the property names of this control are particularly confusing, including DropDown vs. DropDownList. Someone at MS had a bad day when this control was coded. Note also that the word selection is being used in two different ways: here, you want to set the selected item, whereas SelectedText means some text that is selected—which might not be the whole of the item text. This is the same as in a textbox where the user has dragged the mouse to highlight some of the text but not all of it.)
I have a method called void showdata() and If I do this...
textBox1.Text = ds.Tables[0].Rows[rno][0].ToString();
comboBox1.Text = ds.Tables[0].Rows[rno][1].ToString();
I am able to retrieve values from database but I am unable to retrieve value from radiobuttons e.g.
rBMale.Text = ds.Tables[0].Rows[rno][3].ToString();
by using navigation buttons.
When I say values I mean the green dot that you see from the application not the string value. I have named rBMale as radiobutton.
Does anyone know why that is?
Thanks
It is not clear, but it seems that you want to set the Checked property of the RadioButton.
rBMale.Checked = Convert.ToBoolean(ds.Tables[0].Rows[rno][3]);
This property is used to paint the RadioButton with the glyph to represent the value for true or for false
The Text property of radio buttons and check boxes is the on screen text that tells the user what the button is (in theory) and has little or nothing to do with its value. What you are probably looking for is the Checked property, which is a bool.
try something along these lines:
rBMale.Checked = Convert.ToBoolean(ds.Tables[0].Rows[rno][3]);
ASP.NET 1.1 - I have a DataGrid on an ASPX page that is databound and displays a value within a textbox. The user is able to change this value, then click on a button where the code behind basically iterates through each DataGridItem in the grid, does a FindControl for the ID of the textbox then assigns the .Text value to a variable which is then used to update the database. The DataGrid is rebound with the new values.
The issue I'm having is that when assigning the .Text value to the variable, the value being retrieved is the original databound value and not the newly entered user value. Any ideas as to what may be causing this behaviour?
Code sample:
foreach(DataGridItem dgi in exGrid.Items)
{
TextBox Text1 = (TextBox)dgi.FindControl("TextID");
string exValue = Text1.Text; //This is retrieving the original bound value not the newly entered value
// do stuff with the new value
}
So the code sample is from your button click event?
Are you sure you are not rebinding your datasource on postback?
When are you attempting to retrieve the value from the TextBox? i.e. when is the code sample you provided being executed?
If you aren't already, you'll want to set up a handler method for the ItemCommand event of the DataGrid. You should be looking for the new TextBox value within that method. You should also make sure your DataGrid is not being re-databound on postback.
I would also highly recommend reading through Scott Mitchell's excellent article series on using the DataGrid control and all of it's functions:
https://web.archive.org/web/20210608183626/https://aspnet.4guysfromrolla.com/articles/040502-1.aspx