Windows application freezes most of the time on button click - forms

I am working on a windows application which freezes most of the time on button click events on Home Page. Please find the code below for your reference. Thanks
using System;
using System.Windows.Forms;
namespace FileMigrationAgen
{
public partial class HomePage : Form
{
public HomePage()
{
InitializeComponent();
}
private void tableLayoutPanel4_Paint(object sender, PaintEventArgs e)
{
}
private async void button1_Click(object sender, EventArgs e)
{
SharepointMigration sharepointMigration = new SharepointMigration();
sharepointMigration.Show();
this.Hide();
}
private async void button2_Click(object sender, EventArgs e)
{
OneDriveMigration oneDriveMigration = new OneDriveMigration();
oneDriveMigration.Show();
this.Hide();
}
private void HomePage_FormClosed(object sender, FormClosedEventArgs e)
{
Application.Exit();
}
}
}

I wouldn't recommend performing navigation in the manner that you have - hiding parent forms etc.
Have a look at this thread it has an example of using a static class to perform the navigation and keep track of the navigation stack.

Related

Click Object on Web Page in Xamarin forms

I wan to click button which created in web page.
I added following code to the my project but it did execute, string always null.
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
wbWeb.Source = "https://www.facebook.com";
}
private async void btnBrowser_Clicked(object sender, EventArgs e)
{
string str = wbWeb.EvaluateJavaScriptAsync($"document.getElementById('loginbutton').click();");
//str is null
}
}

Admob/Unity Reward Ad

When I test rewardAd, I can see the ad and I can get reward, but let's say I have 3 Revive ability I watched an ad and it gives 7 revive ability and let's say I watched again now it's not giving me reward, but if I spend my ability and watch again let's say I spend 2 now I have 8 when I watch reward ad it gives me 2 and it stucks at 10 again it never goes over 10 and if I have 2 ability it gives 8 if I have 5 ability it gives 5 it always completes at 10 I have no idea how to fix it, can you help me.
public void RequestRewardAd()
{
AdRequest request = new AdRequest.Builder().Build();
rewardBasedVideo.LoadAd(request, rewardBasedVideoId);
}
public void ShowRewardAd()
{
if (rewardBasedVideo.IsLoaded())
{
rewardBasedVideo.Show();
}
}
public void HandleRewardBasedVideoLoaded(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoLoaded event received");
}
public void HandleRewardBasedVideoFailedToLoad(object sender, AdFailedToLoadEventArgs args)
{
Debug.Log("HandleRewardBasedVideoFailedToLoad event received with message: "+ args.Message);
}
public void HandleRewardBasedVideoOpened(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoOpened event received");
}
public void HandleRewardBasedVideoStarted(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoStarted event received");
}
public void HandleRewardBasedVideoClosed(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoClosed event received");
RequestRewardAd();
}
public void HandleRewardBasedVideoRewarded(object sender, Reward args)
{
string type = args.Type;
PlayerPrefs.SetFloat("Revive", (int)args.Amount);
}
public void HandleRewardBasedVideoLeftApplication(object sender, EventArgs args)
{
Debug.Log("HandleRewardBasedVideoLeftApplication event received");
}

How do I add a mouseclick event to a winform treenode?

How do I add a mouseclick event to a winform treenode?
Update
Note that I want to do this at runtime.
To do this dynamically you need to handle the TreeView's NodeMouseClick event thusly:
using System;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
treeView1.NodeMouseClick +=
new TreeNodeMouseClickEventHandler(treeView1_NodeMouseClick);
treeView1.Nodes.Add(new TreeNode("Node 1"));
treeView1.Nodes.Add(new TreeNode("Node 2"));
}
void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
Console.WriteLine("Clicked: " + e.Node.Text);
}
}
}

Form Method on another thread not invoking the events

I am trying to achieve an update form.
I use a library to open a form when there is an updated file and download using edtFTPNet
In the form I pass the FTP object and start download, in FormLoad i handle two events and i use Thread to StartDownload(). My two events never invoking, i use them to set a progress bar.
public partial class UpdateProgressForm : XtraForm
{
public FTPConnection FtpConn { get; set; }
public string UpdateFileName { get; set; }
public UpdateProgressForm()
{
InitializeComponent();
}
private void OnLoad(object sender, EventArgs e)
{
FtpConn.Downloading += FileDownLoading;
FtpConn.BytesTransferred += FileBytesTransfered;
}
private void FileDownLoading(object sender, FTPFileTransferEventArgs e)
{
progressBar.Properties.Maximum = (int) e.FileSize;
}
private void FileBytesTransfered(object sender, BytesTransferredEventArgs e)
{
progressBar.Position = (int) e.ByteCount;
}
public void StartDownload()
{
FtpConn.DownloadFile(#".\" + UpdateFileName, UpdateFileName);
}
private void OnShown(object sender, EventArgs e)
{
Thread tt = new Thread(StartDownload) {IsBackground = true};
tt.Start();
}
}
Library method calling the Form:
private void DownloadUpdateFile(string updateFileName)
{
using (ProgressForm = new UpdateProgressForm { FtpConn = FtpConn, UpdateFileName = updateFileName })
{
ProgressForm.ShowDialog();
}
}
Any help? Thank you.
Take a look in the designer and make sure you subscribe to those events
Make sure you Instanciate and Show the from from the Main Thread.
Are you sure that the event handlers are not invoked? I think your problem rather is that you try to update the progress bar on the worker thread on which the event handlers are invoke (which is not the thread on which the GUI was created). You should make sure that the GUI updates are performed on the correct thread:
private void FileDownLoading(object sender, FTPFileTransferEventArgs e)
{
progressBar.Invoke((MethodInvoker) delegate
{
progressBar.Properties.Maximum = (int) e.FileSize;
});
}

How do you trigger an event across classes?

I am writing a Class Library that will be used by other applications. I am writing it in C#.NET. I am having a problem with triggering events across classes. Here is what I need to do...
public class ClassLibrary
{
public event EventHandler DeviceAttached;
public ClassLibrary()
{
// do some stuff
OtherClass.Start();
}
}
public class OtherClass : Form
{
public Start()
{
// do things here to initialize receiving messages
}
protected override void WndProc (ref message m)
{
if (....)
{
// THIS IS WHERE I WANT TO TRIGGER THE DEVICE ATTACHED EVENT IN ClassLibrary
// I can't seem to access the eventhandler here to trigger it.
// How do I do it?
}
base.WndProc(ref m);
}
}
Then in the application that is using the class library I will do this...
public class ClientApplication
{
void main()
{
ClassLibrary myCL = new ClassLibrary();
myCL.DeviceAttached += new EventHandler(myCl_deviceAttached);
}
void myCl_deviceAttached(object sender, EventArgs e)
{
//do stuff...
}
}
Probably the easiest option is to add a method to ClassLibrary which raises the event...i.e.
internal void RaiseDeviceAttached(object sender, EventArgs e)
{
if (DeviceAttached != null) DeviceAttached(sender, e);
}
Then, in OtherClass, simply call that method of ClassLibrary.
Another option is to go down the reflection route to trigger the event.