Changing an Object's properties in another class? - c#

I'm trying to change an Object's properties from another class, like so.
abilities.cs (class)
public static void hideAllButtons()
{
frmFight fight = new frmFight();
fight.btnAbility1.Visible = false;
fight.btnAbility2.Visible = false;
fight.btnAbility3.Visible = false;
fight.btnAbility4.Visible = false;
fight.btnAbility5.Visible = false;
fight.btnAbility6.Visible = false;
vars.buttonsVisible = false;
}
I'm trying to use the method from the previous class to change the object's properties in the following Form;
frmFight.cs (form)
private void btnAbility1_Click(object sender, EventArgs e)
{
abilities.hideAllButtons();
btnAbilities.Enabled = false;
}
I've tried everything in my knowledge and understanding, and a lot of looking up on the internet. I've tried making the objects static, public, creating the object within the class. But nothing works. Usually I get StackOverFlow errors.
I'm pretty new to OOP, too, but I'm not an idiot so don't think to go too 'lightly' on me with a possible fix, or cause, of my problem - if you understand, that is.

Method hideAllButtons is static so should have a Form argument.
public static void hideAllButtons(frmFight fight)
{
fight.btnAbility1.Visible = false;
fight.btnAbility2.Visible = false;
fight.btnAbility3.Visible = false;
fight.btnAbility4.Visible = false;
fight.btnAbility5.Visible = false;
fight.btnAbility6.Visible = false;
//vars.buttonsVisible = false; // What about this???
}
and call this method in click handler,
hideAllButtons(this);

This really wont work because your controls is on your frmFight.
You need to do is place
private void hideAllButtons()
{
btnAbility1.Visible = false;
btnAbility2.Visible = false;
btnAbility3.Visible = false;
btnAbility4.Visible = false;
btnAbility5.Visible = false;
btnAbility6.Visible = false;
vars.buttonsVisible = false;
}
on your frmFight.cs (form) as private method and just call it on your button click to make it simpler.
private void btnAbility1_Click(object sender, EventArgs e)
{
hideAllButtons();
btnAbilities.Enabled = false;
}
Regards

Related

What is NewWindow2 and NewWindow3 in WebForm/WebBrowser

I am trying to convert our current WebForms app to use CefSharp browser which uses ChromimumWebBrowser. I came across the below in current code:
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
if (!newWindow3ListenerAdded) {
newWindow3ListenerAdded = true;
SHDocVw.WebBrowser browser = (SHDocVw.WebBrowser)webBrowser.ActiveXInstance;
browser.NewWindow3 += Browser_NewWindow3;
browser.NewWindow2 += browser_NewWindow2;
}
}
private void browser_NewWindow2(ref object ppDisp, ref bool Cancel)
{
webform.webBrowser.GoBack();
ppDisp = webform.webBrowser.ActiveXInstance;
webform.Show();
}
private void Browser_NewWindow3(ref object ppDisp, ref bool cancel, uint dwFlags, string sourceUrl, string targetUrl)
{
cancel = true;
if (targetUrl.Contains(".pdf")) {
WebForm webForm = new WebForm();
webForm.Text = targetUrl;
webForm.Navigate(targetUrl);
webForm.Show();
} else {
webBrowser.Navigate(targetUrl);
}
}
Could someone please guide me in converting this to Cef. I know I have to implement OnBeforePopup() in ILifeSpanHandler. But how exactly can I write the logic to load pdf/GoBack/Navigate(targetUrl) for different types of popups.
I think I can easily convert Browser_NewWindow3() the way it is (checking if targetUrl.Contains(".pdf")) but on what condition do I handle browser_NewWindow2() logic i.e. webBrowser.GoBack(); ?
This is what I tried,
OnBeforePopup() {
newBrowser = null;
if (targetUrl.Contains(".pdf")) {
CefForm cefForm = new CefForm();
cefForm.Text = targetUrl;
cefForm.Navigate(targetUrl);
cefForm.Show();
return false;
} else {
browserControl.Load(targetUrl);
return true;
}
}
But how do I handle browser_NewWindow2 logic ?

C# enabling or disabling control

So, I'm losing my mind over a bug in my software.
I have this code, I use it 2 times in my software, the other function similar (with a different name) works normally. But this one is inverted...
I mean by that : Instead of enabling the controls when the groupbox.text contains "INC", it disable them.
Any idea on what is going on?
`private void Enable_disableSTM()
{
if (STM_groupBox.Text.Contains("INC"))
{
STM_radioButton_appel.Enabled = true;
STM_radioButton_autre.Enabled = true;
STM_radioButton_resolution.Enabled = true;
STM_Textbox_SR.Enabled = true;
STM_textBox_remarque.Enabled = true;
STM_Dropdown_Sendto.Enabled = true;
STM_pictureBox_Boutonenvoyer.Enabled = true;
}
else
{
STM_radioButton_appel.Enabled = false;
STM_radioButton_autre.Enabled = false;
STM_radioButton_resolution.Enabled = false;
STM_Textbox_SR.Enabled = false;
STM_textBox_remarque.Enabled = false;
STM_Dropdown_Sendto.Enabled = false;
STM_pictureBox_Boutonenvoyer.Enabled = false;
}
} `
Edit :
Like I said, in my software I have this other function that is working fine. I tried also to change my IF to STM_Textbox_reademail.Text != "" and it's still not working correctly. It's inverted. Enabling when it should not and Disabling too.
`if (SQ_TextBox_reademail.Text != "")
{
SQ_radioButton_appel.Enabled = true;
SQ_radioButton_autre.Enabled = true;
SQ_radioButton_resolution.Enabled = true;
SQ_Textbox_SR.Enabled = true;
SQ_textBox_remarque.Enabled = true;
SQ_Dropdown_Sendto.Enabled = true;
SQ_pictureBox_Boutonenvoyer.Enabled = true;
}
else
{
SQ_radioButton_appel.Enabled = false;
SQ_radioButton_autre.Enabled = false;
SQ_radioButton_resolution.Enabled = false;
SQ_Textbox_SR.Enabled = false;
SQ_textBox_remarque.Enabled = false;
SQ_Dropdown_Sendto.Enabled = false;
SQ_pictureBox_Boutonenvoyer.Enabled = false;
} `
Edit 2 : Okay... I figured out something that works. I'm calling my function at a different place now and it's working. Still does not make sense why I can call the other one at the same place and it works but this one doesn't... but hey... now it works! thanks all!
Your problem is that you are checking if a string contains the "INC" word in a case sensitive way the solution is changing the if statement to check in the string the inc word ignoring the case :
private void Enable_disableSTM()
{
if (STM_groupBox.Text.IndexOf("INC", StringComparison.OrdinalIgnoreCase) >= 0;)
{
STM_radioButton_appel.Enabled = true;
STM_radioButton_autre.Enabled = true;
STM_radioButton_resolution.Enabled = true;
STM_Textbox_SR.Enabled = true;
STM_textBox_remarque.Enabled = true;
STM_Dropdown_Sendto.Enabled = true;
STM_pictureBox_Boutonenvoyer.Enabled = true;
}
else
{
STM_radioButton_appel.Enabled = false;
STM_radioButton_autre.Enabled = false;
STM_radioButton_resolution.Enabled = false;
STM_Textbox_SR.Enabled = false;
STM_textBox_remarque.Enabled = false;
STM_Dropdown_Sendto.Enabled = false;
STM_pictureBox_Boutonenvoyer.Enabled = false;
}
}
Okay... I figured out something that works. I'm calling my function at a different place now and it's working. Still does not make sense why I can call the other one at the same place and it works but this one doesn't... but hey... now it works! thanks all!

How to prevent hanging of application when a button is clicked multiple times?

I have an application where user can click on a Scan button to scan the image to preview in the application. When user clicks, usually a "Preparing to scan" message will be shown and goes away when the scan is 100% complete.
The scan works fine. The problem if I stress test it by pressing the scan button many times while it's doing it's work, the application completely hangs and the message just stays there and I had to restart my whole application.
The code: It's just a small section
private void ScanStripButton_Click(object sender, EventArgs e)
{
if (SCAN_INTO_BATCH)
{
GENERATE_BATCH_FOLDER = true;
StartTwainScan();
}
}
Any idea on how to prevent this issue?
Appreciate the help
EDIT:
public void StartTwainScan()
{
Boolean EnableUI = false;
Boolean ADF = false;
Boolean EnableDuplex = false;
if (Properties.Settings.Default.TwainShow.Equals("1"))
{
EnableUI = true;
}
if (Properties.Settings.Default.ScanType.Equals("2"))
{
ADF = true;
}
if (Properties.Settings.Default.DuplexEnable.Equals("1"))
{
EnableDuplex = true;
}
var rs = new ResolutionSettings
{
Dpi = GetResolution(),
ColourSetting = GetColorType()
};
var pg = new PageSettings()
{
Size = GetPageSize()
};
var settings = new ScanSettings
{
UseDocumentFeeder = ADF,
ShowTwainUI = EnableUI,
ShowProgressIndicatorUI = true,
UseDuplex = EnableDuplex,
Resolution = rs,
Page = pg
};
try
{
TwainHandler.StartScanning(settings);
}
catch (TwainException ex)
{
MessageBox.Show(ex.Message);
//Enabled = true;
//BringToFront();
}
}
This isn't going to be the correct answer, but you haven't shown enough code to give you the right code. It should point you in the right direction.
private void ScanStripButton_Click(object sender, EventArgs e)
{
ScanStripButton.Enabled = false;
if (SCAN_INTO_BATCH)
{
GENERATE_BATCH_FOLDER = true;
StartTwainScan();
}
ScanStripButton.Enabled = true;
}
Basically you disable the button when the scan starts and enable it when it finishes.
private async void ScanStripButton_Click(object sender, EventArgs e)
{
await Task.Run(() =>
{
if (SCAN_INTO_BATCH)
{
GENERATE_BATCH_FOLDER = true;
StartTwainScan();
}
});
}
or
private bool clicked = false;
private void ScanStripButton_Click(object sender, EventArgs e)
{
try
{
if(clicked)
return;
clicked = true;
if (SCAN_INTO_BATCH)
{
GENERATE_BATCH_FOLDER = true;
StartTwainScan();
}
}
finally
{
clicked = false;
}
}

How to use a dynamic forms names in a class (C#.Net)

I had long time developing windows applications using VB.NET and one of thing that I was always use is:
Class (Test) and inside it there are some subs like the following:
VB.NET Code
Friend Sub FLoadKeyLock(ByVal FRMNAME As Object)
FRMNAME.Button3.Enabled = true;
FRMNAME.Button4.Enabled = true;
FRMNAME.Button1.Enabled = false;
FRMNAME.Button2.Enabled = false;
FRMNAME.Button5.Enabled = false;
FRMNAME.Button6.Enabled = false;
FRMNAME.Button7.Enabled = false;
FRMNAME.Button8.Enabled = false;
FRMNAME.Button9.Enabled = false;
FRMNAME.Button10.Enabled = false;
End Sub
Where FRMNAME refers to forms name that I send from different forms and I use (me) keyword to send current form name to apply that sub.
That was totally works fine in VB.NET but I cannot use it the same way in C#.NET.
C#.NET Code
internal void FLoadKeyLock (Form FRMNAME)
{
FRMNAME.Button3.Enabled = true;
FRMNAME.Button4.Enabled = true;
FRMNAME.Button1.Enabled = false;
FRMNAME.Button2.Enabled = false;
FRMNAME.Button5.Enabled = false;
FRMNAME.Button6.Enabled = false;
FRMNAME.Button7.Enabled = false;
FRMNAME.Button8.Enabled = false;
FRMNAME.Button9.Enabled = false;
FRMNAME.Button10.Enabled = false;
}
The Error is: System.Windows.Forms.Form' does not contain a definition for 'Button3' and no extension method 'Button3' accepting a first argument of type
And same error for all used buttons.
So I have two questions:
1 - How to use such a function in C#.NET?
2 - Why it's not working the same way as VB.NET?
As #Maarten said.
dynamic has solved my problem
internal void FLoadKeyLock (dynamic FRMNAME)
{
FRMNAME.Button3.Enabled = true;
FRMNAME.Button4.Enabled = true;
FRMNAME.Button1.Enabled = false;
FRMNAME.Button2.Enabled = false;
FRMNAME.Button5.Enabled = false;
FRMNAME.Button6.Enabled = false;
FRMNAME.Button7.Enabled = false;
FRMNAME.Button8.Enabled = false;
FRMNAME.Button9.Enabled = false;
FRMNAME.Button10.Enabled = false;
}

Methods not working

In my main form, I am running this:
this.disableForm();
btnAbort.Enabled = true;
disableForm is an extension method for Form in my program defined as the following:
public static void disableForm(this Form f)
{
foreach (Control c in f.Controls)
{
f.Enabled = false;
}
f.Cursor = Cursors.WaitCursor;
}
The problem is that the next command btnAbort.Enabled = true; doesn't do anything.
It works if I put the code directly in the method and not call disableForm(). Why is this happening? Does it have something to do with threads?
This line:
f.Enabled = false;
Should be
c.Enabled = false;
The problem is because you are accidentally disabling your entire form.
public static void disableForm(this Form f)
{
foreach (Control c in f.Controls)
{
//f.Enabled = false;
c.Enabled = false;
}
f.Cursor = Cursors.WaitCursor;
}

Categories

Resources