Page load of user control and parent page - user-controls

I have a user control for showing message of actions. It is hidden on page load of user control, so that it disappears once a warning is acted upon. But in certain cases when a page loads, i want the user control to be visible, but doesn't due to hiding in page load of user control. How can I manage this?
usercontrol
protected void Page_Load(object sender, EventArgs e)
{
this.Visible = false;
}
public void SetMessage(string title, string desc)
{
this.Visible = true;
Title = title;
Description = desc;
}
parent page
protected void Page_Load(object sender, System.EventArgs e)
{
msgDialogue.SetMessage(a);
}

From what I understood, you need to show or hide a usercontrol depending on a property value in your page (or in a session variable). If this is the case, you would better add a placeholder in your page, then load the control (or not load it) depending on your condition.
if(condition)
{
_usercontrol ctrl = LoadControl("~/usercontrols/_usercontrol.ascx") as _usercontrol;
Placeholder1.Controls.Add(ctrl);
}
//otherwise, do not load the control
Hope it helps.

Related

Show popup or smart dialog once on screen load

I want to show a popup with some message to user whenever screen gets loaded for the first time for each record.
For example when Sales Order screen is loaded for particular sales order, it should show the popup only once. Then user navigates to a next sales order, it should again show the popup for that particular sales order only once.
I have written the code in constructor and RowSelected event, but it does not has the Current record. That is CRCurrentCaseNotes is always null in both this events. However, with the button (ViewNotes in below code sample), it works.
[PXViewName("CRCurrentCaseNotes")]
[PXCopyPasteHiddenView]
public PXSelect<Note,
Where<CRCase.caseID, Equal<Current<CRCase.caseID>>>> CRCurrentCaseNotes;
public CRCaseMaintExtension()
: base()
{
if (CRCurrentCaseNotes.Current != null)
{
CRCurrentCaseNotes.AskExt();
}
}
protected virtual void CRCase_RowSelecting(PXCache cache, PXRowSelectingEventArgs e)
{
var caseRow = (CRCase)e.Row;
if (caseRow == null) return;
if (CRCurrentCaseNotes.Current != null)
{
CRCurrentCaseNotes.AskExt();
}
}
// SAME CODE WORKS WITH THE BUTTON CLICK
public PXAction<CRCase> viewNotes;
[PXUIField(DisplayName = "View Notes")]
[PXButton]
protected virtual IEnumerable ViewNotes(PXAdapter adapter)
{
if (CRCurrentCaseNotes.Current != null)
{
CRCurrentCaseNotes.AskExt();
}
return adapter.Get();
}
Try to show the dialog when the primary DAC key field is modified:
protected void CRCase_CaseID_FieldUpdated(PXCache sender, PXFieldUpdatedEventArgs e)
{
CRCase case = e.Row as CRCase;
if (case != null && case.CaseID != null && !e.ExternalCall)
{
// Show dialog
}
}
If there's no other way you could use JavaScript to show/hide the SmartPanel:
document.getElementById("<%=SmartPanelID.ClientID %>").style.display = 'block';
EDIT:
Dialog can only be shown from an action event handler (FieldUpdated won't work) or from JavaScript. To open dialog when the page open, you can try hooking DocumentReady event in JavaScript and call the Acumatica action from JavaScript too: px_alls['ds'].executeCallback('ActionName');

How can I override or act on KeyDown: Space or Enter for ListView in UWP?

I've attached the KeyDown event to a ListView in my Win 10 UWP app. I want to make VirtualKey.Enter have a special effect, but the event is not firing for this particular key. Neither does it for Space, Arrow up or down. This I guess because the listview already has defined a special behaviour for those keys.
I'd like to override some of those keys though, or at least trigger additional actions. Even attaching events to those key with modifiers (e.g. Shift+ArrowDown) would not work because the events still are not firing.
I read that for WPF that there is a PreviewKeyDown-event which one can attach to. I can't find that event for UWP though. Are there any other options?
Stephanie's answer is a good one and it works in the general case. However, as Nilzor observed it will not work in the case of a ListView for the Enter key. For some reason the ListView handles the KeyDown event in case Enter is pressed.
A better way to handle key events when dealing with a ListView, as the question asks, is this.
private void ListView_Loaded(object sender, RoutedEventArgs e)
{
(sender as ListView).AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(ListView_KeyDown), true);
}
private void ListView_KeyDown(object sender, KeyRoutedEventArgs args)
{
if (args.Key == Windows.System.VirtualKey.Enter)
{
}
}
Notice the last argument in the AddHandler function. This specifies whether we want to handle events already handled by a previous element in the visual tree.
Of course don't forget to unsubscribe from the event when appropriate
Here is one way to do it : subscribe to the global Window.Current.CoreWindow.KeyDown event.
Then save the focus state of your listview and react accordingly.
Here is the code :
public sealed partial class MainPage : Page
{
bool hasFocus = false;
public MainPage()
{
this.InitializeComponent();
Window.Current.CoreWindow.KeyDown += CoreWindow_KeyDown;
}
private void CoreWindow_KeyDown(Windows.UI.Core.CoreWindow sender, Windows.UI.Core.KeyEventArgs args)
{
if(hasFocus)
{
Debug.Write("Key down on list");
}
}
private void myList_GotFocus(object sender, RoutedEventArgs e)
{
hasFocus = true;
}
private void myList_LostFocus(object sender, RoutedEventArgs e)
{
hasFocus = false;
}
You will also need to subscribe to the focus events in xaml, for your ListView :
<ListView .... GotFocus="myList_GotFocus" LostFocus="myList_LostFocus"/>
Corcus's solution doesn't work for me. What is working is handling PreviewKeyDown directly from XAML. Works well for SPACE or ENTER key:
XAML:
<ListView PreviewKeyDown="BookmarksListView_PreviewKeyDown">
Code behind:
private void BookmarksListView_PreviewKeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.Enter)
{
// DO YOUR STUFF...
e.Handled = true;
}
}
You can use AddHandler method.
private void KeyEnterEventHandler(object sender, KeyRoutedEventArgs e)
{
if (e.OriginalKey == Windows.System.VirtualKey.Enter)
{
PlayFromListView();
}
}
private void LoadListView()
{
foreach (var music in playListStorageFile.PlayList)
{
ListViewItem item = new ListViewItem();
item.AddHandler(FrameworkElement.KeyDownEvent, new KeyEventHandler(KeyEnterEventHandler), true);
TextBlock mytext = new TextBlock();
mytext.Text = music.Nro.ToString() + " - " + music.Name;
mytext.Tag = music.Nro;
item.Content = mytext;
lvMusics.Items.Add(item);
}
}
https://learn.microsoft.com/en-us/uwp/api/windows.ui.xaml.uielement.addhandler?view=winrt-18362

c# showing error while browsing a webpage

This is a test program. I just created a simple Windows application form with one button, and if the button is clicked, I need it to do something. So, I wrote my code as:
IWebDriver driver;
public Form1()
{
InitializeComponent();
}
public void SetupTest()
{
driver = new FirefoxDriver();
}
private void button1_Click(object sender, EventArgs e)
{
driver.Navigate().GoToUrl("webaddress");
driver.FindElement(By.TagName("Atlast")).Click();
Thread.Sleep(5000);
}
I have included all of the dependencies (both code and references), but I am getting the following error when I click the button:
Object reference not set to an instance of an object. in driver.navigate part of my code..
What mistake did I make here? Can anyone please help me out with this?
private void button1_Click(object sender, EventArgs e)
{
SetupTest()
driver.Navigate().GoToUrl("webaddress");
driver.FindElement(By.TagName("Atlast")).Click();
Thread.Sleep(5000);
}
You need to be calling SetupTest in your button click code. Why? This is where you are creating your new instance of the IWebDriver, therefore it needs to be called otherwise any references to driver will simply refer to null (by default).

How to close user control used in radgrid filtering?

I have a radgrid, for date column i have created a custom user control for filtering. I need to create a close button to close the user control. There are no close events which i can call. I don't want to make visibility collapsed. I started with something below:
public partial class DateFilterControl : UserControl, IFilteringControl
{
public event CloseEventHandler Close;
public delegate void CloseEventHandler();
private void Button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
It is throwing nullreference exception which is obvious to come. What code do i need to put to close the user control?
use Messaging Service. With this you can close the window in ViewModel so no need to give close function in Backend. Add Command property to Cancel button
<Button Content="Cancel" Command="{Binding CancelCommand}"/>
Now in the ViewModel add the RelayCommand property in that add
Messenger.Default.Send<bool>(true, typeof(XViewModel));
Now in the BackEnd of this userControl adds following in the constructor.
Messenger.Default.Register<bool>(this, typeof(ScheduleViewModel), (b) =>
{
if (b == true)
{
this.DialogResult = true;
}
});
Now u can close the Window...
This will surely helps u...

Fire parent click event when child is clicked in windows form user control

I have a user control
public partial class ButtonControl : UserControl
which has two controls of label and picturebox
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.text = new System.Windows.Forms.Label();
I have used this control in a windows form
this.appointmentButton = new DentalSoft.UI.Controls.ButtonControl();
created an event
this.appointmentButton.Click += new System.EventHandler(this.appointmentButton_Click);
but the problem is, if i click on the image or the label inside of the control, the click event doesn't fire.
I want to fire this click event no matter where the user clicks inside of the control. Is it possible?
Yes it is a simple matter. When you click on the child controls they receive the click event and the user control does not. So you can subscribe to the child control click events and when they occur simply raise the usercontrol click event and it will appear to click no matter where the mouse is positioned.
Just double click on the picturebox and the label to create the click event handlers then add a line of code to call the parent usercontrol OnClick method.
private void text_Click(object sender, EventArgs e)
{
this.OnClick(new EventArgs());
}
private void pictureBox1_Click(object sender, EventArgs e)
{
this.OnClick(new EventArgs());
}
I solved this problem the following way. I wanted to changes the background images of the parent PictureBox when the child Label was clicked. I played around some and found I could call the event handler in the following way.
private void Label_Click(object sender, EventArgs e)
{
Label label = (Label)sender;
Box_Click(label.Parent, e);
}
private void Box_Click(object sender, EventArgs e)
{
//
PictureBox box = sender as PictureBox;
//
if (isMine[int.Parse(box.Name)])
{
box.Image = Image.FromFile(#"..\..\images\BoxRedX.jpg");
MessageBox.Show("Game Over");
}
else
{
box.Image = Image.FromFile(#"..\..\images\BoxGray.jpg");
}
}
As you can see I grab the Child and using it to reference its parent and send it to the parents click event. This is the code I used to work in my project. The important method is the first on listed. The second is there for clarity to see how it passed and worked.

Resources