Why does TextChanged fire when a RichEditBox gains focus? - c#

I have a RichEditBox in a C# Windows Runtime app. I have set it to set a Boolean flag IsFileUpToDate to false on TextChanged like so:
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (IsFileUpToDate != false)
{
IsFileUpToDate = false;
}
}
When the page is first navigated to, IsFileUpToDate should be set to true. I have set it like so:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
IsFileUpToDate = true;
}
However, the RichEditBox gains focus immediately when the page loads, and this seems to be causing it set the Boolean to false even though the text hasn't been changed. Why is it doing this? How can I rewrite these commands so that the Boolean is reliably set?

RichEditBox is getting it's text changed on when it is loaded
you can have another flag to make sure it won't change yours on the fisrt time
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (IsFirstload)
{
IsFirstLoad = false;
return;
}
if (IsFileUpToDate != false)
{
IsFileUpToDate = false;
}
}

Related

How to catch event with hardware back button on xamarin forms?

I know the solution on Android
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
switch(keyCode){
case KeyEvent.KEYCODE_BACK:
// do something here
return true;
}
return super.onKeyDown(keyCode, event); }
But i cant do it on the forms side. The focus is an Entry so the keyboard is visible and when i press hardware back button (keyboard disappear) i'd like to set my variable value.
Edited:
bool _tapped;
LwTeszt.PropertyChanged += (s, e) =>
{
if (LwTeszt.SelectedItem != null)
{
EntryTeszt.Focus();
}
};
EntryTeszt.Unfocused += EntryTeszt_Unfocus;
private void EntryTeszt_Unfocus(object sender, FocusEventArgs e)
{
_tapped = true;
}
private void ViewCell_Tapped(object sender, EventArgs e)
{
if (_tapped)
{
EntryTeszt.Unfocus();
_tapped = false;
return;
}
EntryTeszt.Focus();
}
Unless you have additional logic that you didn't post I think you might be overcomplicating things. I believe all you need to do is focus your entry on ViewCell_Tapped
private void ViewCell_Tapped(object sender, EventArgs e)
{
EntryTeszt.Focus();
}
Forms will handle unfocusing your Entry for you when back is pressed and in that case you won't need the LwTeszt.PropertyChanged, _tapped, and the EntryTeszt.Unfocused parts.
There is an Event you have to just override that and you can handle back button in Xamarin forms. Use below code for handling back button:
protected override bool OnBackButtonPressed()
{
// your code
return base.OnBackButtonPressed();
}
Also you can refer this question for more information.

Action perform button visible false does not work

Well, I have this situation, in a program I put a Button whose code is activated with PerformClick (programmatically), that button must be invisible in the interface so I put the value visible=false since the beginning of the program but the action on the event click doesn't perform, but if I put visible = true, the action actually is performed, any ideas of the problem?
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
_btnRecargarInsumos.PerformClick();
}
this.Close();
}
_btnRecargarInsumos: is the button and is actually performed in another Form.
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
cbACDescripcion: Combobox which will be "reloaded" with the values of the DataSet: dsDescripciones.
The property visible is false since the beginnig of the program, but I also try to set visible=true and just before the method PerformClick() change it, but is the same.
But if I put visible=true since the beginning it works in that way.
If you click a button that's not visible or not enabled, nothing happens, even if you click it programmatically. Here's a workaround that works for me, although it's a bit of a hack:
_btnRecargarInsumos.SuspendLayout();
_btnRecargarInsumos.Visible = true;
_btnRecargarInsumos.PerformClick();
_btnRecargarInsumos.Visible = false;
_btnRecargarInsumos.ResumeLayout();
Why not just put your code in a separate method?
Example:
private StuffToDoAtClick()
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
//Your Button.Click() code//
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
StuffToDoAtClick()
}
//Your Datagridview code//
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
StuffToDoAtClick();
}
this.Close();
}

using checkbox to enable textbox

i have an application here in winforms that am trying to make. This is how i want it to happen: whenever the user clicks on register visitor button the registration form should be opening. works fine. here is the function that is called in that case:
private void Register_Visitor_Load(object sender, EventArgs e)
On this form i have a textfield placed which i want to disable when the form loads. i wrote a line which disables the textbox on form load:
textbox1.enabled = false;
i placed the above line in the load function which is working fine. now i want to enable my textbox1 based on the checkbox checked. for this i wrote the code:
CheckState state = checkBox1.CheckState;
switch (state)
{
case CheckState.Checked:
{
textBox1.Enabled = true;
break;
}
case CheckState.Indeterminate:
case CheckState.Unchecked:
{
break;
}
now when i place the code above in the page load function nothing happens which is surely going to happen as that function is only called on form load. what am not getting is where to place the checkbox code so that my textbox is enable on runtime. other function are in response to button but what i want here it to instantly enable the textfield on runtime when the user checks the checkbox. kindly explain me how am i going to accomplish this!
You can use CheckStateChanged event; so whatever reason the checkBox1 is checked/unchecked/grayed you'll have the textBox1 properly enabled/disabled
private void checkBox1_CheckStateChanged(object sender, EventArgs e) {
textBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
you are placing code at wrong event.
Instead of placing in pageload place that code on chekchange event of checkbox.
That will help you.
private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
textBox1.Enable=true;
}
else
{
textBox1.Enable=false;
}
}
Place the above code inside the function which handles the event for check box.
In your case it is checkchanged status.
You can try this:
private void checkBox1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Enabled = false;
}
else
{
textBox1.Enabled = true;
}
}
I did a hybrid of some of the above answers and it worked perfectly. I wanted the state of a button to be disabled upon loading the form, but then enabled if the user checks a box, here's the code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
private void Form1_Load(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}

How to make textbox appear only after user clicks button in c#

How would I make a textbox appear only after clicking a button. THis means that It should be hidden, and once user clicks, then it can appear.
private void button7_Click(object sender, EventArgs e)
{
// .. what next?
}
You can use Control.Visible to make any control visible or hidden:
private void button7_Click(object sender, EventArgs e)
{
theTextBox.Visible = true;
}
Just set it's Visible property to false initially (ie: in the designer).
Assuming you have defined a TextBox textBox1 somewhere:
private void button7_Click(object sender, EventArgs e)
{
textBox1.Visible = !textBox1.Visible;
}
This way you can toggle the visibility.
You can set it just to true if you like, but make sure the initial Visible state, you can set it in the designer, is false.

Bit defines textbox visibility

I don't know how to fix or handle this issue. But I currenlty have 2 links as following:
NavigateUrl="~/Admin/ManageProducts.aspx?IsMeal=true and False.
When it is set to TRUE I want txtDescription to be visible, and when set to FALSE I wan't txtDescription to be invisible.
IsMeal is a BIT in my database. So I need to define somehow that, when ManageProducts.aspx?IsMeal=true then txtDescription should be visible, and reverse
FALSE = invisible
But how do I manage this?
In your Page_Load() method you can add the following:
protected void Page_Load(object sender, EventArgs e) {
txtDescription.Visible = Convert.ToBoolean(Request.QueryString("IsMeal"));
}
In the ManageProducts.aspx.cs file place this:
protected void Page_Load(object sender, EventArgs e) {
if (Request.QueryString["IsMeal"] != null) {
if (Boolean.Parse(Request.QueryString["IsMeal"])) {
txtDescription.Visible = true;
}
else {
txtDescription.Visible = false;
}
}
}
Wouldn't you just set the property accordingly:
bool isMeal = Convert.ToBoolean(Request.QueryString["IsMeal"]);
txtDescription.Visible = isMeal;

Categories

Resources