Why textbox doesn't print the text, but I got string ? - c#

I am using Windows Form;
I want to set TextBox tbCommandName1.Text which is on my Form;
I got the value from GetInfo() and how can I send string from Second.GetInfo() to my main Form like to class Favorit ?
I don't want to create an instance of Form; Because it makes to Initialize all my components again.
I bet I have to use get/set.
Give me please, a few hints;
THanks a lot to you, my guru!
namespace ParserFavorit
{
public partial class Favorit : Form, IForm
{
public Favorit()
{
InitializeComponent();
}
public string CommandName1
{
get { return tbCommandName1.Text; }
set { tbCommandName1.Text = value; }
}
private void bStart_Click(object sender, EventArgs e)
{
string ID = tbGetID.Text;
Second.StartBrowser(Second.GetDriver(), ID);
}
}
public class Second : Form
{
private static ChromeDriver driver = null;
public static ChromeDriver GetDriver()
{
if (driver == null)
{
driver = new ChromeDriver();
}
return driver;
}
public static void StartBrowser(ChromeDriver driver, string IDevent)
{
driver.Navigate().GoToUrl("myURL" + IDevent);
GetInfo();
}
public static void GetInfo()
{
System.Threading.Thread.Sleep(2000);
string CommandName1 = driver.FindElement(By.XPath(".//*[#id='react-root']/div/div[2]/div/div[1]/div/div[1]/div/div[1]/div/header/div[2]/span[1]")).Text;
string CommandName2 = driver.FindElement(By.XPath(".//*[#id='react-root']/div/div[2]/div/div[1]/div/div[1]/div/div[1]/div/header/div[2]/span[2]")).GetAttribute("innerHTML");
}
}
interface IForm
{
string CommandName1 { get; set; }
}
}

I am sure that I have a TextBox with name tbCommandName1;
My form doesn't show the value which I got from website, but method PrintName got it.
How so?
namespace ParserFavorit
{
public partial class Favorit : Form
{
public Favorit()
{
InitializeComponent();
}
private void bStart_Click(object sender, EventArgs e)
{
string ID = tbGetID.Text;
Second.StartBrowser(Second.GetDriver(), ID);
}
public void PrintName(string Command1Name)
{
string Name = Command1Name;
tbCommandName1.Text = Name;
}
}
public class Second
{
private static ChromeDriver driver = null;
public static ChromeDriver GetDriver()
{
if (driver == null)
{
driver = new ChromeDriver();
}
return driver;
}
public static void StartBrowser(ChromeDriver driver, string ID)
{
driver.Navigate().GoToUrl("https://m.favorit.com.ua/uk/live/events/" + ID);
GetInfo();
}
public static void GetInfo()
{
System.Threading.Thread.Sleep(2000);
string CommandName1 = driver.FindElement(By.XPath(".//*[#id='react-root']/div/div[2]/div/div[1]/div/div[1]/div/div[1]/div/header/div[2]/span[1]")).Text;
Favorit favorit = new Favorit();
favorit.PrintName(CommandName1);
}
}
}

Related

How to call RefreshProcess(SaveEventTriggerModelArgs obj) in Window_Loaded?

I have the following code, I would like to call the function RefreshProcess(SaveEventTriggerModelArgs obj) from MainWindow_Loaded.
However the problem I am running into due to lack of knowledge working with window apps I calling this method inside.
It will not let me because of the arguments SaveEventTriggerModelArgs obj and if I add those into RefreshProcess, they are different from void MainWindow_Loaded(object sender, RoutedEventArgs e). How to do it?
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.Loaded+= Window_Loaded;
}
private void RefreshProcess(SaveEventTriggerModelArgs obj)
{
var rect = new Rect();
Dispatcher.Invoke(() =>
{
obj.CurrentEventTriggerModel.ProcessInfo = new ProcessInfo()
{
ProcessName = "Nox" != null ? $"Nox" : "",
Position = rect,
};
});
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
}
}
public class SaveEventTriggerModelArgs : INotifyEventArgs
{
public Model CurrentEventTriggerModel { get; set; }
}
public class MousePointEventArgs : INotifyEventArgs
{
public ViewModel MousePointViewMode { get; set; }
}
public class ViewModel
{
}
public class Model
{
public ProcessInfo ProcessInfo { get;set;}
}
public class ProcessInfo
{
public string ProcessName { get;set;}
public Rect Position { get;set;}
}

How to use the AppSettings plugin on Xamarin? I want to be able to save what the user entered on EmailEntry

How to use the AppSetting on Xamarin plugin. I installed it. Now how to use it, I'm using Entry field with name on it to get the email address from the EmailEntry on xmlfile.
I tried some code on Main Page but the email entered is not getting save.
Settings.cs
namespace DrainLog.Utils
{
public static class Settings
{
private static ISettings AppSettings
{
get
{
return CrossSettings.Current;
}
}
#region Setting Constants
private const string LastEmailSettingsKey = "last_email_key";
private static readonly string SettingsDefault = string.Empty;
#endregion
public static string LastUsedEmail
{
get
{
return AppSettings.GetValueOrDefault(LastEmailSettingsKey,
SettingsDefault);
}
set
{
AppSettings.AddOrUpdateValue(LastEmailSettingsKey, value);
}
}
}
}
MainPage
namespace DrainLog
{
public partial class MainPage : ContentPage
{
public static string item;
public MainPage()
{
InitializeComponent();
drainquatity();
EmailAddress = DrainLog.Utils.Settings.LastUsedEmail;
}
public string emailAddress;
public string EmailAddress
{
get{return emailAddress;}
set
{
emailAddress = value;
DrainLog.Utils.Settings.LastUsedEmail = value;
}
private void EnterButton_clicked(object sender, EventArgs e)
{
bool isNameEmpty = string.IsNullOrEmpty(nameEntry.Text);
bool isEmailEmpty = string.IsNullOrEmpty(emailEntry.Text);
if (isNameEmpty || isEmailEmpty)
{
}
else
{
Navigation.PushAsync(new HomePage());
}
}
}
}
}
I want to be able to save the email address that the user entered so when the user exits the app then re opens it. It's still there live it's save.

Cross-Thread Call using MVP WIndows Forms

I would like to use MVP Design pattern for a WinForm App but i'm facing the problem of calling a View Update from another thread.
Here's my code
MODEL
public class Model : IModel
{
public string Status { get; set; }
public async void LongOperation(IHomeView View)
{
for (int i = 0; i < 1000; i++)
{
View.StatusListView = i.ToString();
}
}
}
PRESENTER
public class HomePresenter
{
IHomeView _IView;
IModel _IModel;
Model _Model = new Model();
public HomePresenter(IHomeView IView)
{
_IView = IView;
}
public async void LaunchLongOperation()
{
await Task.Run(() => _Model.LongOperation(_IView));
}
}
INTERFACE VIEW-PRESENTER
public interface IHomeView
{
string StatusListView { get; set; }
}
INTERFACE PRESENTER-MODEL
public interface IModel
{
string Status { get; set; }
}
FORM:
public partial class frmMain : Form, IHomeView
{
HomePresenter _Presenter;
public frmMain()
{
InitializeComponent();
_Presenter = new HomePresenter(this);
}
public string StatusListView
{
get
{
return lstActivityLog.Text;
}
set
{
lstActivityLog.Items.Add(value);
}
}
private void btnAvvia_Click(object sender, EventArgs e)
{
_Presenter.launchLongOperation();
}
}
i would like to update a list view in the Main form during the long operations of the Model class.
Which is the best way to do that?
Try this code without debugging, you'll be surprised about it works!
The quick and dirty way to make it work in debugging mode as well is to add Control.CheckForIllegalCrossThreadCalls = false; into the constructor of your form.
public partial class MainForm : Form, IHomeView
{
HomePresenter _Presenter;
public MainForm()
{
InitializeComponent();
Control.CheckForIllegalCrossThreadCalls = false; //<-- add this
_Presenter = new HomePresenter(this);
}
public string StatusListView
{
get
{
return lstActivityLog.Text;
}
set
{
lstActivityLog.Items.Add(value);
}
}
private void button1_Click(object sender, EventArgs e)
{
_Presenter.LaunchLongOperation();
}
}

how to convert 'this' in "Self Referencing Generics"

I want to add an observer in my model, i try to generic delegate but here is problem when invoke.
Here is my code and it works when I use 'handler.DynamicInvoke(this)' instead of 'Invoke'
but I know DynamicInvoke is slow... I want to know is here a right way to use Invoke.
public class Model<T>
{
public delegate void UpdatePrototype<T>(T mdl);
private List<UpdatePrototype<T>> listeners = new List<UpdatePrototype<T>>();
public void Bind(UpdatePrototype<T> handler)
{
listeners.Add(handler);
}
public void Sync()
{
foreach(UpdatePrototype<T> handler in listeners)
{
handler.Invoke((T)this); // << ERROR: can not convert Model<T> to T
}
}
public string Name = "Model";
}
public class MyModel : Model<MyModel>
{
public string Name = "MyModel";
}
public class YourModel : Model<YourModel>
{
public string Name = "YourModel";
}
void Main()
{
MyModel mdl = new MyModel();
mdl.Bind(MyUpdate);
mdl.Sync();
YourModel your = new YourModel();
your.Bind(YourUpdate);
your.Sync();
}
void MyUpdate(MyModel mdl)
{
Debug.Log(mdl.Name);
}
void YourUpdate(YourModel mdl)
{
Debug.Log(mdl.Name);
}
============
thanks #IVAAAN123 i modify my code as follow.
it is fine for me, although mdl.Sync<MyModel>() has a little odd ;)
public class Model<T>
{
public delegate void UpdatePrototype<T>(T mdl);
private List<UpdatePrototype<T>> listeners = new List<UpdatePrototype<T>>();
public void Bind(UpdatePrototype<T> handler)
{
listeners.Add(handler);
}
public void Sync()
{
foreach(UpdatePrototype<T> handler in listeners)
{
handler.DynamicInvoke(this);
}
}
public void Sync<T>() where T : Model<T>
{
foreach(UpdatePrototype<T> handler in listeners)
{
handler.Invoke((T)this);
}
}
public string Name = "Model";
}
public class MyModel : Model<MyModel>
{
public string Name = "MyModel";
}
public class YourModel : Model<YourModel>
{
public string Name = "YourModel";
}
void Main()
{
MyModel mdl = new MyModel();
mdl.Bind(MyUpdate);
mdl.Sync<MyModel>();
mdl.Sync();
YourModel your = new YourModel();
your.Bind(YourUpdate);
your.Sync<YourModel>();
your.Sync();
}
void MyUpdate(MyModel mdl)
{
Debug.Log(mdl.Name);
}
void YourUpdate(YourModel mdl)
{
Debug.Log(mdl.Name);
}
}
public class Model<T>
{
public delegate void UpdatePrototype<S>(S mdl);
private List<UpdatePrototype<Model<T>>> listeners = new List<UpdatePrototype<Model<T>>>();
public void Bind(UpdatePrototype<Model<T>> handler)
{
listeners.Add(handler);
}
public void Sync()
{
foreach (UpdatePrototype<Model<T>> handler in listeners)
{
handler(this);
}
}
public virtual string Name
{
get
{
return "Model";
}
}
}
public class MyModel : Model<MyModel>
{
public override string Name
{
get
{
return "MyModel";
}
}
}
public class YourModel : Model<YourModel>
{
public override string Name
{
get
{
return "YourModel";
}
}
}
void main()
{
MyModel mdl = new MyModel();
mdl.Bind(MyUpdate);
mdl.Sync();
YourModel your = new YourModel();
your.Bind(YourUpdate);
your.Sync();
}
void MyUpdate(Model<MyModel> mdl)
{
Console.WriteLine("MY MODEL HANDLER");
Console.WriteLine(mdl.Name);
}
void YourUpdate(Model<YourModel> mdl)
{
Console.WriteLine("YOUR MODEL HANDLER");
Console.WriteLine(mdl.Name);
}

how toaccess the attributes in another form? c#

I have a problem in a same namespace:
public partial class frmForm1 : Form // Form1
{
public class Account
{
public string Username;
public string Password;
}
public class ListAcc
{
public static int count = 0;
private static List<Account> UserList;
public static List<Account> Data()
{
return UserList;
}
}
}
public partial class frmForm2 : Form // Form2
{
private void button2_Click(object sender, EventArgs e)
{
frmForm1.Account A;
string m = frmForm1.ListAcc<A>.[0].Username; //ERROR
}
}
How could i access the attributes (Username, Password...) in frmForm1? Someone help me? Thanks!
string m = frmForm1.ListAcc.Data()[0].Username
But, you must have first element in your username list.
Complete Source code:
public class Account
{
public string Username;
public string Password;
}
public class ListAcc
{
public static int count = 0;
private static List<Account> UserList;
public static List<Account> Data()
{
return UserList;
}
ListAcc()
{
UserList = new List<Account>();
UserList.Add(new Account() { Username = "x", Password = "y" });
}
}
public partial class frmForm1 : Form // Form1
{
public static ListAcc;
}
public partial class frmForm2 : Form // Form2
{
private void button2_Click(object sender, EventArgs e)
{
string m = frmForm1.ListAcc.Data()[0].Username;
}
}

Categories