Is there any way to initialize a HalconWindow or a HSmartWindowControlWPF in C#?
I am having multiple HSmartWindowControlWPFs in a TabView but only those that were visible before are updated. So if I am trying to put an HImage in all HalconWindows, without choosing another Tab than the default before, only the default Tab gets updated, all other HalconWindows stay black. But if they were once selected they are updated.
Is there any way to create this behaviour automatically?
The key is to index through the tabs at initialization so that they initialize the controls.
private void TabbedHalconApp_Loaded(Object sender, RoutedEventArgs e)
{
TabControl1.BeginInit();
for (int index = 0; index < this.TabControl1.Items.Count; index++)
{
this.TabControl1.SelectedIndex = index;
this.TabControl1.UpdateLayout();
}
// Reset to first tab
this.TabControl1.SelectedIndex = 0;
TabControl1.EndInit();
}
Then you can load the image to the Halcon window. Here is how you might do this when the form is loaded.
private void HWindow2_Loaded(Object sender, EventArgs e)
{
(sender as HSmartWindowControlWPF).HalconWindow.DispImage(myTestImage);
}
private void HWindow1_Loaded(Object sender, RoutedEventArgs e)
{
(sender as HSmartWindowControlWPF).HalconWindow.DispImage(myTestImage);
}
Related
So I have a button, and when the button is clicked, a picture box is created. I just wanted to know how I can make a message box appear when I click on that newly created picturebox.
private void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < 1; i++)
{
PictureBox p = new PictureBox();
flowLayoutPanel1.Controls.Add(p);
}
}
you have to add a click event to your picturebox
p.MouseClick += p_MouseClick;
after adding a event this function will be called on that event -
void p_MouseClick(object sender, MouseEventArgs e)
{
MessageBox.Show("clicked");
}
When i click on a button,text will appear in textbox1 but then i want it to change focus to another textbox(textbox2) and when i click the same button again,display the same text but in textbox2.
private void btna_Click(object sender, EventArgs e)
{
textBox1.Text = ("A");
if (textBox1.Text.Length > 0)
{
textBox2.Focus();
}
If you want to alternate between different TextBoxes in your click event to determine which one to update, you can track them in a private variable, and then just switch which one you're using based on the current value, for example:
private TextBox textBoxToUpdate = null;
private void button1_Click(object sender, EventArgs e)
{
// Swap the textBoxToUpdate between textBox1 and textBox2 each time
textBoxToUpdate = (textBoxToUpdate == textBox1) ? textBox2 : textBox1;
textBoxToUpdate.Text = "A";
}
Another way to do this if you're updating multiple controls is to store them in a list and increment an integer that defines the index to the next item.
// holds the text boxes we want to rotate between
private List<TextBox> textBoxesToUpdate = new List<TextBox>();
private void Form1_Load(object sender, System.EventArgs e)
{
// add some text boxes to our list
textBoxesToUpdate.Add(textBox1);
textBoxesToUpdate.Add(textBox2);
textBoxesToUpdate.Add(textBox3);
textBoxesToUpdate.Add(textBox4);
}
// stores the index of the next textBox to update
private int textBoxToUpdateIndex = 0;
private void button1_Click(object sender, EventArgs e)
{
textBoxesToUpdate[textBoxToUpdateIndex].Text = "A";
// increment the index but set it back to zero if it's equal to the count
if(++textBoxToUpdateIndex == textBoxesToUpdate.Count) textBoxToUpdateIndex = 0;
}
I have a certain number of Textboxes, I need to track the last two focused Texboxes. This is the approch that I attempted.
private Control _focusedControl;
private Control _lastfocusedControl;
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp1txt_LostFocus(object sender, System.EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
private void PCp2txt_GotFocus(object sender, EventArgs e)
{
_focusedControl = (Control)sender;
}
This is not working because when I press the button the contenent of the _lastfocusedControl will be the same as _focusedControl because another control was focused by clicking that button.
You can handle Enter event of all those TextBox controls using a single handler and in the handler and keep track of last n focused TextBox controls:
const int n = 2;
TextBox[] textBoxes = new TextBox[n];
private void textBox_Enter(object sender, EventArgs e)
{
var destination = new TextBox[n];
Array.Copy(textBoxes, 1, destination, 0, textBoxes.Length - 1);
textBoxes = destination;
textBoxes[textBoxes.Length - 1] = (TextBox)sender;
}
In above example, we are shifting the array to left, then assign the sender to the last element. This way the array always contains the last n focused TextBox controls for you.
I would suggest, to make it simpple, do something like when your textbox get focused, put the name of it into an array, for example, and after that, list the last two name added of the array created.
I'm trying to create a asp:UploadFile control dynamically on button click event. After creating the first control it won't create a second or third one. Below is my code.
protected void AddFileInputControl_Click(object sender, EventArgs e)
{
FileUpload image = new FileUpload();
image.ID = "image";
fileinputs_div.Controls.Add(image);
}
Any help will be greatly appreciated.
Try to give unique ID for each image, for example using global counter :
private int counter;
protected void AddFileInputControl_Click(object sender, EventArgs e)
{
FileUpload image = new FileUpload();
image.ID = "image" + counter++;
fileinputs_div.Controls.Add(image);
}
Asp.net doesn't save the dynamically created control for the next calls. That means you would need to create them every PostBack. Something like this:
private int _counter = 0
protected void AddFileInputControl_Click(object sender, EventArgs e)
{
for (int i = 0; i < _counter; i++)
{
fileinputs_div.Controls.Add(new FileUpload()
{
ID = string.Format("image #{0}", i);
});
}
_counter++;
}
I am very new and extremely confused as to how I can accomplish this project. The project requests us to create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. This is where I am starting to have problems; I don't need to have what the user selects displayed in a message but the message reflecting what the font size and type were that the user selected. After the user clicks a button, display "Hello" in the selected font and size. I need help in getting the button to display the message in the desired font in a C# Windows Visual Studio 2010 form. I have just a basic code written to start me off which includes the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0;
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0;
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Since this is the beginning of this project, the font name and size that the user selects will eventually produce a message in that chosen font name and size. Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
You can use the ListBox.SelectedIndex property to set the initial selection of the listboxes. For example, you can add the following lines of code to explicitly select the first items in the listboxes after you add the event handlers:
this.listBox1.SelectedIndexChanged += new System.EventHandler (this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler (this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
By default, the SelectedIndex property of a ListBox is -1, which means there is no selection.
To answer your second question, to display 'Hello' in the selected font and size, I will assume that we can simply change the font of the textBox1 control.
First, make sure that textBox1 has some text; put this statement into the Form1 constructor after the call to InitializeComponent:
textBox1.Text = "Hello!";
Then, modify the event handlers to change the typeface and size of the font:
private void UpdateFont()
{
if (listBox1.SelectedIndex == -1 || listBox2.SelectedIndex == -1)
return; // selection not complete yet, so do nothing
string typeface = listBox1.SelectedItem.ToString();
float size = Convert.ToSingle(listBox2.SelectedItem.ToString());
textBox1.Font = new Font(typeface, size);
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateFont();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
UpdateFont();
}
to put the default value of your listboxes:
listBox1.SelectedItem = "Arial";
listBox2.SelectedItem = "8";
or a better "dynamic solution":
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
the following code will make the text font and size change, depending on what the user has selected in the listbox.
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
edit:
you are getting an error because there's probably no text in your textbox.
TextBox1.Text ="this is some text";
add this to your form.