How to detect currently dropped value on combobox using mouse hover [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Using combobox at C#, VS 2010, Forms.
After you drop a combobox, you scroll on list of choices with your mouse. Which event triggers this on MSDN Combobox Events
example: list of choices on combobox are apple, banana, chocolate, etc., you point at apple it calls the event, you point on banana it calls the same event, etc.
Also how do I get the values its pointing at?
If there is no event available, can I make one via program?
Been googling for a long time now can't seem to find what I need.

Which event triggers on this...
If you create a combo box and add items, you can set the SelectedIndexChanged event and set it to your own custom event handler, like this:
comboBox1.Items.Add("Apple");
comboBox1.Items.Add("Banana");
comboBox1.Items.Add("chocolate");
comboBox1.SelectedIndexChanged += ComboBox1OnSelectedIndexChanged;
The method receives a sender object that is of type combobox, the only tricky thing is that the signature sets it to an object. Casting it allows us to pull out the value.
private void ComboBox1OnSelectedIndexChanged(object sender, EventArgs eventArgs)
{
myvalue = ((ComboBox)sender).SelectedValue;
}

Seems like you could get what you want from this
Redrawing of owner-drawn winforms combobox items
specifically when
(state & DrawItemState.HotLight) > 0
Let me know if more explanation is in order.
EDIT --
What I mean is, by implementing ownerdraw, you are made aware of what item the mouse is over. When the mouse is over the item, then, per the linked article
((state & DrawItemState.Selected) > 0) || ((state & DrawItemState.HotLight) > 0)
is true.
So in that case you can fire an event as needed with the info the OP wants.

Related

C# do (not) call event [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am quite new to this and I am building an easy app in visual studio in C# which is plotting graphs and user can customize them by using checkboxes and radiobuttons. These are linked to events and when checkstate is changed, the event is called and the code do its job. But these events are called even when the checkstate was not changed and all plotted areas reload multiple times which is not very pleasant to the user. Can you please advise me how to call the event only when it is required. It's WinForms. An example is below. I want to display the output in both cases - if the bool value is true or false, the output is dependent on this and the outcome is different.
`
private void CheckBoxCountInvalid_CheckStateChanged(object sender, EventArgs e)
{
if (checkBoxCountInvalid.Checked)
countInvalid = true;
else
countInvalid = false;
ShowOutput();
}
`
An (if else) sounds like if would work fine for that problem.

Reload/Refresh data from DB periodically using entity framework 5 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm using entity framework 5 on my winform application. I have a datagridView on my form which contains data from my database:
public partial class Form1 : Form
{
etudiantEntities cont = new etudiantEntities();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cont.etudiant1.Load();
etudiant1DataGridView.DataSource = cont.etudiant1.Local;
}
Right now, every thing is perfect.
Now, i want to reload data when there is an update happened in other forms. I want to reload it periodically.
Is there a way to do that with entity framework?
Thank you!
You have like five problems at once. Writing a proper answer would require a book (which I would suggest you to go read anyway), so this answer is a summary you can use to learn more.
First, you need to elaborate on "when there is an update happen[ing] in other forms". You need to detect this change. How to do that, depends on how that form works. Hopefully it does using data binding and INotifyPropertyChanged, see Raise an event whenever a property's value changed?.
Then on these "other forms", you subscribe to their model's PropertyChanged event and propagate that as an event on each form. Be sure to unsubscribe when appropriate as well. In the PropertyChanged event handler of your form, you raise an event that's specific to that form, like MyModelChanged.
Now you have a form that can notify interested parties of events, by subscribing to that event.
Something like this:
var yourEditForm = new YourEditForm();
yourEditForm.MyModelChanged += this.YourEditForm_MyModelChanged;
yourEditForm.Show();
Now where you place this code is pretty crucial. When working with multiple forms you want to communicate with each other, you need some kind of "controller" (or give it a name) that knows about all forms and their events that are relevant to your application, and ties it all together.
So in your controller you now have the above code and this event handler:
private void YourEditForm_MyModelChanged(object sender, EventArgs e)
{
}
Now in that event handler, you can let your aptly named Form1 reload its data. You can do so by exposing a public method that does just that:
public void RefreshGrid()
{
cont.etudiant1.Load();
etudiant1DataGridView.DataSource = cont.etudiant1.Local;
}
There's your "refresh". You can call form1.RefreshGrid() in the event handler shown above.
Note that all of this is pretty much hacked together. Go read a tutorial or two about data binding in WinForms to let this properly be handled, because doing it manually is going to be a pain to maintain.
You can start by reading Data Binding and Windows Forms and Change Notification in Windows Forms Data Binding on MSDN.

Maintaining focus on textbox while I click on different buttons [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am currently trying to develop a custom keyboard for WinCE application.Currently I have a form with a text box and a button. The issue is how can I maintain the focus on the keyboard when I click on the mouse to SendInput (to make sure that the textbox capture that input). One way is to set the "Focusable" property but I can't seem to set that on a Windows Form. I hope someone could help me on this. Thanks!
If you are not afraid of moving to the native side you may consider to implement a Software Input Panel (SIP). It will behave in the way you describe and can be used by any application running on the device.
This documentation is for Compact 2013, but it's also valid for previous releases (you can find release-specific versions on MSDN but they were pretty good in hiding them):
http://msdn.microsoft.com/en-us/library/ee500563.aspx
You should set the TextBox.Focus() on the button press event handler. I assume the button has a KeyPress or KeyDown function.
A more flexible alternative would be to store the last focused control.
private Control lastFocusedControl;
And when the text box is focused on it sets the value using the GotFocus event.
private void TextBox_GotFocus(object sender, EventArgs e)
{
lastFocusedControl = (Control)sender;
}
And then in the event handler you can simply do.
lastFocusedControl.Focus();
Although it is for VB.Net it has some good ideas: http://msdn.microsoft.com/en-us/magazine/hh708756.aspx
See WS_EX_NOACTIVATE and
If (m.Msg = WM_MOUSEACTIVATE) Then
m.Result = MA_NOACTIVATE
Else
MyBase.WndProc(m)
End If
Now the challenge is to adopt this to your idea (a separate process and form? or a panel with buttons?).
OTOS MS provides a custom keyboard SDK API set to write custom software keyboards for Windows Mobile (c/C++).

How can we access dynamically created controls in C# windows application? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am designing C# windows application using 3-Tier architecture, Basically i am making management system for super market in which i have created a form for generating BILL. I have used FlowLayout panel for generating 4 ComboBoxes, 3 TextBoxes and 2 Numeric UpDown respectively to hold Bill values accordingly. I have supplied add button with which we can create these controls dynamically on each button click, so, for example, when user clicks on the add button to add new item, new row will be generated with 4 CBs, 3 TBs and 2 NUpDowns. Having mentioned the scenario, I have following queries:
How can i access each and every of the control in each row in turn? So, for example, I want to access 3rd ComboBox of 5th row, How can I access that in particular.
I am using 3-tier application architecture to design the application. I have placed their functions in BusinessLogicLayer and have called them in UI in button event. Am i alright with this approach?
I want each ComboBox, in each row, to be connected with the first one. for example first one indicates main category when user selects anything from that, second combo box(sub category) should show items connected with the first one and so on. How can i do that?
Thanks
I don't know the very architecture of your application, but let me try and help you. Assumed you have some sort of Customer, Product and Order objects for your business logic the workflow could be the following
void ButtonFindCustomer_Click(object sender, EventArgs e)
{
m_order.Customer = Customer.Find(TextBoxFirst.Text, TextBoxLast.Text, TextBoxCustomerID.Text);
}
void ButtonAddProduct_Click(object sender, Event args)
{
m_order.AddArticle(Product.Find(TextBoxArticleNumber.Text), NumericUpDownArticleAmount.Value);
}
void ButtonSubmit_Click(object sender, EventArgs e)
{
m_order.Place();
}
I used the prefix for the member variable here just for clarity, for the lack on context. In your real code you should avoid it. Furthermore I have omitted the check if the user exists in ButtonFindCustomer_Click, this case should be handled, too. In ButtonAddProduct_Click the case a product is added is handled. Again the existence of the product is assumed, thus you'll have to introduce some error handling here, for example by using something like
if(Product.Exists(productNumber))
{
// add to order
}
else
{
// emit error message
}
The actual transaction is performed in the ButtonSubmit_Click handler. The order is validated and then sent to the data access layer (for example written to a SQL-Database). Once again the error handling is missing, please keep that in mind.
To get to your actual questions:
1) You'll somehow have to keep track the controls you created. If you are always creating them in the same groups, you should consider a user control, which avoids keeping track of which controls belong together. The control could - for example - contain a drop down box for categories, a drop down box for products and a numeric up down for the amount.
class ProductSelector : UserControl
{
... //add controls to user control
public event EventHandler CategoryChanged;
public event EventHandler ProductChanged;
...
public void PopulateCategories(list<string> names, list<string> ids)
{
...
}
public void PopulateProducts(list<string> names, list<string ids)
{
...
}
}
Now any time anything about the product is changed, you'll receive an event and can directly access your user control and all necessary data (if you wrote the functions in ProductSelector). If you want to access the controls in turn, you can either get all controls of type ProductSelector from MainForm.Controls or create a list of all ProductControls you added.
2) Yes, I think you'll be alright with this approach. Somewhere you'll have to access the functions of your BLL and UI event handlers are a good starting point for.
3) Please see me answer to 1).
I hope my explanation met your requirements and answered you questions. Feel free to ask, if I may help you any further.

how to make a label increment by 1 every second [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
for example i would like a label increment by 1 point every second.
For example
the label will equal 1, then after one second it will equal 2, then after one second it will equal 3, after one second it will equal 4 etc.
its like for a survival scoring mechanism. See how long you can last without dying.
From toolbar add a timer to your form, set it's interval to 1000 (=1s) and set Enabled to true, write an Tick event handler as below:
var num = int.parse(lbl_increase.Text);
lbl_increase.Text = (num+1).ToString();
you can do it like this
1) drag a timer to your form.
the timer is under the components tab (see picture)
2) click your timer (it is not visible on your form, but it is under your form) and go to the properties tab
3) change 'enabled' to true and change 'interval' to 1000.
4) go to event tab and create an event for 'Tick'
5) add this code in your method :
private void timer1_Tick(object sender, EventArgs e)
{
//Convert the text from your label to an int
int i = Convert.ToInt32(yourLabel.Text);
//increment the int
i++;
//set the text to the value of the int
yourLabel.Text = i.ToString();
}
i hope it helps

Categories

Resources