FindViewById not displaying id - c#

I have a button that I want to clear certain fields after it is pressed. I have a button event that is supposed to display a question before the clear functionality is implemented. After the user confirms they want to clear the button then the button will clear the text in the fields. However, as of now the dialog isn't displaying. Below is a sample of my Clear Button function. Please let me know if you see anything I am not seeing.
void btnPalletClear_Click(object sender, EventArgs e)
{
dialog = new Dialog(this, Android.Resource.Style.ThemeHoloLightDialogNoActionBarMinWidth);
View myView = View.Inflate(this, Resource.Layout.confirmation_dialog, null);
myView.FindViewById<TextView>(Resource.Id.txtConfirmTitle).Text = "Clear Pallet";
myView.FindViewById<TextView>(Resource.Id.txtConfirmMessage).Text = "Are you sure?";
myView.FindViewById<LinearLayout>(Resource.Id.llQuantity).Visibility = ViewStates.Gone;
myView.FindViewById<Button>(Resource.Id.cmdConfirmCancel).Click += delegate { dialog.Dismiss(); };
myView.FindViewById<Button>(Resource.Id.cmdConfirmOK).Click += delegate
{
dialog.SetContentView(myView);
dialog.Show();
txtPalletUNQ.Text = "";
adapter.lstPallet.Clear();
adapter.NotifyDataSetChanged();
txtPalletVTPID.Text = "";
};
}

I cut the dialog.SetContentView(myview), and dialog.Show() out and pasted it in outside of the cmdConfirmOk and it fixed it.

Related

Adding "OnClick" event to code behind button

So I've created a button, and I want to change its text by clicking on it.
This is my code behind (this is from the Page_Load code) :
Button InviteToGameBTN = new Button();
InviteToGameBTN.Click += new EventHandler(InviteToGameBTN_OnClick);
.
protected void InviteToGameBTN_OnClick(object sender,EventArgs e)
{
Button b1 = (Button)sender;
b1.Text = "Text changed";
}
What could be wrong ?
Thanks to all.
Not sure if this will 100% work but you may be able to use InviteToGameBTN.text to directly change it:
var button = FindViewById<Button>(Resource.Id.button1);
button.Click += delegate
{
button.Text = "changed text";
};
This was my little bit that I made in a new program, doesn't use an event handler but it uses the button.text to directly change its properties through the delegate { }

C# WinForms contextmenu focus issue when textbox is added

In a C# WinForms application I need to create a ContextMenuStrip with dropdown and textbox:
private System.Windows.Forms.ContextMenuStrip ct1;
private void button_Click(object sender, EventArgs e)
{
var header = new ToolStripMenuItem("Header");
header.Enabled = false;
var options = new ToolStripMenuItem("Options");
for (int i = 0; i < 5; i++)
{
var checkoption = new ToolStripMenuItem("Check Me " + i + "!");
checkoption.CheckOnClick = true;
options.DropDownItems.Add(checkoption);
}
var txt = new ToolStripTextBox();
txt.Text = "changeme";
options.DropDownItems.Add(txt);
options.DropDown.Closing += DropDown_Closing;
ct1.Items.Clear();
ct1.Items.Add(header);
ct1.Items.Add(options);
ct1.Show(this, button.Left, button.Top);
}
private void DropDown_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
e.Cancel = (e.CloseReason == ToolStripDropDownCloseReason.ItemClicked);
}
Now, e.Cancel will prevent closing the dropdown if the reason is ItemClicked, so I can select more items without having to open the menu again:
Please note that "changeme" is a ToolStripTextBox!
Once I focus it (click on it), I can edit the text inside:
After finish editing the textbox, I still can change the checkbox items, but there is no focus indicator:
How can I get back the focus indicator just as shown on the first picure?
Note: if I move the mouse onto "Header", the dropdown will close, and then moving it back to "Options", will reopen the dropdown and then the focus indicator is good again:
How can I do this without closing and reopening the dropdown?
I have tried Select() for the options item, but it did not help, neither Invalidate() on ct1.
Just have found it:
First needs to add a click handler on the dropdown:
options.DropDown.Click += DropDown_Click;
Then in the click handler it needs to be focused:
private void DropDown_Click(object sender, EventArgs e)
{
var dropdown = (ToolStripDropDown)sender;
dropdown.Focus();
}

Close dialog, when pressed OK button on it

If the user clicks on a button, a dialog shows up, asking to input a string, and there's an 'OK' button on the same dialog, when the user presses that, the dialog should close. That's at least the plan, the problem is: after adding the eventhandler to the OK button, my application freezes, when the user would open the dialog.
addNewFamButton = FindViewById<Button>(Resource.Id.newFamilyButton);
addNewFamButton.Click += (sender, e) => {
Dialog dialog = new Dialog(this);
dialog.SetContentView(Resource.Layout.addNewFamily);
dialog.SetTitle("Add new family to the list");
dialog.Show();
// Problem starts here:
Button saveNewFamily = FindViewById<Button>(Resource.Id.dialogButtonOK);
saveNewFamily.Click += (object o, EventArgs ea) => { dialog.Dispose(); };
};
I tried it with dialog.Cancel() too, but I got the same results. If I remove the last two lines, the dialog works, but obviously won't close.
FIXED: Thanks to user370305 for the simple solution:
Button saveNewFamily = dialog.FindViewById<Button>(Resource.Id.dialogButtonOK);
Your OK button is part of Dialog view, so you have to find that view using your dialog object reference, something like, (I am not familiar with xamarin but this one gives you hint)
Change your line,
// Problem starts here:
Button saveNewFamily = FindViewById<Button>(Resource.Id.dialogButtonOK);
with
Button saveNewFamily = dialog.FindViewById<Button>(Resource.Id.dialogButtonOK);
try this
// create an EditText for the dialog
final EditText enteredText = new EditText(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Title of the dialog");
builder.setView(enteredText);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int id)
{
// perform any operation you want
enteredText.getText().toString());// get the text
// other operations
dialog.cancel(); // close the dialog
}
});
builder.create().show();

Playing around with buttons in Windows Forms

I'm on to a small project where I try to make my own web browser.
I found out that a web browser is worthless without "New Tabs"-function, so I thought that I could use buttons as tabs and every time I press "ctrl + T" a new button appears.
The problems I encountered is:
-Array of buttons in a way that makes it possible for me to spawn a new button every time I press "ctrl + T"
-When the button is spawned it should be clickable and disabled when clicked until another tab (button) is click.
At the moment I focus on getting 1 tab to work, so here's an example:
private void TB_Address_KeyPress(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.T && e.Modifiers == Keys.Control)
{
Button tabButton = new Button();
tabButton = new System.Windows.Forms.Button();
tabButton.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
tabButton.Cursor = System.Windows.Forms.Cursors.Hand;
tabButton.ForeColor = System.Drawing.Color.Lime;
tabButton.Location = new System.Drawing.Point(154, 32);
tabButton.Name = "tabButton";
tabButton.Size = new System.Drawing.Size(152, 23);
tabButton.TabIndex = 13;
tabButton.Text = "Tab 2";
tabButton.UseVisualStyleBackColor = false;
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Controls.Add(tabButton);
}
}
I also have this click function:
private void tabButton_Click(object sender, EventArgs e)
{
tab_1.Enabled = true;
tabButton.Enabled = false;
}
"tab_1" is a button created in the design mode.
"tabButton.Enabled" is red marked because it cannot find tabButton.
I understand why it cannot be found. But I have no idea about how to solve the problem in a good way.
You are assigning the tabButton_Click to all buttons with this line:
tabButton.Click += new System.EventHandler(this.tabButton_Click);
Just cast the sender to button and you will get the button who fired the event:
void tabButton_Click(object sender, EventArgs e)
{
Button buttonSender = (Button) sender;
buttonSender.Enabled=false;
}
You are not finding "tab_1" because it is not a valid name inside the tabButton_Click scope.
That's why you have to cast the sender object to WindowsForms Button, and then change its properties.
I'm going with a different method.
Creating all the buttons needed initially.
Sorry for wasting your time.

How to create an onClick eventHandler for a dynamically created button

Currently, I am doing a project for students' hostel and now I have to implement some search strategies about students.Here I have to create a button dynamically when the user clicks on the another server button in .aspx page and accordingly I have to create the onclick event handler for the newly created button. The code-snippet that I used is:
protected void btnsearchByName_Click(object sender, EventArgs e)
{
TextBox tbsearchByName = new TextBox();
Button btnsearchName = new Button();
tbsearchByName.Width = 250;
tbsearchByName.ID = "tbsearchByName";
tbsearchByName.Text = "Enter the full name of a student";
btnsearchName.ID = "btnsearchName";
btnsearchName.Text = "Search";
btnsearchName.Click += new EventHandler(this.btnsearchName_Click);
pnlsearchStudents.Controls.Add(tbsearchByName);
pnlsearchStudents.Controls.Add(btnsearchName);
}
protected void btnsearchName_Click(object sender, EventArgs e)
{
lblsearch.Text = "btnsearchName_Click event fired in " + DateTime.Now.ToString();
}
Here, the problem is newly created eventHandler doesnot get fired. I have gone through this site and looked several questions and answers and also gone through the page life-cycle and they all say that the dynamic button should be on Init or Pre_init, but my problem is I have to create it when another button is clicked, how can it be possible?
You need to add the click handler for the button on every postback.
you could look for the button in the search students panel on page load or try the page OnInit() method to add the handler when its created.
Also check here:
Dynamically added ASP.NET button click handler being ignored
and here:
asp.net dynamically button with event handler
and here:
asp:Button Click event not being fired
(all of which give similar suggestions)
Try this http://msdn.microsoft.com/ru-ru/library/system.web.ui.webcontrols.button.command(v=vs.90).aspx
btnsearchName.Command += new CommandEventHandler(this.btnsearchName_Click);
btnsearchName.CommandName = "Click";
You need to recreate the button and attach the event handler every time. For this, create a list of button and save it on session. On page load, go through the List and create the button every time
public Button create_button()
{
btnsearchName.ID = "btnsearchName";
btnsearchName.Text = "Search";
btnsearchName.Click += new EventHandler(this.btnsearchName_Click);
return btnsearchName;
}
public TextBox create_textbox()
{
TextBox tbsearchByName = new TextBox();
Button btnsearchName = new Button();
tbsearchByName.Width = 250;
tbsearchByName.ID = "tbsearchByName";
tbsearchByName.Text = "Enter the full name of a student";
return tbsearchByName;
}
protected void btnsearchByName_Click(object sender, EventArgs e)
{
TextBox tbsearchByName = create_textbox();
Button btnsearchName = create_button();
//add to panels
pnlsearchStudents.Controls.Add(tbsearchByName);
pnlsearchStudents.Controls.Add(btnsearchName);
//add to session
List<Button> lstbutton = Session["btn"] as List<Button>
lstbutton.add(btnsearchName);
//similarly add textbox
//again add to session
Session["btn"] = lstbutton
}
public override page_load(object sender, eventargs e)
{
//fetch from session, the lstButton and TextBox and recreate them
List<Button> lstbutton = Session["btn"] as List<Button>;
foreach(Button b in lstbutton)
pnlsearchStudents.Controls.Add(b);
//similar for textbox
}
I am not sure but may be you have to override the OnInit() method like this.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
}
You just need to add this code on ready state of jquery code and it will work fine for the dynamic button too
$(document).ready(function(){
$('input#tbsearchByName').click(function(){
// code goes here
});
});

Categories