In my Windows form application (WinForms), I'm using CEFSharp to open a web page. I want to modify the right-click context to allow the user to display the URL that was opened. Currently, the context has "Back", "Forward", "Print...", and "View Source"
The comment from #amaitland got the ball rolling. Here is my implementation. I hope this helps someone else.
When you initialize your instance of the WinForms.ChromiumWebBrowser, you set it's property MenuHandler to your instance of a IContextMenuHandler.
chromeBrowser = New WinForms.ChromiumWebBrowser(uri)
chromeBrowser.MenuHandler = New Classes.CefBasicMenuHandler()
Controls.Add(chromeBrowser)
Your implementation of an IContextMenuHandler is where you can control the context menu.
Public Class CefBasicMenuHandler
Implements IContextMenuHandler
private const ShowDevTools as Integer = 26501
private const CloseDevTools as Integer = 26502
Private Const CopyUrlAddress as Integer = 26503
Public Sub OnBeforeContextMenu(browserControl As IWebBrowser, browser As IBrowser, frame As IFrame, parameters As IContextMenuParams, model As IMenuModel) Implements IContextMenuHandler.OnBeforeContextMenu
'//To disable the menu then call clear
model.Clear()
'//Add new custom menu items
model.AddItem(CType(CopyUrlAddress, CefMenuCommand), "Copy URL address")
model.AddSeparator()
model.AddItem(CType(ShowDevTools, CefMenuCommand), "Show DevTools")
model.AddItem(CType(CloseDevTools, CefMenuCommand), "Close DevTools")
model.AddSeparator()
model.AddItem(CefMenuCommand.Reload, "Reload")
model.AddItem(CefMenuCommand.Copy, "Copy")
End Sub
Public Function OnContextMenuCommand(browserControl As IWebBrowser, browser As IBrowser, frame As IFrame, parameters As IContextMenuParams, commandId As CefMenuCommand, eventFlags As CefEventFlags) As Boolean Implements IContextMenuHandler.OnContextMenuCommand
Dim commandId1 As Integer = CType(commandId, Integer)
If commandId1 = ShowDevTools Then
browser.ShowDevTools()
End If
If commandId1 = CloseDevTools Then
browser.CloseDevTools()
End If
If commandId1 = CopyUrlAddress Then
Clipboard.SetText(parameters.PageUrl)
End If
Return False
End Function
Public Sub OnContextMenuDismissed(browserControl As IWebBrowser, browser As IBrowser, frame As IFrame) Implements IContextMenuHandler.OnContextMenuDismissed
End Sub
Public Function RunContextMenu(browserControl As IWebBrowser, browser As IBrowser, frame As IFrame, parameters As IContextMenuParams, model As IMenuModel, callback As IRunContextMenuCallback) As Boolean Implements IContextMenuHandler.RunContextMenu
Return False
End Function
End Class
Related
I am trying to set Value or SelectedIndex based on the datasource return after Read.
This is my View
#(Html.Kendo().DropDownList.
Name("ddlUsers").
DataTextField("text").
HtmlAttributes(New With {.style = "width:500px"}).
DataValueField("id").
DataSource(Sub(s)
s.Read(Sub(r) r.Action("GetUserList", "Employees")
End Sub). ServerFiltering(True)
End Sub).Events(Sub(e) e.Change("SetHiddenUGID")) )
The method GetUserList looks like this
Shared Function GetUserList() As IList
Return db.GetDBUserList().Where(Function(w) w.value <> 0).Select(Function(s) New With {.id = s.value,.text = s.text,.isdefault = s.isdefault}).ToList()
End Function
Now GetDBUserList returns a List of Employees
Public Class Employees
Public Property value As Int64
Public Property text As String
Public Property isdefault As Int32
End Class
I want to set the default value of the dropdown based on isdefault when it's 1, any ideas?
I have tried
var dropdownlist = $("#ddlUsers").data("kendoDropDownList");
dropdownlist.select(function (dataItem) {
if (dataItem.isdefault === 1) {
$("#ddlUsers").data("kendoDropDownList").value(dataItem.id);
}
});
But it did not work.
Your DropDownList is bound to remote data, so you cannot attempt to set the value until after your read action returns with the data.
So, you have to add your code to select the default item in the dataBound event of the DropDownList, as there is not data to select until that point.
But, your function is attempting to set the value to dataItem.id...your model does not have an id field...it has value, text, and isdefault.
Also, the DropDownList select() method takes jQuery selector, an index, or a function that returns a boolean (http://docs.telerik.com/kendo-ui/api/javascript/ui/dropdownlist#methods-select). Your function does not return anything...so it would result in nothing being selected.
Try doing this in the dataBound event of the DropDownList:
dataBound: function(e) {
var data = e.sender.dataSource.data();
e.sender.select(function (dataItem) {
return (dataItem.isdefault === 1);
});
}
Here's a working example: http://dojo.telerik.com/#Stephen/inUKI
It's using javascript initialization but you can add the dataBound event handler to your Razor initialization easily.
I'm using Facebook C# SDK to interact with my funpage, I wrote an ASP NET vb page that posts photo on the fun page wall, it works fine on my local machine, but I receive this error when I try to execute the page on my website:
Attempt by method 'DynamicClass.CallSite.Target(System.Runtime.CompilerServices.Closure, System.Runtime.CompilerServices.CallSite, System.Object)' to access type 'System.Runtime.CompilerServices.CallSite`1' failed.
It happens when I try to cicle the array with my page access tocken.(me/accounts)
the code below generates this error after _step = 3 :
Dim app As New FacebookClient(access_token)
_step = 1
Dim accts As Object = app.Get("/me/accounts")
_step = 2
' find the access token for the fan page
Dim page_access_token As String
_step = 3
For Each acct As Object In accts.data
_step = 4
If acct.id = Session("page_id").ToString() Then
_step = 5
page_access_token = acct.access_token
' get_album(page_access_token);
_step = 6
Dim fb As New FacebookClient(page_access_token)
_step = 7
Session.Add("page_access_token", page_access_token)
Exit For
End If
Next
I read on the web that my hosting services has server trust level set to Medium.
hope that someone can help me to solve this.
I got the same problem publishing my web site to Aruba servers.
I solved it casting the dynamic object retrieved from Facebook before accessing it.
In your code you have:
For Each acct As Object In accts.data
But accts is just an object and Aruba prevent to access its .data element
Try to create classes containing all the elemnts you expect to receive from FB.
Public Class fbcls_accts
Public data() As fbcls_image
End Class
Public Class fbcls_image
Public height As Integer
Public width As Integer
Public source As String
End Class
Then use JavaScriptSerializer to convert your object to the final type:
result = (New JavaScriptSerializer().ConvertToType(Of fbcls_accts)(accts))
I'm getting "Me' is valid only within an instance method" error when I convert function to a PageMethods.
Private Shared objDT As System.Data.DataTable
Private Shared objDR As System.Data.DataRow
<WebMethod()> Public Shared Function addstn(itemID As String) As String
For Each Me.objDR In objDT.Rows
If objDR("ProductID") = ProductID Then
If objDR("Options") = desc Then
objDR("Quantity") += 1
blnMatch = True
Exit For
End If
End If
Next
End Function
If I remove Me, I get different error.
I got this shopping cart script somewhere online and I'm not sure what to replace "Me.objDR" with something else.
Thank you in advance
The Me keyword provides a way to refer to the specific instance of a class or structure in which the code is currently executing.
In a shared context there is no instance. You can reference your variable directly or via ClassName.VariableName.
This could work(i'm not sure wherefrom the other variables are):
For Each objDR In objDT.Rows
If ProductID.Equals(objDR("ProductID")) _
AndAlso desc.Equals(objDR("Options"))Then
Dim q = CInt(objDR("Quantity"))
objDR("Quantity") = q + 1
blnMatch = True
Exit For
End If
Next
http://msdn.microsoft.com/en-us/library/20fy88e0%28v=vs.80%29.aspx
Me cannot be used since its a shared method, shared methods are not accessed through an instance.
What is the other error you receive?
Public Shared Function addstn(...
The "Shared" means it's not an instance method. If you want to use "Me", you can't use "Shared". Use the name of the class/module instead.
I'm CSSing my project and would like to customize the font color for the feedback label. My project is built in 3 layers (DAL, BLL, normal page). In the BLL I catch exceptions and I guess this is where I would add the CSS stylesheet reference. Unfortunately, I can't get it to work, this is what it looks like.
BLL
Public Function deleteCustByCustID(ByVal CustID As Integer) As Boolean
If dataCust.DeleteCust(Cust) Then
Throw New Exception("The customer was removed.")
Return True
Else
Throw New Exception("The customer wasn't removed. Please try again.")
Return False
End If
End Function
ASPX.vb page
Try
bllCust.deleteCustByCustID(CustID)
Catch ex As Exception
lblFeedback.Text = ex.Message
End Try
I have my CSS pages stored in a CSS folder. I would like to assign the font color lime to a success and the font color red to a failure.
Any help is much appreciated!
Another option: if you have assigned an ID value to the markup for your "feedback" area, and if you added a runat="server" to that element, you can access the CssClass property in your code-behind file.
By way of example:
Markup
<div id="Feedback" runat="server"></div>
Code
Me.Feedback.CssClass="error"
Then you can use the CSS rules denoted by #rockerest in his answer.
EDIT:
Okay, I looked at your code again, and I see a big problem: you should NEVER use exceptions as a method of controlling program flow. That is probably error #1.
A not-too-uncommon method of returning a more meaningful result from your methods is to encapsulate a result object. Here is a simple example:
Public Class Result
Public IsValid As Boolean
Public Message As String
Public Sub New(ByVal isValid As Boolean, ByVal message As String )
IsValid = isValid
Message = message
End Sub
End Class
You would amend your current function to return a Result object instead of Boolean, and assign the values of the Result object depending on your query results:
Public Function deleteCustByCustID(ByVal CustID As Integer) As Result
Dim result as New Result
If dataCust.DeleteCust(Cust) Then
result = new Result( true, "The customer was removed." )
Else
result = new Result( false, "The customer wasn't removed. Please try again." )
EndIf
Return result
End Function
Then, in whatever code calls the deleteCustByCustID method, you assign the Message property to the content of the feedback area and the CssClass that matches the IsValid state.
Make sense?
EDIT 2:
Okay, assuming you have a CSS class for errors, ".error" and a CSS class for, uh, not errors, ".success". Then, let's pretend the following snippet was inside an event handler or somesuchthing:
Dim result As new Result = deleteCustByCustID( 42 )
Now you have a Result object that has a IsValid state value (it will be either true or false) and a Message string value. Your next step is to apply the message to the feedback element's (I'll assume here that you use an ASP.NET Label control) Text property, and then, based on the value of result.IsValid, assign the correct class to the label's CssClass property:
myFeedbackLabel.Text = result.Message
If result.IsValid Then
myFeedbackLabel.CssClass = "success"
Else
myFeedbackLabel.CssClass = "error"
EndIf
HTH.
The best answer here would be to define two classes in your main stylesheet like so:
.okay{
color: lime;
}
.error{
color: red;
}
And then simply set the correct variable in your BLL: "okay" for the first part of your if statement, and "error" for the else part. In the page, just use that variable as part of the class definition for the message:
Try
bllCust.deleteCustByCustID(CustID)
Catch ex As Exception
lblFeedback.Text = ex.Message
lblFeedback.cssClass = lblFeedback.cssClass + " " + [THE VARIABLE WORD HERE]
End Try
This should do what you're wanting.
I'm trying to return an object from a function.
E.g. Ive got a function populateDog that returns Dog
So in my aspx class I want to be able to be able to pass in Lassie as the name of the dog(I have a dog class) and have the function return the object with the data it populated.
So that in my aspx class i can go lassie.color, lassie.breed
Main Goal is: lbl.txt = Lassie.Color
Thanks
EDIT
Public Function populateDog(ByVal dName As String) As dog
dbConnection()
Dim ObjDog As New dog(dName)
ObjDog.sBreed = "Collie"
Return ObjDog
End Function
The idea was to have a database and I would eventually pass in an ID to query results and return it. For now though I just wanna get this understanding and move forward.
Public Function populateDog(ByVal dName As String) As dog
dbConnection()
Dim ObjDog As New dog(dName)
ObjDog.sBreed = "Collie"
ObjDog.Color = "White"
Return ObjDog
End Function
and
Dim Lassie as dog
Lassie = populateDog("Lassie")
lbl.Text = Lassie.Color
assuming your dog class is something like
Class dog
Public sBreed As String
Public Color As String
' other properties and functions
End Class