I'm a total PyGTK noob, just to get that out there.
I'm trying to set an entry's text with this code:
def on_button1_clicked(self, builder, data = None):
gtk.Entry(txtInput).set_text("Hello")
but when I click the button I get a type error asking for an integer somewhere on the second line of that code. What's going on? I'm not sure what I'm doing wrong and Google hasn't helped.
TIA
The txtInput should be an integer.
It's the maximum length of the entry (or 0 for maximum).
See gtk.Entry Class Reference.
Related
I am trying to change the Image attribute associated with a button when the button is clicked.
I have added the image I want to display (called "Black Pawn.png") as a resource within the solution and (from searching around and looking at similar questions) am trying to set the image attribute to it as shown below:
Private Sub boardButtonA2_Click(sender As Object, e As EventArgs) Handles boardButtonA2.Click
Dim sprites As Object = My.Resources.ResourceManager
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
End Sub
But when I click the button, the default image on it just disappears and is replaced with nothing instead of Black Pawn.png.
I am pretty new to using Visual Basic and Visual Studio so I may have failed to have added the image as a resource properly but I can see the image in the Solution Explorer above the form the button is in.
Any help or advice would be a great help.
Thanks.
EDIT:
Having thought more about the potential scenario, I'm wondering whether this answer is actually relevant. Your example code uses a hard-coded String but I'm wondering whether the actual String really would be a user selection. I'll leave it here anyway.
ORIGINAL:
There's no reason to use a hard-coded String to get a resource. If the String was from user entry then maybe, depending on the circumstances. As it is though, you should be using the dedicated property for that resource. That means using this:
boardButtonA2.Image = My.Resources.Black_Pawn
Just be aware that a new object is created every time you get a resource from My.Resources. For that reason, don't keep getting the same property over and over. If you need to use a resource multiple times, get it once and assign it to a field, then use that field multiple times, e.g.
Private blackPawnImage As Image
Private Function GetBlackPawnImage() As Image
If blackPawnImage Is Nothing Then
blackPawnImage = My.Resources.Black_Pawn
End If
Return blackPawnImage
End Function
and then:
boardButtonA2.Image = GetBlackPawnImage()
Also, I suggest that you change the name of the property to BlackPawn rather than Black_Pawn. You can change it to whatever you want on the Resources page of the project properties.
EDIT:
If this application is a chess game then you definitely will need every resource image for the playing pieces so you probably ought to get all of them at load and assign them to variables, then use them from those variables over the course of the game.
(I am assuming the question is for WinForms, if it isn't I will remove this answer)
When adding images as resources to a project, you have to pay attention to the name given to the resource after it is imported - the name can be changed if you want.
The image below is from one of my projects:
The name of the files on disk are:
CSV - Excel.png
GreenDot.png
RedDot.png
To fix your problem change the line:
boardButtonA2.Image = sprites.GetObject("Black Pawn.png")
to be:
boardButtonA2.Image = sprites.GetObject("Black_Pawn")
I don't think adding the image by going into Project > Add Existing Item properly added the image as a resource.
I instead went into *Project > (Solution Name) Properties > Resources > Images > Add Existing Item * and added it that way and was then able to get it working using jmcilhinney's method (I think my original method/a variation of it would work too but theirs is better).
I am trying to make a dialogue screen into a reusable message screen, which resizes itself dynamically depending on how long the message is. I'm not sure whether that is even possible. This is my code:
FORM show_messagescreen.
"Size screen dynamically according to needed space
DATA out_length TYPE i.
IF gd_0201_message_output_01 > gd_0201_message_output_01.
out_length = strlen( gd_0201_message_output_01 ).
ELSE.
out_length = strlen( gd_0201_message_output_02 ).
ENDIF.
* Code to change visLength of the two output fields here.
CALL SCREEN 0201
STARTING AT 01 01
ENDING AT out_length 05.
ENDFORM.
As you can see I can resize the screen depending on how long the message is, but when the screen appears there is still a scrollbar at the bottom, because the fields are set at around 70 Characters length. Setting the fields length lower just cuts off the message if it is longer than the field. Can I circumvent this scrollbar somehow?
Please tell me if my code is wrong or dirty, I'm a beginner.
Aparrently not possible, as Dynpro is a very old technology. One might try #Sandra RossiĀ“s possible workaround, which could be much more effort than this feature is worth.
I have two forms with combo boxes. The combo box values are stored in My.Settings.testDevices. (System.Collections.Specialized.String.Collection) with a scope of User.
The second form adds the ability to add items to testDevices, and then upon exit it updates My.Settings.testDevices.
Now, only if I make a change to the settings (adding items only), when I exit back to the main form (which remains loaded throughout the process), my application crashes with the following message:
"Additional information: Destination array was not long enough. Check destIndex and length, and the array's lower bounds."
As I understand it, this might be a concurrency issue, however I'm not sure.
My code:
In my main form Load event: (to load from My.Settings)
testDevicesComboBoxMain.Items.Clear()
My.Settings.testDevices.CopyTo(mainFormTestDevices, 0)
testDevicesComboBoxMain.Items.AddRange(mainFormTestDevices)
Where "testDevicesComboBoxMain" is the combo box on the main form.
On the secondary form Close Event: (to save to My.Settings)
Dim items(testDevicesComboBox.Items.Count - 1) As String
testDevicesComboBox.Items.CopyTo(items, 0)
My.Settings.testDevices.Clear()
My.Settings.testDevices.AddRange(items)
My.Settings.Save()
I have found similar questions on here, but none with answers that I understand :P
As I am a beginner with vb.net, could any answers be provided in an easy to understand form please!
Thanks.
I forgot to add:
Public items(My.Settings.testDevices.Count - 1) As String
Public mainFormTestDevices(My.Settings.testDevices.Count - 1) As String
I tried setting separate declarations just in case there was some kind of conflict. These obviously do the same thing, just with different names.
I fixed it by adding a For loop to read from My.Settings.
For Each i As String In My.Settings.testDevices
testDevicesComboBoxMain.Items.Add(i)
Next
This seems to have cured the problem, and may perhaps be a more "modern" way of doing it?
I've searched and found similar topics but none that really answer my question, or perhaps it's the fact that I'm a beginner and I don't fully understand yet.
What I'm trying to do is use a ComboBox and a TextBox. Once the ComboBox selection changes, the TextBox will load more information about the item in the ComboBox. This is for my company, trying to store canned messages with a title.
So I want to have a title for these messages in a ComboBox drop-down, and once a title is chosen, the message is displayed in the TextBox. Right now, I've got the message creation, which is also part of my program, appending to two different text files, since I'm not sure yet on how to append data from two controls onto the same line and concatenate a Tab or other delimiter.
So my question is, what method should I be going about doing this? The part I am REALLY not understanding is how to get the line number value from the selected item in the TextBox, they load just fine using the following line:
savedmsgComboBox.Items.AddRange(File.ReadAllLines("MessageListTitles.txt"))
So how do I get which line this selection is on, and how do I correspond that line with the message I need to display in the TextBox?
This will only work if both arrays have the same amount of lines. Load both files into arrays.
'declare the arrays
Private msgTitles() As String
Private msgList() As String
'in load event fill them
msgTitles = File.ReadAllLines("MessageListTitles.txt")
msgList = File.ReadAllLines("MessageList.txt")
'load the combobox
savedmsgComboBox.Items.AddRange(msgTitles)
'get the items by the index
Dim msgListItem As String = msgList(savedmsgComboBox.SelectedIndex)
I need a way to run a sub every time that text is added to a richtextbox. There is a changed event, but I have to know exactly what was added and at what position (even as trivial as a single space). I was going to do 'onKeyUp' but if users press CTRL + V, instead of getting the inserted content, I'll end up with just a 'v'.
Is there a way to do this? I need to be able to get the position that the content was added at and what was added. I'm doing this in VB.net.
I want to basically do something similar to http://typewith.me/
Let me show an example of what I want:
TEXT 1:
This is a message, currently no changes have been performed.
TEXT 2:
This is a message, currently none changes have been performed.
OUTPUT:
'ne' added at index position 31.
Cheers!
This might not be the most elegant solution, but the first thing that springs to mind is to store the contents of the text box in a variable. Every time it's changed, check the new contents of the text box against the stored contents and do whatever you need to, then store it again. This handles typing, pasting, deleting, etc.