Save User Settings With IsolatedStorage in Silverlight 5 (vb) - silverlight-5.0

I want to save a value from a TextBox, an item of text from the SelectedIndex of a ComboBox and a CheckBox checked true or false. I then want to recall the saved settings with the OnClick from a button. I've had a go with the TextBox below but I get the following error statement: KeyNotFoundException was unhandled by user code. The given Key was not in the present dictionary?
Can anybody help me?
Private Sub Button2_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button2.Click
Dim Store As IsolatedStorageSettings = IsolatedStorageSettings.ApplicationSettings
IsolatedStorageSettings.ApplicationSettings(TextBox1.Text) = TextBox1
End Sub
Private Sub Button3_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button3.Click
TextBox1 = (IsolatedStorageSettings.ApplicationSettings(TextBox1.Text))
End Sub

The ApplicationSettings works similar to a Hashtable. It requires a key to identify any setting you wish to store.
You can store your setting like this (air code):
IsolatedStorageSettings.ApplicationSettings("MyTextBoxKey") = TextBox1.Text
And you can retrieve your setting like this:
TextBox1.Text = CStr(IsolatedStorageSettings.ApplicationSettings("MyTextBoxKey"))
You can read more about the ApplicationSettings on MSDN

Related

Replacing Field values in Word Document QAxObject QT / C++

I'm really new to QT and I was tasked to update some field values in word document programmatically, currently I can replace text from word document fine, but when that field value is inside an object (table or anything) it's not working, my code is:
QString outFile("D:\\#test files\\output.docx");
QString inFile1("D:\\#test files\\input.docx");
QAxObject axObject("Word.Application");
QAxObject* documents = axObject.querySubObject("Documents");
QAxObject* document = documents->querySubObject("Open(const QString&, bool)", inFile1, true);
QAxObject* selection = axObject.querySubObject("Selection");
auto find = selection->querySubObject("Find");
QString sOld = "${name}";
QString sNew = "Ibrahim";
bool bMatchCase = false;
bool bMatchWholeWord = false;
bool bMatchWildCards = false;
bool bReplaceAll = true;
QVariantList vl = { sOld, bMatchCase, bMatchWholeWord, bMatchWildCards, false, false, true, 1, false, sNew, bReplaceAll ? "2" : "1" };
find->dynamicCall("Execute(QString,bool,bool,bool,bool,bool,bool,int,bool,QString,int)", vl);
document->dynamicCall("SaveAs(const QString&)", outFile);
document->dynamicCall("Close()");
axObject.dynamicCall("Quit()");
If you can help it would be awesome :)
If you can change the nature of the target files, you would do well replacing your targets with real Word DocVariable or DocProperty fields. Then use your code to change the variable or properties and update the related fields in the documents. Some document properties (under the Quick Parts > Document Properties menu) are mapped to XML data points and do not require updating of fields if those are used.
The placeholder can be (1) a DocVariable field or (2) a DocProperty field. You can change the variables or properties using code and then update the field.
You can also use one of the built-in mapped Document Property Content Controls in which case there is no need to update a field if the property is changed. It is automatic. More on these in my related page: Repeating Data Mapped Document Property Content Controls or Other Mapped Content Controls.
Here are links to two Word MVP pages on accessing Document Properties using vba.
How Can I Get Access to the Document Properties of a Document Without Opening the Document
How to Use a Single vba Procedure to Read or Write Both Built-In and Custom Document Properties

Sitecore How to get the value inside the field 'Media' of a Media Item

Update 2:
I am still fighting to get the Icon save on the server.
Here what I am doing:
Item item = Sitecore.Context.Database.GetItem(imageId);
var imageIconUrl = Sitecore.Resources.Images.GetThemedImageSource(item.Appearance.Icon, ImageDimension.id32x32);
if (!string.IsNullOrEmpty(imageIconUrl))
{
// download the icon from the url
var iconFullPath = "e:\\pngIcons\\excelIcon.png";
var webClient = new System.Net.WebClient();
var downloadPath = "http://serverName/" + imageIconUrl;
webClient.DownloadFile(downloadPath, iconFullPath);
}
The variable downloadPath contains following string:
http://serverName/sitecore/shell/themes/standard/~/media/E503BA48C89B422D8400393F1C7086A7.ashx?h=32&thn=1&w=32&db=master
At the end what I can see is a png file but there is nothing in it. I also copy the string I get in variable downloadPath and pasted it in browser and I can see the Icon as follow:
Please let me know what I am doing wrong. Or How I can save the Icon. Thanks!!
Original Question:
The sitecore media item has a field "Media". I am talking about this:
I want to access this field. And the reason is:
If I access it with e.g. item.GetMediaStream() then I will get the complete file. I just want to save this little icon some how on the server. Is it possible ?
To get the icon/thumbnail you can use
var icon = Sitecore.Resources.Media.MediaManager.GetThumbnailUrl(mediaItem);
To get the url of the thumbnail.
If you want the stream of the thumbnail you need to use the MediaData object. Like this:
var mediaItem = new MediaItem(item)
var mediaData = new MediaData(mediaItem);
var iconStream = mediaData.GetThumbnailStream();
if (iconStream.Length < 0)
{
// The stream is empty, its probably a file that Sitecore can't
// generate a thumbnail for. Just use the Icon
var icon = item.Appearance.Icon;
}
This will get the icon or thumbnail of the actual media blob that is attached to the media item. If you just want the icon of the Sitecore item, then use Martins method.
If the stream is empty, then Sitecore can't generate a thumbnail for it, so you can just use the icon file for the media item template. item.Appearance.Icon
Appearance section (of Standard Template) has a field called Icon - this is where you can modify icon for the item. There is also a field called Thumbnail - your excel icon is sitting there
You can access the field programmatically, just mind that it starts with two underscores: __Icon:
var iconField = item.Fields["__Icon"];
var thumbnailField = item.Fields["__Thumbnail"];
Update: As you asked, below is the code that saves either of fields into the file on a drive. I have tested the code and confirm successfully stores icon from thumbnail field into the file:
string mediaItemPath = "/sitecore/media library/Files/ExcelFile";
string mediaFiedlName = "Thumbnail"; // also can be "__Icon"
var item = Sitecore.Context.Database.GetItem(mediaItemPath);
var iconField = item.Fields[mediaFiedlName];
if (iconField.HasBlobStream)
{
var thumb = (ImageField)iconField;
var bl = ((MediaItem)thumb.MediaItem).InnerItem.Fields["blob"];
Stream stream = bl.GetBlobStream();
byte[] buffer = new byte[8192];
using (FileStream fs = File.Create("D:\\you_file_name.ico")) // change your path
{
int length;
do
{
length = stream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, length);
}
while (length > 0);
fs.Flush();
fs.Close();
}
}
Update 2: If that does not help then I would advise to look around the way how that icon gets generated on Media field in Sitecore. In the worst case you may do the following - right click that icon and see its url. You will have something similar to what I assign to variable below:
string url = "http://test81/sitecore/shell/Applications/-/media/B4F61650CBE84EE396649602C0C48E1B.ashx?bc=White&db=master&h=128&la=en&mw=640&thn=1&vs=1&ts=b8046903-ae57-4f9d-9dd5-b626ee5eee90";
Of course your URL should have your hostname and media prefix and the rest of parameters. Then use Webclient with that modified URL:
WebClient webClient = new WebClient();
webClient.DownloadFile(url, "e:\\you_file_name.ico");
Not ideal, but may work. Note, that the code above should work in a context of already logged user, so you need to authorise you Webclient prior that (many articles on S.O. how to do that).
Please reply if that approach has worked for you (I've spent decent time writing and testing that code in debugger, so would want to know whether that helped)

WXPython: getting the initial value of a combo box

In a dialog box I have a combo box and a text field. I would like to make so that if one particular value in the combo box is selected, the text field would be disabled (or hidden), and if another value is selected, the text field would be enabled.
I have:
self.myCombo = wx.ComboBox(parent=self, choices=['value1', 'value2'], style = wx.CB_READONLY)
self.myCombo.Bind(wx.EVT_COMBOBOX, self.onChange)
# ...
def onChange(self, ev):
self.myTextField.Enable(False) if self.myCombo.GetValue() != "value1" else self.myTextField.Enable(True)
And this does work like a charm, the text field gets enabled and disabled.
However, I would like to have the text field enabled or disabled depending on the initial value of the combo box, meaning the value gotten from a config file and selected when the dialog box is open.
I've tried the same:
self.myTextField = wx.TextCtrl(parent=self)
self.myTextField.Enable(False) if self.myCombo.GetValue() != "value1" else self.myTextField.Enable(True)
but this doesn't work. I've tried GetSelection also, but when logging this, both GetValue and GetSelection return -1.
The combobox probably isn't fully initialized yet when you try to query it. If you want to disable it when it loads, you don't have to check its value. Just disable it. But for what you want to do, I would recommend using wxPython's wx.CallAfter() method.
Something like the following should suffice:
def __init__(self):
# initialize various variables
wx.CallAfter(self.check_combobox, args)
def check_combobox(self, args):
self.myTextField.Enable(False) if self.myCombo.GetValue() != "value1" else self.myTextField.Enable(True)

Continually check process and show Button/TextBox

I am attempting to create a form using VB.Net that checks if the IExplorer process is running and then display a RichTextBox (which advises the user to close IE) is the process = 1 and a Button to proceed to the next form if the process = 0.
That's the easy part, the hard part is that if the process was = 0 when the form was loaded then the user opens IE, I want to remove the button and show the RichTextBox (which advises the user to close IE) and once again if they close IE the Button reappears.
I have the button and RichTextBox in the form_load with an If statement that shows depending on IE being open or not, but i cannot get them to swap over if IE is closed or opened, any help will be greatly appreciated.
Here is the code I have in the Form_load for the RTB and Button
aProc = Process.GetProcessesByName("iexplore")
If aProc.Length = 0 Then
Dim b1 As New Button
b1.Location = New System.Drawing.Point(274, 244)
b1.Name = "btnOK"
b1.Size = New System.Drawing.Size(75, 29)
b1.TabIndex = 5
b1.Text = "OK"
b1.UseVisualStyleBackColor = False
Me.Controls.Add(b1)
AddHandler b1.Click, AddressOf btn_OK
Else
Dim t1 As New RichTextBox
t1.Location = New System.Drawing.Point(170, 233)
t1.Name = "rtbMessage2"
t1.ReadOnly = True
t1.Font = New System.Drawing.Font("Arial", 9.75!, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
t1.Size = New System.Drawing.Size(293, 40)
t1.TabIndex = 5
t1.Text = ("Internet Explorer is Running - Please Close Internet Explorer to Continue")
Me.Controls.Add(t1)
AddHandler t1.Click, AddressOf btn_OK
End If
Two changes I would make:
Add a timer that continually calls the logic (show/hide the Button/RichTextBox)
Make the Button and RichTextBox always there, but initially invisible.
To do this, I would (on load) create the Button and RichTextBox with .Visible = false. Then create a timer that runs every 500 milliseconds (+/-). That timer would call a function that contains the logic you have above. However, instead of creating the controls (with that logic) just reference them and set their visibility.
In essence, create the controls once, run the logic multiple times.

Cancel Handler on a KeyUP Event? VB.NET

There a way to use e.Cancel () on a keyup event?
I'm trying to validate a textbox with a Regex and I need to cancel the event if it no meets the Regex expression, or delete the key pressed to meet so that the expression
For Example:
Dim rex As Regex = New Regex("^[0-9]{0,9}(\.[0-9]{0,2})?$")
Private Sub prices_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Textbox1.KeyUp,
Dim TxtB As TextBox = CType(sender, TextBox)
If (rex.IsMatch(TxtB.Text) = False ) Then
e.cancel = true
End If
End Sub
ERROR: 'cancel' is not a member of 'System.Windows.Forms.KeyEventArgs'.
Try
If (rex.IsMatch(TxtB.Text) = False) Then
e.SuppressKeyPress = True
End If
From MSDN:
You can assign true to this property in an event handler such as KeyDown in order to prevent user input.
Setting SuppressKeyPress to true also sets Handled to true.
It's unclear what you are trying to cancel though, since you are reading the TextBox.Text value. KeyDown is usually the preferred event to intercept the keystroke.
If you are trying to validate the entire string, the Validating event might be more appropriate. With TextBox controls, you will always have the danger of someone pasting clipboard text into the control, which could bypass key events.