Unable to handle a button click event in SAP B1 UI API - c#

I'm starting with SAP B1 UI API (9.0) and I'm trying to handle a button click without any luck so far. This is how I'm doing it (removing unnecessary to make it shorter):
static void Main(string[] args)
{
SetApplication(args);
var cParams = (FormCreationParams)App.CreateObject(BoCreatableObjectType.cot_FormCreationParams);
cParams.UniqueID = "MainForm_";
cParams.BorderStyle = BoFormBorderStyle.fbs_Sizable;
_form = App.Forms.AddEx(cParams);
/*Setting form's title, left, top, width and height*/
// Button
var item = _form.Items.Add("BtnClickMe", BoFormItemTypes.it_BUTTON);
/*Setting button's left, top, width and height*/
var btn = (Button)item.Specific;
btn.Caption = "Click Me";
_form.VisibleEx = true;
App.ItemEvent += new _IApplicationEvents_ItemEventEventHandler(App_ItemEvent);
}
private static void SetApplication(string[] args)
{
string connectionString = args[0];
int appId = -1;
try
{
var guiApi = new SboGuiApi();
guiApi.Connect(connectionString);
App = guiApi.GetApplication(appId);
}
catch (Exception e)
{ /*Notify error and exit*/ }
}
private static void App_ItemEvent(string FormUID, ref ItemEvent pVal, out bool BubbleEvent)
{
BubbleEvent = true;
if (FormUID == "MainForm_" && pVal.EventType == BoEventTypes.et_CLICK &&
pVal.BeforeAction && pVal.ItemUID == "BtnClickMe")
{
App.MessageBox("You just click on me!");
}
}
When I click the button nothing happens, is this the way to go? I've made so many variations in the handler method but nothing yet. Another detail is that the visual studio's debugger terminates as soon as the addon is started (maybe this has something to do with my problem).
I hope you can help me. Thanks in advance.
David.

Since the application stops running there are two possible answers to this question depending on what you prefer to use.
If you are using the SAPbouiCOM library you need a way to keep the application running, the way I use is the System.Windows.Forms.Application.Run(); from the windows forms assembly.
If you are using the SAPBusinessOneSDK and SAPbouiCOM.Framework as a reference you can use the App.Run();.
Both of these need to be invoked as soon as your setup code has run.

Related

ASP.NET Webform - how to stop the clicking of a button multiple times

New here and relatively new to development and very new to ASP.
I have created a WCF service with a method that ultimately inserts a record into a SQL table and calls an email send class.
On my ASP web form that consumes the service, I have a button that invokes one of the methods.
What I dont understand and wish to fix, the method all in takes around 10 seconds to complete (I need to get this down but thats for further down the line). I can keep clicking the button before the first click has had a chance to complete and in turn send the email. With each click, it queues up and before you know it you have 10 identical emails.
I want to disable the button until the method has returned.
I have spent all day trying to resolve this and have looked and multiple forums, coming up with nothing. So any help is appreciated. - I cant work out what I'm missing.
Service Method;
public class A : IA
{
public int Set_A(string a, int t, string d, string c, int f)
{
using (var dbAbs = new Entities())
{
if (a != "" && d != "" && c != "")
{
// Do stuff - add to entity etc.
if (tl != null)
{
try
{
SendMail(tl.TL_E, a, t, d, c, f);
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
Retval = dbAbs.SaveChanges();
}
else
{
retval = -99;
}
return retval;
}
else return -1;
}
}
Code behind the button click on the web form;
protected void AddA_Click(object sender, EventArgs e)
{
AddA.Enabled = false;
AddA.Visible = false;
var absvc = new AService.AClient();
// setting up variables
int ret = absvc.Set_A(a, ab, d, c, f);
//error handling
AddA.Enabled = true;
AddA.Visible = true;
}
As you can see, I've tried to use .Enabled and .Visible but they never seem to work.
Any help is appreciate, I've ive not provided enough info just shout at me!
Cheers
Liam
Ryan Wilson was correct - It is a duplicate question.
Thanks to kmonty on other post for solving this for me.
For reference, the below worked for me.
Here is a solution that works for the asp.net button object. On the
front end, add these attributes to your asp:Button definition:
<asp:Button ... OnClientClick="this.disabled=true;" UseSubmitBehavior="false" />
In the back end, in the click event handler method call, add this code
to the end (preferably in a finally block)
myButton.Enabled = true;

office DocumentBeforeSave events only work after bind in few seconds

I'm working on a feature which is to create a backup when a open word saved each times.
I'm using the blow code to hooking into word process and bind events to it, the word is opened by process.
officeApplication = (Application)Marshal.GetActiveObject("Word.Application").
officeApplication.DocumentBeforeSave += new ApplicationEvents4_DocumentBeforeSaveEventHandler(App_BeforeSaveDocument);
And in App_BeforeSaveDocument I did my work.
I get officeApplication right, and bind events were fine, when I click save in word, the events triggered perfectly.
The problem is, a few seconds(may be 30s) after, the events will not fire anymore, no matter click save or save us or close document.
Is there any suggestions?
After a lot of researching, I still can't find the reason. And I decide to use a trick to approach it.
First, open a thread in the binding event:
static void App_BeforeSaveDocument(Microsoft.Office.Interop.Word.Document document, ref bool saveAsUI, ref bool cancel)
{
if (th != null)
th.Abort();
th = new Thread(backupOnSave);
th.IsBackground = true;
th.Start(document);
}
Then do an infinity loop in the thread:
internal static void backupOnSave(object obj)
{
try
{
Application app = obj as Application;
if (app == null || app.ActiveDocument == null)
{
return;
}
Microsoft.Office.Interop.Word.Document document = app.ActiveDocument;
if (!tempData.ContainsKey(document.FullName))
return;
var loopTicks = 2000;
while (true)
{
Thread.Sleep(loopTicks);
if (document.Saved)
{
if (!tempData.ContainsKey(document.FullName))
break;
var p = tempData[document.FullName];
var f = new FileInfo(p.FileFullName);
if (f.LastWriteTime != p.LastWriteTime)//changed, should create new backup
{
BackupFile(p, f);
p.LastWriteTime = f.LastWriteTime;
}
}
}
}
catch (Exception ex)
{
log.write(ex);
}
}
And it works fine. Don't remember to abort the thread when the document closed or exception happen.

Let user start typing when winform pops up without them having to click on the textbox

I am working on a barcode reader project in Visual Studio using C#. I have created a WinForm Application and have added a RichTextBox to it. I want the user to be able to start scanning when they open the program without having to click on the textbox.
Thanks in advance!
(I'm assuming you have an application with a multitude of stuff in it. However there is one field that needs to be filled in with a scanned barcode.)
I faced a simular issue a while ago. I needed to capture a barcode in WPF. Setting the focus property in load seemed a good idea but because there were a multitude of other controls on the page that the user could click etc. focus jumped from one control to the other, making the barcode go in the wrong fields or vanish in a grid that has focus for example.
We were not able to use any other way of reading the barcode from the scanner because it was used for other applications too. It had to be configured as input.
We came up with a solution of capturing the keypresses instead.
By using the keydown events we could track the scanner input and stated that if more than 5 keys came in within a limited time + with our prefix and suffix it had to be a barcode.
EDIT: here is a simplified version of the class.
public delegate void BarcodeRead(string barcode);
public class ManualReader
{
private string barcode = "no barcode detected";
private string possible = "";
private DateTime timestarted = DateTime.MinValue;
private Timer InputTimeout;
public BarcodeRead OnBarcodeRead;
public void OnKeyDown(object sender, KeyEventArgs.KeyEventArgs e)
{
//create timer if it does not exist
if (InputTimeout == null)
{
InputTimeout = new Timer(100);
InputTimeout.Enabled = true;
InputTimeout.Elapsed += new ElapsedEventHandler(OnTimedEvent);
}
//reset timer
InputTimeout.Stop();
InputTimeout.Start();
//possible barcode
possible += CharToKey.GetCharFromKey(e);
if (timestarted == DateTime.MinValue)
{
timestarted = DateTime.Now;
}
}
//Timer elapses
private void OnTimedEvent(object sender, ElapsedEventArgs e)
{
//Is it a barcode?
if ((timestarted.AddMilliseconds(600) > DateTime.Now) && (possible.Length > 5)
&& (timestarted != DateTime.MinValue) && possible.Contains("\r"))
{
barcode = possible;
barcode = barcode.Remove(0, 1);
barcode = barcode.Replace("\r", "");
//launch delegate
if (OnBarcodeRead != null)
{
OnBarcodeRead.Invoke(barcode);
}
}
//delete timers
timestarted = DateTime.MinValue;
InputTimeout.Dispose();
InputTimeout = null;
possible = null;
}
}
}
I'm aware that for really short timeouts datetime functions aren't precise but still this little 'hack' worked perfectly for our application.
You can add directly in the element. This works for textbox but not sure with RichTexBox
FocusManager.FocusedElement="{Binding RelativeSource={RelativeSource Self}}"

Show a Balloon notification

I'm trying to use the below code to show a Balloon notification. I've verified that it's being executed by using breakpoints. It's also showing no errors.
What should I do to debug this since it's not throwing errors and not showing the balloon?
private void showBalloon(string title, string body)
{
NotifyIcon notifyIcon = new NotifyIcon();
notifyIcon.Visible = true;
if (title != null)
{
notifyIcon.BalloonTipTitle = title;
}
if (body != null)
{
notifyIcon.BalloonTipText = body;
}
notifyIcon.ShowBalloonTip(30000);
}
You have not actually specified an icon to display in the task bar. Running your code in LINQPad, by simply adding notifyIcon.Icon = SystemIcons.Application before the call to ShowBalloonTip I was able to get the tip to be displayed. Also note that you should call Dispose when you are done with your NotifyIcon instance.
Matthew identified the issue, but I still struggled to put all the pieces together. So I thought a concise example that works in LINQPad as-is would be helpful (and presumably elsewhere). Just reference the System.Windows.Forms assembly, and paste this code in.
var notification = new System.Windows.Forms.NotifyIcon()
{
Visible = true,
Icon = System.Drawing.SystemIcons.Information,
// optional - BalloonTipIcon = System.Windows.Forms.ToolTipIcon.Info,
// optional - BalloonTipTitle = "My Title",
BalloonTipText = "My long description...",
};
// Display for 5 seconds.
notification.ShowBalloonTip(5000);
// This will let the balloon close after it's 5 second timeout
// for demonstration purposes. Comment this out to see what happens
// when dispose is called while a balloon is still visible.
Thread.Sleep(10000);
// The notification should be disposed when you don't need it anymore,
// but doing so will immediately close the balloon if it's visible.
notification.Dispose();
See the below source code.
using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace ShowToolTip
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btBallonToolTip_Click(object sender, EventArgs e)
{
ShowBalloonTip();
this.Hide();
}
private void ShowBalloonTip()
{
Container bpcomponents = new Container();
ContextMenu contextMenu1 = new ContextMenu();
MenuItem runMenu = new MenuItem();
runMenu.Index = 1;
runMenu.Text = "Run...";
runMenu.Click += new EventHandler(runMenu_Click);
MenuItem breakMenu = new MenuItem();
breakMenu.Index = 2;
breakMenu.Text = "-------------";
MenuItem exitMenu = new MenuItem();
exitMenu.Index = 3;
exitMenu.Text = "E&xit";
exitMenu.Click += new EventHandler(exitMenu_Click);
// Initialize contextMenu1
contextMenu1.MenuItems.AddRange(
new System.Windows.Forms.MenuItem[] { runMenu, breakMenu, exitMenu });
// Initialize menuItem1
this.ClientSize = new System.Drawing.Size(0, 0);
this.Text = "Ballon Tootip Example";
// Create the NotifyIcon.
NotifyIcon notifyIcon = new NotifyIcon(bpcomponents);
// The Icon property sets the icon that will appear
// in the systray for this application.
string iconPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + #"\setup-icon.ico";
notifyIcon.Icon = new Icon(iconPath);
// The ContextMenu property sets the menu that will
// appear when the systray icon is right clicked.
notifyIcon.ContextMenu = contextMenu1;
notifyIcon.Visible = true;
// The Text property sets the text that will be displayed,
// in a tooltip, when the mouse hovers over the systray icon.
notifyIcon.Text = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipText = "Morgan Tech Space BallonTip Running...";
notifyIcon.BalloonTipTitle = "Morgan Tech Space";
notifyIcon.ShowBalloonTip(1000);
}
void exitMenu_Click(object sender, EventArgs e)
{
this.Close();
}
void runMenu_Click(object sender, EventArgs e)
{
MessageBox.Show("BallonTip is Running....");
}
}
}
For the sake of future coders:
the [timeout] parameter is deprecated as of windows vista
See: C# NotifyIcon Show Balloon Parameter Deprecated
So you might as well just put 0 into the parameter for > Windows Vista. What's worse, comments on the linked answer suggests that the replacement for these balloons, toast notifications, were only introduced in Windows 8. So for poor old Windows 7 falling between two stools, with Vista < 7 < 8, we seem to be at the mercy of however long Windows wants to keep that balloon there! It does eventually fade away, I've noticed, but after some empirical testing I'm quite sure that parameter is indeed being ignored.
So, building on the answers above, and in particular taking the lambda functions suggested by #jlmt in the comments, here's a solution that works for me on Windows 7:
//Todo: use abstract factory pattern to detect Windows 8 and in that case use a toastnotification instead
private void DisplayNotificationBalloon(string header, string message)
{
NotifyIcon notifyIcon = new NotifyIcon
{
Visible = true,
Icon = SystemIcons.Application
};
if (header != null)
{
notifyIcon.BalloonTipTitle = header;
}
if (message != null)
{
notifyIcon.BalloonTipText = message;
}
notifyIcon.BalloonTipClosed += (sender, args) => dispose(notifyIcon);
notifyIcon.BalloonTipClicked += (sender, args) => dispose(notifyIcon);
notifyIcon.ShowBalloonTip(0);
}
private void dispose(NotifyIcon notifyIcon)
{
notifyIcon.Dispose();
}
Notes
I've put a TODO in there to write another implementation for Windows
8, as people are 50/50 now on Windows 7/8 so would be good to support
a newer functionality. I guess anyone else coding this for multiple
versions of windows should probably do the same, ideally. Or just
stop supporting 7 and switch to using ToastNotification.
I purposely defined the disposal in a function so I could debug and verify that the breakpoint was indeed being hit.
ShowBalloonnTip takes the number of milliseconds. 3 milliseconds might be too fast for you to even see. Try something more like 3000
You might need to pass a component model to the contructor. It's what I see in all the examples. Sorry been a long time since I've used it. See first answer here:
NotifyIcon not showing
Take a look at the example here http://msdn.microsoft.com/en-us/library/system.windows.forms.notifyicon.aspx
I see some distinct differences between it an your code, there are many pieces you're leaving out such as creating a ComponentModelContainer and passing that into the NotifyIcon's constructor.

How to update text in a label

I have here a long method that takes a little while to execute. I would like to keep the user entertained so I created a progress bar and a label. What I would like is for that label to change while the system executes the progress. Ive been looking at Application.DoEvents(), but it seems like thats the wrong way to go. This application is pretty simple and its just a project and nothing professional. All this app does is send a file to a client and insert the data into a database.
I have one label (besides a success and error label), that I would like to constantly update along side the progress bar. Any ideas or tips on how to do this? Would Application.DoEvents() be acceptable in this situation? Or is there a simple way to update the text. I am using C#, asp.net, and a System.Web.UI.Page. Any help or pointing me to the right direction would be greatly appreciated.
protected void Button_Click(object sender, EventArgs e)
{
PutFTPButton.Enabled = false;
Thread.Sleep(3000);
Button btn = (Button)sender;
KaplanFTP.BatchFiles bf = new KaplanFTP.BatchFiles();
KaplanFTP.Transmit transmit = new KaplanFTP.Transmit();
//label text change here
if (btn.ID == PutFTPButton.ID)
{
//bf.ReadyFilesForTransmission();
DirectoryInfo dir = new DirectoryInfo(#"C:\Kaplan");
FileInfo[] BatchFiles = bf.GetBatchFiles(dir);
bool result = transmit.UploadBatchFilesToFTP(BatchFiles);
//label text change here
if (!result)
{
ErrorLabel.Text += KaplanFTP.errorMsg;
return;
}
bf.InsertBatchDataIntoDatabase("CTL");
bf.InsertBatchDataIntoDatabase("HDR");
bf.InsertBatchDataIntoDatabase("DET");
bf.InsertBatchDataIntoDatabase("NTS");
List<FileInfo> allfiles = BatchFiles.ToList<FileInfo>();
allfiles.AddRange(dir.GetFiles("*.txt"));
bf.MoveFiles(allfiles);
//label text change here
foreach (string order in bf.OrdersSent)
{
OrdersSentDiv.Controls.Add(new LiteralControl(order + "<br />"));
}
//lblWait.Visible = false;
OrdersSentDiv.Visible = true;
OrdersInfoDiv.Visible = false;
SuccessLabel.Visible = true;
NoBatchesToProcessLbl.Visible = true;
BatchesToProcessLbl.Visible = false;
PutFTPButton.Enabled = false;
BatchesCreatedLbl.Text = int.Parse(NextBatchNum).ToString();
Thread.Sleep(20000);
if (KaplanFTP.errorMsg.Length != 0)
{
ErrorLabel.Visible = true;
SuccessLabel.Visible = false;
ErrorLabel.Text = KaplanFTP.errorMsg;
}
}
}
I think you can use an Ajax UpdateProgress control, check Progress Bar on File Upload ASP.NET.
EDIT: Another one Displaying Progress Bar For Long Running Processes using ASP.NET AJAX.
Application.DoEvents() is not available in an ASP.NET application, nor is it's use acceptable in a standard WinForms application with the advent of multicore processors and the .NET threading library.
A web application requires communication to/from a server. Therefore simply updating the text of a label does nothing unless you are sending that back to the client. In your case you would need an event which was signaled by this line (because it is a batch upload):
transmit.UploadBatchFilesToFTP(BatchFiles);
The event would update the value you want to display. You would then need some AJAX code (or an ASP.NET update panel around a ASP.NET label) on the web page in question to get and display the new value.
HTH
delegate void SetTextCallback(string text);
private void SetText(string text)
{
if (this.label1.InvokeRequired)
{
SetTextCallback d = new SetTextCallback(SetText);
this.Invoke(d, new object[] { text });
}
else this.label1.Text = text;
}
void SomeMethod()
{
SetText(yourVariable.ToString());
}
if i understand you correctly this should work.

Categories